]> granicus.if.org Git - postgresql/blob - src/backend/parser/gram.y
Add ALTER TYPE ... ADD/DROP/ALTER/RENAME ATTRIBUTE
[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-2010, 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
136 %}
137
138 %pure-parser
139 %expect 0
140 %name-prefix="base_yy"
141 %locations
142
143 %parse-param {core_yyscan_t yyscanner}
144 %lex-param   {core_yyscan_t yyscanner}
145
146 %union
147 {
148         core_YYSTYPE            core_yystype;
149         /* these fields must match core_YYSTYPE: */
150         int                                     ival;
151         char                            *str;
152         const char                      *keyword;
153
154         char                            chr;
155         bool                            boolean;
156         JoinType                        jtype;
157         DropBehavior            dbehavior;
158         OnCommitAction          oncommit;
159         List                            *list;
160         Node                            *node;
161         Value                           *value;
162         ObjectType                      objtype;
163         TypeName                        *typnam;
164         FunctionParameter   *fun_param;
165         FunctionParameterMode fun_param_mode;
166         FuncWithArgs            *funwithargs;
167         DefElem                         *defelt;
168         SortBy                          *sortby;
169         WindowDef                       *windef;
170         JoinExpr                        *jexpr;
171         IndexElem                       *ielem;
172         Alias                           *alias;
173         RangeVar                        *range;
174         IntoClause                      *into;
175         WithClause                      *with;
176         A_Indices                       *aind;
177         ResTarget                       *target;
178         struct PrivTarget       *privtarget;
179         AccessPriv                      *accesspriv;
180         InsertStmt                      *istmt;
181         VariableSetStmt         *vsetstmt;
182 }
183
184 %type <node>    stmt schema_stmt
185                 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterFdwStmt
186                 AlterForeignServerStmt AlterGroupStmt
187                 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
188                 AlterCompositeTypeStmt AlterUserStmt AlterUserMappingStmt AlterUserSetStmt
189                 AlterRoleStmt AlterRoleSetStmt
190                 AlterDefaultPrivilegesStmt DefACLAction
191                 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
192                 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
193                 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
194                 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
195                 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
196                 CreateFdwStmt CreateForeignServerStmt CreateAssertStmt CreateTrigStmt
197                 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
198                 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt DoStmt
199                 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
200                 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
201                 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
202                 DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
203                 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
204                 LockStmt NotifyStmt ExplainableStmt PreparableStmt
205                 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
206                 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
207                 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
208                 SelectStmt TransactionStmt TruncateStmt
209                 UnlistenStmt UpdateStmt VacuumStmt
210                 VariableResetStmt VariableSetStmt VariableShowStmt
211                 ViewStmt CheckPointStmt CreateConversionStmt
212                 DeallocateStmt PrepareStmt ExecuteStmt
213                 DropOwnedStmt ReassignOwnedStmt
214                 AlterTSConfigurationStmt AlterTSDictionaryStmt
215
216 %type <node>    select_no_parens select_with_parens select_clause
217                                 simple_select values_clause
218
219 %type <node>    alter_column_default opclass_item opclass_drop alter_using
220 %type <ival>    add_drop opt_asc_desc opt_nulls_order
221
222 %type <node>    alter_table_cmd alter_type_cmd
223 %type <list>    alter_table_cmds alter_type_cmds
224
225 %type <dbehavior>       opt_drop_behavior
226
227 %type <list>    createdb_opt_list alterdb_opt_list copy_opt_list
228                                 transaction_mode_list
229 %type <defelt>  createdb_opt_item alterdb_opt_item copy_opt_item
230                                 transaction_mode_item
231
232 %type <ival>    opt_lock lock_type cast_context
233 %type <ival>    vacuum_option_list vacuum_option_elem
234 %type <boolean> opt_force opt_or_replace
235                                 opt_grant_grant_option opt_grant_admin_option
236                                 opt_nowait opt_if_exists opt_with_data
237
238 %type <list>    OptRoleList AlterOptRoleList
239 %type <defelt>  CreateOptRoleElem AlterOptRoleElem
240
241 %type <str>             opt_type
242 %type <str>             foreign_server_version opt_foreign_server_version
243 %type <str>             auth_ident
244 %type <str>             opt_in_database
245
246 %type <str>             OptSchemaName
247 %type <list>    OptSchemaEltList
248
249 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
250
251 %type <list>    TriggerEvents TriggerOneEvent
252 %type <value>   TriggerFuncArg
253 %type <node>    TriggerWhen
254
255 %type <str>             copy_file_name
256                                 database_name access_method_clause access_method attr_name
257                                 name cursor_name file_name
258                                 index_name opt_index_name cluster_index_specification
259
260 %type <list>    func_name handler_name qual_Op qual_all_Op subquery_Op
261                                 opt_class opt_inline_handler opt_validator validator_clause
262
263 %type <range>   qualified_name OptConstrFromTable
264
265 %type <str>             all_Op MathOp
266
267 %type <str>             iso_level opt_encoding
268 %type <node>    grantee
269 %type <list>    grantee_list
270 %type <accesspriv> privilege
271 %type <list>    privileges privilege_list
272 %type <privtarget> privilege_target
273 %type <funwithargs> function_with_argtypes
274 %type <list>    function_with_argtypes_list
275 %type <ival>    defacl_privilege_target
276 %type <defelt>  DefACLOption
277 %type <list>    DefACLOptionList
278
279 %type <list>    stmtblock stmtmulti
280                                 OptTableElementList TableElementList OptInherit definition
281                                 OptTypedTableElementList TypedTableElementList
282                                 reloptions opt_reloptions
283                                 OptWith opt_distinct opt_definition func_args func_args_list
284                                 func_args_with_defaults func_args_with_defaults_list
285                                 func_as createfunc_opt_list alterfunc_opt_list
286                                 aggr_args old_aggr_definition old_aggr_list
287                                 oper_argtypes RuleActionList RuleActionMulti
288                                 opt_column_list columnList opt_name_list
289                                 sort_clause opt_sort_clause sortby_list index_params
290                                 name_list from_clause from_list opt_array_bounds
291                                 qualified_name_list any_name any_name_list
292                                 any_operator expr_list attrs
293                                 target_list insert_column_list set_target_list
294                                 set_clause_list set_clause multiple_set_clause
295                                 ctext_expr_list ctext_row def_list indirection opt_indirection
296                                 reloption_list group_clause TriggerFuncArgs select_limit
297                                 opt_select_limit opclass_item_list opclass_drop_list
298                                 opt_opfamily transaction_mode_list_or_empty
299                                 OptTableFuncElementList TableFuncElementList opt_type_modifiers
300                                 prep_type_clause
301                                 execute_param_clause using_clause returning_clause
302                                 opt_enum_val_list enum_val_list table_func_column_list
303                                 create_generic_options alter_generic_options
304                                 relation_expr_list dostmt_opt_list
305
306 %type <range>   OptTempTableName
307 %type <into>    into_clause create_as_target
308
309 %type <defelt>  createfunc_opt_item common_func_opt_item dostmt_opt_item
310 %type <fun_param> func_arg func_arg_with_default table_func_column
311 %type <fun_param_mode> arg_class
312 %type <typnam>  func_return func_type
313
314 %type <boolean>  TriggerForType OptTemp
315 %type <oncommit> OnCommitOption
316
317 %type <node>    for_locking_item
318 %type <list>    for_locking_clause opt_for_locking_clause for_locking_items
319 %type <list>    locked_rels_list
320 %type <boolean> opt_all
321
322 %type <node>    join_outer join_qual
323 %type <jtype>   join_type
324
325 %type <list>    extract_list overlay_list position_list
326 %type <list>    substr_list trim_list
327 %type <list>    opt_interval interval_second
328 %type <node>    overlay_placing substr_from substr_for
329
330 %type <boolean> opt_instead
331 %type <boolean> opt_unique opt_concurrently opt_verbose opt_full
332 %type <boolean> opt_freeze opt_default opt_recheck
333 %type <defelt>  opt_binary opt_oids copy_delimiter
334
335 %type <boolean> copy_from
336
337 %type <ival>    opt_column event cursor_options opt_hold opt_set_data
338 %type <objtype> reindex_type drop_type comment_type
339
340 %type <node>    fetch_args limit_clause select_limit_value
341                                 offset_clause select_offset_value
342                                 select_offset_value2 opt_select_fetch_first_value
343 %type <ival>    row_or_rows first_or_next
344
345 %type <list>    OptSeqOptList SeqOptList
346 %type <defelt>  SeqOptElem
347
348 %type <istmt>   insert_rest
349
350 %type <vsetstmt> set_rest SetResetClause
351
352 %type <node>    TableElement TypedTableElement ConstraintElem TableFuncElement
353 %type <node>    columnDef columnOptions
354 %type <defelt>  def_elem reloption_elem old_aggr_elem
355 %type <node>    def_arg columnElem where_clause where_or_current_clause
356                                 a_expr b_expr c_expr func_expr AexprConst indirection_el
357                                 columnref in_expr having_clause func_table array_expr
358                                 ExclusionWhereClause
359 %type <list>    ExclusionConstraintList ExclusionConstraintElem
360 %type <list>    func_arg_list
361 %type <node>    func_arg_expr
362 %type <list>    row type_list array_expr_list
363 %type <node>    case_expr case_arg when_clause case_default
364 %type <list>    when_clause_list
365 %type <ival>    sub_type
366 %type <list>    OptCreateAs CreateAsList
367 %type <node>    CreateAsElement ctext_expr
368 %type <value>   NumericOnly
369 %type <list>    NumericOnly_list
370 %type <alias>   alias_clause
371 %type <sortby>  sortby
372 %type <ielem>   index_elem
373 %type <node>    table_ref
374 %type <jexpr>   joined_table
375 %type <range>   relation_expr
376 %type <range>   relation_expr_opt_alias
377 %type <target>  target_el single_set_clause set_target insert_column_item
378
379 %type <str>             generic_option_name
380 %type <node>    generic_option_arg
381 %type <defelt>  generic_option_elem alter_generic_option_elem
382 %type <list>    generic_option_list alter_generic_option_list
383 %type <str>             explain_option_name
384 %type <node>    explain_option_arg
385 %type <defelt>  explain_option_elem
386 %type <list>    explain_option_list
387 %type <node>    copy_generic_opt_arg copy_generic_opt_arg_list_item
388 %type <defelt>  copy_generic_opt_elem
389 %type <list>    copy_generic_opt_list copy_generic_opt_arg_list
390 %type <list>    copy_options
391
392 %type <typnam>  Typename SimpleTypename ConstTypename
393                                 GenericType Numeric opt_float
394                                 Character ConstCharacter
395                                 CharacterWithLength CharacterWithoutLength
396                                 ConstDatetime ConstInterval
397                                 Bit ConstBit BitWithLength BitWithoutLength
398 %type <str>             character
399 %type <str>             extract_arg
400 %type <str>             opt_charset
401 %type <boolean> opt_varying opt_timezone
402
403 %type <ival>    Iconst SignedIconst
404 %type <str>             Sconst comment_text notify_payload
405 %type <str>             RoleId opt_granted_by opt_boolean ColId_or_Sconst
406 %type <list>    var_list
407 %type <str>             ColId ColLabel var_name type_function_name param_name
408 %type <node>    var_value zone_value
409
410 %type <keyword> unreserved_keyword type_func_name_keyword
411 %type <keyword> col_name_keyword reserved_keyword
412
413 %type <node>    TableConstraint TableLikeClause
414 %type <ival>    TableLikeOptionList TableLikeOption
415 %type <list>    ColQualList
416 %type <node>    ColConstraint ColConstraintElem ConstraintAttr
417 %type <ival>    key_actions key_delete key_match key_update key_action
418 %type <ival>    ConstraintAttributeSpec ConstraintDeferrabilitySpec
419                                 ConstraintTimeSpec
420
421 %type <list>    constraints_set_list
422 %type <boolean> constraints_set_mode
423 %type <str>             OptTableSpace OptConsTableSpace OptTableSpaceOwner
424 %type <list>    opt_check_option
425
426 %type <target>  xml_attribute_el
427 %type <list>    xml_attribute_list xml_attributes
428 %type <node>    xml_root_version opt_xml_root_standalone
429 %type <node>    xmlexists_argument
430 %type <ival>    document_or_content
431 %type <boolean> xml_whitespace_option
432
433 %type <node>    common_table_expr
434 %type <with>    with_clause
435 %type <list>    cte_list
436
437 %type <list>    window_clause window_definition_list opt_partition_clause
438 %type <windef>  window_definition over_clause window_specification
439                                 opt_frame_clause frame_extent frame_bound
440 %type <str>             opt_existing_window_name
441
442
443 /*
444  * Non-keyword token types.  These are hard-wired into the "flex" lexer.
445  * They must be listed first so that their numeric codes do not depend on
446  * the set of keywords.  PL/pgsql depends on this so that it can share the
447  * same lexer.  If you add/change tokens here, fix PL/pgsql to match!
448  *
449  * DOT_DOT is unused in the core SQL grammar, and so will always provoke
450  * parse errors.  It is needed by PL/pgsql.
451  */
452 %token <str>    IDENT FCONST SCONST BCONST XCONST Op
453 %token <ival>   ICONST PARAM
454 %token                  TYPECAST DOT_DOT COLON_EQUALS
455
456 /*
457  * If you want to make any keyword changes, update the keyword table in
458  * src/include/parser/kwlist.h and add new keywords to the appropriate one
459  * of the reserved-or-not-so-reserved keyword lists, below; search
460  * this file for "Keyword category lists".
461  */
462
463 /* ordinary key words in alphabetical order */
464 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
465         AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
466         ASSERTION ASSIGNMENT ASYMMETRIC AT ATTRIBUTE AUTHORIZATION
467
468         BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
469         BOOLEAN_P BOTH BY
470
471         CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
472         CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
473         CLUSTER COALESCE COLLATE COLUMN COMMENT COMMENTS COMMIT
474         COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
475         CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
476         CREATEROLE CREATEUSER CROSS CSV CURRENT_P
477         CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
478         CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
479
480         DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
481         DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
482         DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
483
484         EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
485         EXCLUDE EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
486
487         FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
488         FREEZE FROM FULL FUNCTION FUNCTIONS
489
490         GLOBAL GRANT GRANTED GREATEST GROUP_P
491
492         HANDLER HAVING HEADER_P HOLD HOUR_P
493
494         IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
495         INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY INLINE_P
496         INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
497         INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
498
499         JOIN
500
501         KEY
502
503         LANGUAGE LARGE_P LAST_P LC_COLLATE_P LC_CTYPE_P LEADING
504         LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP
505         LOCATION LOCK_P LOGIN_P
506
507         MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
508
509         NAME_P NAMES NATIONAL NATURAL NCHAR NEXT NO NOCREATEDB
510         NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
511         NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
512
513         OBJECT_P OF OFF OFFSET OIDS ON ONLY OPERATOR OPTION OPTIONS OR
514         ORDER OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
515
516         PARSER PARTIAL PARTITION PASSING PASSWORD PLACING PLANS POSITION
517         PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
518         PRIOR PRIVILEGES PROCEDURAL PROCEDURE
519
520         QUOTE
521
522         RANGE READ REAL REASSIGN RECHECK RECURSIVE REF REFERENCES REINDEX
523         RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA RESET RESTART
524         RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
525
526         SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE SEQUENCES
527         SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
528         SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
529         STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
530         SYMMETRIC SYSID SYSTEM_P
531
532         TABLE TABLES TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
533         TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
534         TRUNCATE TRUSTED TYPE_P
535
536         UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
537         UPDATE USER USING
538
539         VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
540         VERBOSE VERSION_P VIEW VOLATILE
541
542         WHEN WHERE WHITESPACE_P WINDOW WITH WITHOUT WORK WRAPPER WRITE
543
544         XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLPARSE
545         XMLPI XMLROOT XMLSERIALIZE
546
547         YEAR_P YES_P
548
549         ZONE
550
551 /*
552  * The grammar thinks these are keywords, but they are not in the kwlist.h
553  * list and so can never be entered directly.  The filter in parser.c
554  * creates these tokens when required.
555  */
556 %token                  NULLS_FIRST NULLS_LAST WITH_TIME
557
558
559 /* Precedence: lowest to highest */
560 %nonassoc       SET                             /* see relation_expr_opt_alias */
561 %left           UNION EXCEPT
562 %left           INTERSECT
563 %left           OR
564 %left           AND
565 %right          NOT
566 %right          '='
567 %nonassoc       '<' '>'
568 %nonassoc       LIKE ILIKE SIMILAR
569 %nonassoc       ESCAPE
570 %nonassoc       OVERLAPS
571 %nonassoc       BETWEEN
572 %nonassoc       IN_P
573 %left           POSTFIXOP               /* dummy for postfix Op rules */
574 /*
575  * To support target_el without AS, we must give IDENT an explicit priority
576  * between POSTFIXOP and Op.  We can safely assign the same priority to
577  * various unreserved keywords as needed to resolve ambiguities (this can't
578  * have any bad effects since obviously the keywords will still behave the
579  * same as if they weren't keywords).  We need to do this for PARTITION,
580  * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
581  * so that they can follow a_expr without creating
582  * postfix-operator problems.
583  *
584  * The frame_bound productions UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING
585  * are even messier: since UNBOUNDED is an unreserved keyword (per spec!),
586  * there is no principled way to distinguish these from the productions
587  * a_expr PRECEDING/FOLLOWING.  We hack this up by giving UNBOUNDED slightly
588  * lower precedence than PRECEDING and FOLLOWING.  At present this doesn't
589  * appear to cause UNBOUNDED to be treated differently from other unreserved
590  * keywords anywhere else in the grammar, but it's definitely risky.  We can
591  * blame any funny behavior of UNBOUNDED on the SQL standard, though.
592  */
593 %nonassoc       UNBOUNDED               /* ideally should have same precedence as IDENT */
594 %nonassoc       IDENT PARTITION RANGE ROWS PRECEDING FOLLOWING
595 %left           Op OPERATOR             /* multi-character ops and user-defined operators */
596 %nonassoc       NOTNULL
597 %nonassoc       ISNULL
598 %nonassoc       IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
599 %left           '+' '-'
600 %left           '*' '/' '%'
601 %left           '^'
602 /* Unary Operators */
603 %left           AT ZONE                 /* sets precedence for AT TIME ZONE */
604 %right          UMINUS
605 %left           '[' ']'
606 %left           '(' ')'
607 %left           TYPECAST
608 %left           '.'
609 /*
610  * These might seem to be low-precedence, but actually they are not part
611  * of the arithmetic hierarchy at all in their use as JOIN operators.
612  * We make them high-precedence to support their use as function names.
613  * They wouldn't be given a precedence at all, were it not that we need
614  * left-associativity among the JOIN rules themselves.
615  */
616 %left           JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
617 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
618 %right          PRESERVE STRIP_P
619
620 %%
621
622 /*
623  *      The target production for the whole parse.
624  */
625 stmtblock:      stmtmulti
626                         {
627                                 pg_yyget_extra(yyscanner)->parsetree = $1;
628                         }
629                 ;
630
631 /* the thrashing around here is to discard "empty" statements... */
632 stmtmulti:      stmtmulti ';' stmt
633                                 {
634                                         if ($3 != NULL)
635                                                 $$ = lappend($1, $3);
636                                         else
637                                                 $$ = $1;
638                                 }
639                         | stmt
640                                 {
641                                         if ($1 != NULL)
642                                                 $$ = list_make1($1);
643                                         else
644                                                 $$ = NIL;
645                                 }
646                 ;
647
648 stmt :
649                         AlterDatabaseStmt
650                         | AlterDatabaseSetStmt
651                         | AlterDefaultPrivilegesStmt
652                         | AlterDomainStmt
653                         | AlterFdwStmt
654                         | AlterForeignServerStmt
655                         | AlterFunctionStmt
656                         | AlterGroupStmt
657                         | AlterObjectSchemaStmt
658                         | AlterOwnerStmt
659                         | AlterSeqStmt
660                         | AlterTableStmt
661                         | AlterCompositeTypeStmt
662                         | AlterRoleSetStmt
663                         | AlterRoleStmt
664                         | AlterTSConfigurationStmt
665                         | AlterTSDictionaryStmt
666                         | AlterUserMappingStmt
667                         | AlterUserSetStmt
668                         | AlterUserStmt
669                         | AnalyzeStmt
670                         | CheckPointStmt
671                         | ClosePortalStmt
672                         | ClusterStmt
673                         | CommentStmt
674                         | ConstraintsSetStmt
675                         | CopyStmt
676                         | CreateAsStmt
677                         | CreateAssertStmt
678                         | CreateCastStmt
679                         | CreateConversionStmt
680                         | CreateDomainStmt
681                         | CreateFdwStmt
682                         | CreateForeignServerStmt
683                         | CreateFunctionStmt
684                         | CreateGroupStmt
685                         | CreateOpClassStmt
686                         | CreateOpFamilyStmt
687                         | AlterOpFamilyStmt
688                         | CreatePLangStmt
689                         | CreateSchemaStmt
690                         | CreateSeqStmt
691                         | CreateStmt
692                         | CreateTableSpaceStmt
693                         | CreateTrigStmt
694                         | CreateRoleStmt
695                         | CreateUserStmt
696                         | CreateUserMappingStmt
697                         | CreatedbStmt
698                         | DeallocateStmt
699                         | DeclareCursorStmt
700                         | DefineStmt
701                         | DeleteStmt
702                         | DiscardStmt
703                         | DoStmt
704                         | DropAssertStmt
705                         | DropCastStmt
706                         | DropFdwStmt
707                         | DropForeignServerStmt
708                         | DropGroupStmt
709                         | DropOpClassStmt
710                         | DropOpFamilyStmt
711                         | DropOwnedStmt
712                         | DropPLangStmt
713                         | DropRuleStmt
714                         | DropStmt
715                         | DropTableSpaceStmt
716                         | DropTrigStmt
717                         | DropRoleStmt
718                         | DropUserStmt
719                         | DropUserMappingStmt
720                         | DropdbStmt
721                         | ExecuteStmt
722                         | ExplainStmt
723                         | FetchStmt
724                         | GrantStmt
725                         | GrantRoleStmt
726                         | IndexStmt
727                         | InsertStmt
728                         | ListenStmt
729                         | LoadStmt
730                         | LockStmt
731                         | NotifyStmt
732                         | PrepareStmt
733                         | ReassignOwnedStmt
734                         | ReindexStmt
735                         | RemoveAggrStmt
736                         | RemoveFuncStmt
737                         | RemoveOperStmt
738                         | RenameStmt
739                         | RevokeStmt
740                         | RevokeRoleStmt
741                         | RuleStmt
742                         | SelectStmt
743                         | TransactionStmt
744                         | TruncateStmt
745                         | UnlistenStmt
746                         | UpdateStmt
747                         | VacuumStmt
748                         | VariableResetStmt
749                         | VariableSetStmt
750                         | VariableShowStmt
751                         | ViewStmt
752                         | /*EMPTY*/
753                                 { $$ = NULL; }
754                 ;
755
756 /*****************************************************************************
757  *
758  * Create a new Postgres DBMS role
759  *
760  *****************************************************************************/
761
762 CreateRoleStmt:
763                         CREATE ROLE RoleId opt_with OptRoleList
764                                 {
765                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
766                                         n->stmt_type = ROLESTMT_ROLE;
767                                         n->role = $3;
768                                         n->options = $5;
769                                         $$ = (Node *)n;
770                                 }
771                 ;
772
773
774 opt_with:       WITH                                                                    {}
775                         | /*EMPTY*/                                                             {}
776                 ;
777
778 /*
779  * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
780  * for backwards compatibility).  Note: the only option required by SQL99
781  * is "WITH ADMIN name".
782  */
783 OptRoleList:
784                         OptRoleList CreateOptRoleElem                   { $$ = lappend($1, $2); }
785                         | /* EMPTY */                                                   { $$ = NIL; }
786                 ;
787
788 AlterOptRoleList:
789                         AlterOptRoleList AlterOptRoleElem               { $$ = lappend($1, $2); }
790                         | /* EMPTY */                                                   { $$ = NIL; }
791                 ;
792
793 AlterOptRoleElem:
794                         PASSWORD Sconst
795                                 {
796                                         $$ = makeDefElem("password",
797                                                                          (Node *)makeString($2));
798                                 }
799                         | PASSWORD NULL_P
800                                 {
801                                         $$ = makeDefElem("password", NULL);
802                                 }
803                         | ENCRYPTED PASSWORD Sconst
804                                 {
805                                         $$ = makeDefElem("encryptedPassword",
806                                                                          (Node *)makeString($3));
807                                 }
808                         | UNENCRYPTED PASSWORD Sconst
809                                 {
810                                         $$ = makeDefElem("unencryptedPassword",
811                                                                          (Node *)makeString($3));
812                                 }
813                         | SUPERUSER_P
814                                 {
815                                         $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
816                                 }
817                         | NOSUPERUSER
818                                 {
819                                         $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
820                                 }
821                         | INHERIT
822                                 {
823                                         $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
824                                 }
825                         | NOINHERIT
826                                 {
827                                         $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
828                                 }
829                         | CREATEDB
830                                 {
831                                         $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
832                                 }
833                         | NOCREATEDB
834                                 {
835                                         $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
836                                 }
837                         | CREATEROLE
838                                 {
839                                         $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
840                                 }
841                         | NOCREATEROLE
842                                 {
843                                         $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
844                                 }
845                         | CREATEUSER
846                                 {
847                                         /* For backwards compatibility, synonym for SUPERUSER */
848                                         $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
849                                 }
850                         | NOCREATEUSER
851                                 {
852                                         $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
853                                 }
854                         | LOGIN_P
855                                 {
856                                         $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
857                                 }
858                         | NOLOGIN_P
859                                 {
860                                         $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
861                                 }
862                         | CONNECTION LIMIT SignedIconst
863                                 {
864                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
865                                 }
866                         | VALID UNTIL Sconst
867                                 {
868                                         $$ = makeDefElem("validUntil", (Node *)makeString($3));
869                                 }
870                 /*      Supported but not documented for roles, for use by ALTER GROUP. */
871                         | USER name_list
872                                 {
873                                         $$ = makeDefElem("rolemembers", (Node *)$2);
874                                 }
875                 ;
876
877 CreateOptRoleElem:
878                         AlterOptRoleElem                        { $$ = $1; }
879                         /* The following are not supported by ALTER ROLE/USER/GROUP */
880                         | SYSID Iconst
881                                 {
882                                         $$ = makeDefElem("sysid", (Node *)makeInteger($2));
883                                 }
884                         | ADMIN name_list
885                                 {
886                                         $$ = makeDefElem("adminmembers", (Node *)$2);
887                                 }
888                         | ROLE name_list
889                                 {
890                                         $$ = makeDefElem("rolemembers", (Node *)$2);
891                                 }
892                         | IN_P ROLE name_list
893                                 {
894                                         $$ = makeDefElem("addroleto", (Node *)$3);
895                                 }
896                         | IN_P GROUP_P name_list
897                                 {
898                                         $$ = makeDefElem("addroleto", (Node *)$3);
899                                 }
900                 ;
901
902
903 /*****************************************************************************
904  *
905  * Create a new Postgres DBMS user (role with implied login ability)
906  *
907  *****************************************************************************/
908
909 CreateUserStmt:
910                         CREATE USER RoleId opt_with OptRoleList
911                                 {
912                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
913                                         n->stmt_type = ROLESTMT_USER;
914                                         n->role = $3;
915                                         n->options = $5;
916                                         $$ = (Node *)n;
917                                 }
918                 ;
919
920
921 /*****************************************************************************
922  *
923  * Alter a postgresql DBMS role
924  *
925  *****************************************************************************/
926
927 AlterRoleStmt:
928                         ALTER ROLE RoleId opt_with AlterOptRoleList
929                                  {
930                                         AlterRoleStmt *n = makeNode(AlterRoleStmt);
931                                         n->role = $3;
932                                         n->action = +1; /* add, if there are members */
933                                         n->options = $5;
934                                         $$ = (Node *)n;
935                                  }
936                 ;
937
938 opt_in_database:
939                            /* EMPTY */                                  { $$ = NULL; }
940                         | IN_P DATABASE database_name   { $$ = $3; }
941                 ;
942
943 AlterRoleSetStmt:
944                         ALTER ROLE RoleId opt_in_database SetResetClause
945                                 {
946                                         AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
947                                         n->role = $3;
948                                         n->database = $4;
949                                         n->setstmt = $5;
950                                         $$ = (Node *)n;
951                                 }
952                 ;
953
954
955 /*****************************************************************************
956  *
957  * Alter a postgresql DBMS user
958  *
959  *****************************************************************************/
960
961 AlterUserStmt:
962                         ALTER USER 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
973 AlterUserSetStmt:
974                         ALTER USER RoleId SetResetClause
975                                 {
976                                         AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
977                                         n->role = $3;
978                                         n->database = NULL;
979                                         n->setstmt = $4;
980                                         $$ = (Node *)n;
981                                 }
982                         ;
983
984
985 /*****************************************************************************
986  *
987  * Drop a postgresql DBMS role
988  *
989  * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
990  * might own objects in multiple databases, there is presently no way to
991  * implement either cascading or restricting.  Caveat DBA.
992  *****************************************************************************/
993
994 DropRoleStmt:
995                         DROP ROLE name_list
996                                 {
997                                         DropRoleStmt *n = makeNode(DropRoleStmt);
998                                         n->missing_ok = FALSE;
999                                         n->roles = $3;
1000                                         $$ = (Node *)n;
1001                                 }
1002                         | DROP ROLE IF_P EXISTS name_list
1003                                 {
1004                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1005                                         n->missing_ok = TRUE;
1006                                         n->roles = $5;
1007                                         $$ = (Node *)n;
1008                                 }
1009                         ;
1010
1011 /*****************************************************************************
1012  *
1013  * Drop a postgresql DBMS user
1014  *
1015  * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
1016  * might own objects in multiple databases, there is presently no way to
1017  * implement either cascading or restricting.  Caveat DBA.
1018  *****************************************************************************/
1019
1020 DropUserStmt:
1021                         DROP USER name_list
1022                                 {
1023                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1024                                         n->missing_ok = FALSE;
1025                                         n->roles = $3;
1026                                         $$ = (Node *)n;
1027                                 }
1028                         | DROP USER IF_P EXISTS name_list
1029                                 {
1030                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1031                                         n->roles = $5;
1032                                         n->missing_ok = TRUE;
1033                                         $$ = (Node *)n;
1034                                 }
1035                         ;
1036
1037
1038 /*****************************************************************************
1039  *
1040  * Create a postgresql group (role without login ability)
1041  *
1042  *****************************************************************************/
1043
1044 CreateGroupStmt:
1045                         CREATE GROUP_P RoleId opt_with OptRoleList
1046                                 {
1047                                         CreateRoleStmt *n = makeNode(CreateRoleStmt);
1048                                         n->stmt_type = ROLESTMT_GROUP;
1049                                         n->role = $3;
1050                                         n->options = $5;
1051                                         $$ = (Node *)n;
1052                                 }
1053                 ;
1054
1055
1056 /*****************************************************************************
1057  *
1058  * Alter a postgresql group
1059  *
1060  *****************************************************************************/
1061
1062 AlterGroupStmt:
1063                         ALTER GROUP_P RoleId add_drop USER name_list
1064                                 {
1065                                         AlterRoleStmt *n = makeNode(AlterRoleStmt);
1066                                         n->role = $3;
1067                                         n->action = $4;
1068                                         n->options = list_make1(makeDefElem("rolemembers",
1069                                                                                                                 (Node *)$6));
1070                                         $$ = (Node *)n;
1071                                 }
1072                 ;
1073
1074 add_drop:       ADD_P                                                                   { $$ = +1; }
1075                         | DROP                                                                  { $$ = -1; }
1076                 ;
1077
1078
1079 /*****************************************************************************
1080  *
1081  * Drop a postgresql group
1082  *
1083  * XXX see above notes about cascading DROP USER; groups have same problem.
1084  *****************************************************************************/
1085
1086 DropGroupStmt:
1087                         DROP GROUP_P name_list
1088                                 {
1089                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1090                                         n->missing_ok = FALSE;
1091                                         n->roles = $3;
1092                                         $$ = (Node *)n;
1093                                 }
1094                         | DROP GROUP_P IF_P EXISTS name_list
1095                                 {
1096                                         DropRoleStmt *n = makeNode(DropRoleStmt);
1097                                         n->missing_ok = TRUE;
1098                                         n->roles = $5;
1099                                         $$ = (Node *)n;
1100                                 }
1101                 ;
1102
1103
1104 /*****************************************************************************
1105  *
1106  * Manipulate a schema
1107  *
1108  *****************************************************************************/
1109
1110 CreateSchemaStmt:
1111                         CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1112                                 {
1113                                         CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1114                                         /* One can omit the schema name or the authorization id. */
1115                                         if ($3 != NULL)
1116                                                 n->schemaname = $3;
1117                                         else
1118                                                 n->schemaname = $5;
1119                                         n->authid = $5;
1120                                         n->schemaElts = $6;
1121                                         $$ = (Node *)n;
1122                                 }
1123                         | CREATE SCHEMA ColId OptSchemaEltList
1124                                 {
1125                                         CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1126                                         /* ...but not both */
1127                                         n->schemaname = $3;
1128                                         n->authid = NULL;
1129                                         n->schemaElts = $4;
1130                                         $$ = (Node *)n;
1131                                 }
1132                 ;
1133
1134 OptSchemaName:
1135                         ColId                                                                   { $$ = $1; }
1136                         | /* EMPTY */                                                   { $$ = NULL; }
1137                 ;
1138
1139 OptSchemaEltList:
1140                         OptSchemaEltList schema_stmt                    { $$ = lappend($1, $2); }
1141                         | /* EMPTY */                                                   { $$ = NIL; }
1142                 ;
1143
1144 /*
1145  *      schema_stmt are the ones that can show up inside a CREATE SCHEMA
1146  *      statement (in addition to by themselves).
1147  */
1148 schema_stmt:
1149                         CreateStmt
1150                         | IndexStmt
1151                         | CreateSeqStmt
1152                         | CreateTrigStmt
1153                         | GrantStmt
1154                         | ViewStmt
1155                 ;
1156
1157
1158 /*****************************************************************************
1159  *
1160  * Set PG internal variable
1161  *        SET name TO 'var_value'
1162  * Include SQL92 syntax (thomas 1997-10-22):
1163  *        SET TIME ZONE 'var_value'
1164  *
1165  *****************************************************************************/
1166
1167 VariableSetStmt:
1168                         SET set_rest
1169                                 {
1170                                         VariableSetStmt *n = $2;
1171                                         n->is_local = false;
1172                                         $$ = (Node *) n;
1173                                 }
1174                         | SET LOCAL set_rest
1175                                 {
1176                                         VariableSetStmt *n = $3;
1177                                         n->is_local = true;
1178                                         $$ = (Node *) n;
1179                                 }
1180                         | SET SESSION set_rest
1181                                 {
1182                                         VariableSetStmt *n = $3;
1183                                         n->is_local = false;
1184                                         $$ = (Node *) n;
1185                                 }
1186                 ;
1187
1188 set_rest:       /* Generic SET syntaxes: */
1189                         var_name TO var_list
1190                                 {
1191                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1192                                         n->kind = VAR_SET_VALUE;
1193                                         n->name = $1;
1194                                         n->args = $3;
1195                                         $$ = n;
1196                                 }
1197                         | var_name '=' var_list
1198                                 {
1199                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1200                                         n->kind = VAR_SET_VALUE;
1201                                         n->name = $1;
1202                                         n->args = $3;
1203                                         $$ = n;
1204                                 }
1205                         | var_name TO DEFAULT
1206                                 {
1207                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1208                                         n->kind = VAR_SET_DEFAULT;
1209                                         n->name = $1;
1210                                         $$ = n;
1211                                 }
1212                         | var_name '=' DEFAULT
1213                                 {
1214                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1215                                         n->kind = VAR_SET_DEFAULT;
1216                                         n->name = $1;
1217                                         $$ = n;
1218                                 }
1219                         | var_name FROM CURRENT_P
1220                                 {
1221                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1222                                         n->kind = VAR_SET_CURRENT;
1223                                         n->name = $1;
1224                                         $$ = n;
1225                                 }
1226                         /* Special syntaxes mandated by SQL standard: */
1227                         | TIME ZONE zone_value
1228                                 {
1229                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1230                                         n->kind = VAR_SET_VALUE;
1231                                         n->name = "timezone";
1232                                         if ($3 != NULL)
1233                                                 n->args = list_make1($3);
1234                                         else
1235                                                 n->kind = VAR_SET_DEFAULT;
1236                                         $$ = n;
1237                                 }
1238                         | TRANSACTION transaction_mode_list
1239                                 {
1240                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1241                                         n->kind = VAR_SET_MULTI;
1242                                         n->name = "TRANSACTION";
1243                                         n->args = $2;
1244                                         $$ = n;
1245                                 }
1246                         | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1247                                 {
1248                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1249                                         n->kind = VAR_SET_MULTI;
1250                                         n->name = "SESSION CHARACTERISTICS";
1251                                         n->args = $5;
1252                                         $$ = n;
1253                                 }
1254                         | CATALOG_P Sconst
1255                                 {
1256                                         ereport(ERROR,
1257                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1258                                                          errmsg("current database cannot be changed"),
1259                                                          parser_errposition(@2)));
1260                                         $$ = NULL; /*not reached*/
1261                                 }
1262                         | SCHEMA Sconst
1263                                 {
1264                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1265                                         n->kind = VAR_SET_VALUE;
1266                                         n->name = "search_path";
1267                                         n->args = list_make1(makeStringConst($2, @2));
1268                                         $$ = n;
1269                                 }
1270                         | NAMES opt_encoding
1271                                 {
1272                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1273                                         n->kind = VAR_SET_VALUE;
1274                                         n->name = "client_encoding";
1275                                         if ($2 != NULL)
1276                                                 n->args = list_make1(makeStringConst($2, @2));
1277                                         else
1278                                                 n->kind = VAR_SET_DEFAULT;
1279                                         $$ = n;
1280                                 }
1281                         | ROLE ColId_or_Sconst
1282                                 {
1283                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1284                                         n->kind = VAR_SET_VALUE;
1285                                         n->name = "role";
1286                                         n->args = list_make1(makeStringConst($2, @2));
1287                                         $$ = n;
1288                                 }
1289                         | SESSION AUTHORIZATION ColId_or_Sconst
1290                                 {
1291                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1292                                         n->kind = VAR_SET_VALUE;
1293                                         n->name = "session_authorization";
1294                                         n->args = list_make1(makeStringConst($3, @3));
1295                                         $$ = n;
1296                                 }
1297                         | SESSION AUTHORIZATION DEFAULT
1298                                 {
1299                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1300                                         n->kind = VAR_SET_DEFAULT;
1301                                         n->name = "session_authorization";
1302                                         $$ = n;
1303                                 }
1304                         | XML_P OPTION document_or_content
1305                                 {
1306                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1307                                         n->kind = VAR_SET_VALUE;
1308                                         n->name = "xmloption";
1309                                         n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1310                                         $$ = n;
1311                                 }
1312                 ;
1313
1314 var_name:       ColId                                                           { $$ = $1; }
1315                         | var_name '.' ColId
1316                                 {
1317                                         $$ = palloc(strlen($1) + strlen($3) + 2);
1318                                         sprintf($$, "%s.%s", $1, $3);
1319                                 }
1320                 ;
1321
1322 var_list:       var_value                                                               { $$ = list_make1($1); }
1323                         | var_list ',' var_value                                { $$ = lappend($1, $3); }
1324                 ;
1325
1326 var_value:      opt_boolean
1327                                 { $$ = makeStringConst($1, @1); }
1328                         | ColId_or_Sconst
1329                                 { $$ = makeStringConst($1, @1); }
1330                         | NumericOnly
1331                                 { $$ = makeAConst($1, @1); }
1332                 ;
1333
1334 iso_level:      READ UNCOMMITTED                                                { $$ = "read uncommitted"; }
1335                         | READ COMMITTED                                                { $$ = "read committed"; }
1336                         | REPEATABLE READ                                               { $$ = "repeatable read"; }
1337                         | SERIALIZABLE                                                  { $$ = "serializable"; }
1338                 ;
1339
1340 opt_boolean:
1341                         TRUE_P                                                                  { $$ = "true"; }
1342                         | FALSE_P                                                               { $$ = "false"; }
1343                         | ON                                                                    { $$ = "on"; }
1344                         | OFF                                                                   { $$ = "off"; }
1345                 ;
1346
1347 /* Timezone values can be:
1348  * - a string such as 'pst8pdt'
1349  * - an identifier such as "pst8pdt"
1350  * - an integer or floating point number
1351  * - a time interval per SQL99
1352  * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1353  * so use IDENT (meaning we reject anything that is a key word).
1354  */
1355 zone_value:
1356                         Sconst
1357                                 {
1358                                         $$ = makeStringConst($1, @1);
1359                                 }
1360                         | IDENT
1361                                 {
1362                                         $$ = makeStringConst($1, @1);
1363                                 }
1364                         | ConstInterval Sconst opt_interval
1365                                 {
1366                                         TypeName *t = $1;
1367                                         if ($3 != NIL)
1368                                         {
1369                                                 A_Const *n = (A_Const *) linitial($3);
1370                                                 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1371                                                         ereport(ERROR,
1372                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1373                                                                          errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1374                                                                          parser_errposition(@3)));
1375                                         }
1376                                         t->typmods = $3;
1377                                         $$ = makeStringConstCast($2, @2, t);
1378                                 }
1379                         | ConstInterval '(' Iconst ')' Sconst opt_interval
1380                                 {
1381                                         TypeName *t = $1;
1382                                         if ($6 != NIL)
1383                                         {
1384                                                 A_Const *n = (A_Const *) linitial($6);
1385                                                 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1386                                                         ereport(ERROR,
1387                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1388                                                                          errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1389                                                                          parser_errposition(@6)));
1390                                                 if (list_length($6) != 1)
1391                                                         ereport(ERROR,
1392                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
1393                                                                          errmsg("interval precision specified twice"),
1394                                                                          parser_errposition(@1)));
1395                                                 t->typmods = lappend($6, makeIntConst($3, @3));
1396                                         }
1397                                         else
1398                                                 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1399                                                                                                 makeIntConst($3, @3));
1400                                         $$ = makeStringConstCast($5, @5, t);
1401                                 }
1402                         | NumericOnly                                                   { $$ = makeAConst($1, @1); }
1403                         | DEFAULT                                                               { $$ = NULL; }
1404                         | LOCAL                                                                 { $$ = NULL; }
1405                 ;
1406
1407 opt_encoding:
1408                         Sconst                                                                  { $$ = $1; }
1409                         | DEFAULT                                                               { $$ = NULL; }
1410                         | /*EMPTY*/                                                             { $$ = NULL; }
1411                 ;
1412
1413 ColId_or_Sconst:
1414                         ColId                                                                   { $$ = $1; }
1415                         | Sconst                                                                { $$ = $1; }
1416                 ;
1417
1418 VariableResetStmt:
1419                         RESET var_name
1420                                 {
1421                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1422                                         n->kind = VAR_RESET;
1423                                         n->name = $2;
1424                                         $$ = (Node *) n;
1425                                 }
1426                         | RESET TIME ZONE
1427                                 {
1428                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1429                                         n->kind = VAR_RESET;
1430                                         n->name = "timezone";
1431                                         $$ = (Node *) n;
1432                                 }
1433                         | RESET TRANSACTION ISOLATION LEVEL
1434                                 {
1435                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1436                                         n->kind = VAR_RESET;
1437                                         n->name = "transaction_isolation";
1438                                         $$ = (Node *) n;
1439                                 }
1440                         | RESET SESSION AUTHORIZATION
1441                                 {
1442                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1443                                         n->kind = VAR_RESET;
1444                                         n->name = "session_authorization";
1445                                         $$ = (Node *) n;
1446                                 }
1447                         | RESET ALL
1448                                 {
1449                                         VariableSetStmt *n = makeNode(VariableSetStmt);
1450                                         n->kind = VAR_RESET_ALL;
1451                                         $$ = (Node *) n;
1452                                 }
1453                 ;
1454
1455 /* SetResetClause allows SET or RESET without LOCAL */
1456 SetResetClause:
1457                         SET set_rest                                    { $$ = $2; }
1458                         | VariableResetStmt                             { $$ = (VariableSetStmt *) $1; }
1459                 ;
1460
1461
1462 VariableShowStmt:
1463                         SHOW var_name
1464                                 {
1465                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1466                                         n->name = $2;
1467                                         $$ = (Node *) n;
1468                                 }
1469                         | SHOW TIME ZONE
1470                                 {
1471                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1472                                         n->name = "timezone";
1473                                         $$ = (Node *) n;
1474                                 }
1475                         | SHOW TRANSACTION ISOLATION LEVEL
1476                                 {
1477                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1478                                         n->name = "transaction_isolation";
1479                                         $$ = (Node *) n;
1480                                 }
1481                         | SHOW SESSION AUTHORIZATION
1482                                 {
1483                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1484                                         n->name = "session_authorization";
1485                                         $$ = (Node *) n;
1486                                 }
1487                         | SHOW ALL
1488                                 {
1489                                         VariableShowStmt *n = makeNode(VariableShowStmt);
1490                                         n->name = "all";
1491                                         $$ = (Node *) n;
1492                                 }
1493                 ;
1494
1495
1496 ConstraintsSetStmt:
1497                         SET CONSTRAINTS constraints_set_list constraints_set_mode
1498                                 {
1499                                         ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1500                                         n->constraints = $3;
1501                                         n->deferred    = $4;
1502                                         $$ = (Node *) n;
1503                                 }
1504                 ;
1505
1506 constraints_set_list:
1507                         ALL                                                                             { $$ = NIL; }
1508                         | qualified_name_list                                   { $$ = $1; }
1509                 ;
1510
1511 constraints_set_mode:
1512                         DEFERRED                                                                { $$ = TRUE; }
1513                         | IMMEDIATE                                                             { $$ = FALSE; }
1514                 ;
1515
1516
1517 /*
1518  * Checkpoint statement
1519  */
1520 CheckPointStmt:
1521                         CHECKPOINT
1522                                 {
1523                                         CheckPointStmt *n = makeNode(CheckPointStmt);
1524                                         $$ = (Node *)n;
1525                                 }
1526                 ;
1527
1528
1529 /*****************************************************************************
1530  *
1531  * DISCARD { ALL | TEMP | PLANS }
1532  *
1533  *****************************************************************************/
1534
1535 DiscardStmt:
1536                         DISCARD ALL
1537                                 {
1538                                         DiscardStmt *n = makeNode(DiscardStmt);
1539                                         n->target = DISCARD_ALL;
1540                                         $$ = (Node *) n;
1541                                 }
1542                         | DISCARD TEMP
1543                                 {
1544                                         DiscardStmt *n = makeNode(DiscardStmt);
1545                                         n->target = DISCARD_TEMP;
1546                                         $$ = (Node *) n;
1547                                 }
1548                         | DISCARD TEMPORARY
1549                                 {
1550                                         DiscardStmt *n = makeNode(DiscardStmt);
1551                                         n->target = DISCARD_TEMP;
1552                                         $$ = (Node *) n;
1553                                 }
1554                         | DISCARD PLANS
1555                                 {
1556                                         DiscardStmt *n = makeNode(DiscardStmt);
1557                                         n->target = DISCARD_PLANS;
1558                                         $$ = (Node *) n;
1559                                 }
1560                 ;
1561
1562
1563 /*****************************************************************************
1564  *
1565  *      ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1566  *
1567  * Note: we accept all subcommands for each of the four variants, and sort
1568  * out what's really legal at execution time.
1569  *****************************************************************************/
1570
1571 AlterTableStmt:
1572                         ALTER TABLE relation_expr alter_table_cmds
1573                                 {
1574                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1575                                         n->relation = $3;
1576                                         n->cmds = $4;
1577                                         n->relkind = OBJECT_TABLE;
1578                                         $$ = (Node *)n;
1579                                 }
1580                 |       ALTER INDEX qualified_name alter_table_cmds
1581                                 {
1582                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1583                                         n->relation = $3;
1584                                         n->cmds = $4;
1585                                         n->relkind = OBJECT_INDEX;
1586                                         $$ = (Node *)n;
1587                                 }
1588                 |       ALTER SEQUENCE qualified_name alter_table_cmds
1589                                 {
1590                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1591                                         n->relation = $3;
1592                                         n->cmds = $4;
1593                                         n->relkind = OBJECT_SEQUENCE;
1594                                         $$ = (Node *)n;
1595                                 }
1596                 |       ALTER VIEW qualified_name alter_table_cmds
1597                                 {
1598                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1599                                         n->relation = $3;
1600                                         n->cmds = $4;
1601                                         n->relkind = OBJECT_VIEW;
1602                                         $$ = (Node *)n;
1603                                 }
1604                 ;
1605
1606 alter_table_cmds:
1607                         alter_table_cmd                                                 { $$ = list_make1($1); }
1608                         | alter_table_cmds ',' alter_table_cmd  { $$ = lappend($1, $3); }
1609                 ;
1610
1611 alter_table_cmd:
1612                         /* ALTER TABLE <name> ADD <coldef> */
1613                         ADD_P columnDef
1614                                 {
1615                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1616                                         n->subtype = AT_AddColumn;
1617                                         n->def = $2;
1618                                         $$ = (Node *)n;
1619                                 }
1620                         /* ALTER TABLE <name> ADD COLUMN <coldef> */
1621                         | ADD_P COLUMN columnDef
1622                                 {
1623                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1624                                         n->subtype = AT_AddColumn;
1625                                         n->def = $3;
1626                                         $$ = (Node *)n;
1627                                 }
1628                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1629                         | ALTER opt_column ColId alter_column_default
1630                                 {
1631                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1632                                         n->subtype = AT_ColumnDefault;
1633                                         n->name = $3;
1634                                         n->def = $4;
1635                                         $$ = (Node *)n;
1636                                 }
1637                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1638                         | ALTER opt_column ColId DROP NOT NULL_P
1639                                 {
1640                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1641                                         n->subtype = AT_DropNotNull;
1642                                         n->name = $3;
1643                                         $$ = (Node *)n;
1644                                 }
1645                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1646                         | ALTER opt_column ColId SET NOT NULL_P
1647                                 {
1648                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1649                                         n->subtype = AT_SetNotNull;
1650                                         n->name = $3;
1651                                         $$ = (Node *)n;
1652                                 }
1653                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1654                         | ALTER opt_column ColId SET STATISTICS SignedIconst
1655                                 {
1656                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1657                                         n->subtype = AT_SetStatistics;
1658                                         n->name = $3;
1659                                         n->def = (Node *) makeInteger($6);
1660                                         $$ = (Node *)n;
1661                                 }
1662                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
1663                         | ALTER opt_column ColId SET reloptions
1664                                 {
1665                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1666                                         n->subtype = AT_SetOptions;
1667                                         n->name = $3;
1668                                         n->def = (Node *) $5;
1669                                         $$ = (Node *)n;
1670                                 }
1671                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET ( column_parameter = value [, ... ] ) */
1672                         | ALTER opt_column ColId RESET reloptions
1673                                 {
1674                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1675                                         n->subtype = AT_ResetOptions;
1676                                         n->name = $3;
1677                                         n->def = (Node *) $5;
1678                                         $$ = (Node *)n;
1679                                 }
1680                         /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1681                         | ALTER opt_column ColId SET STORAGE ColId
1682                                 {
1683                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1684                                         n->subtype = AT_SetStorage;
1685                                         n->name = $3;
1686                                         n->def = (Node *) makeString($6);
1687                                         $$ = (Node *)n;
1688                                 }
1689                         /* ALTER TABLE <name> DROP [COLUMN] IF EXISTS <colname> [RESTRICT|CASCADE] */
1690                         | DROP opt_column IF_P EXISTS ColId opt_drop_behavior
1691                                 {
1692                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1693                                         n->subtype = AT_DropColumn;
1694                                         n->name = $5;
1695                                         n->behavior = $6;
1696                                         n->missing_ok = TRUE;
1697                                         $$ = (Node *)n;
1698                                 }
1699                         /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1700                         | DROP opt_column ColId opt_drop_behavior
1701                                 {
1702                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1703                                         n->subtype = AT_DropColumn;
1704                                         n->name = $3;
1705                                         n->behavior = $4;
1706                                         n->missing_ok = FALSE;
1707                                         $$ = (Node *)n;
1708                                 }
1709                         /*
1710                          * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1711                          *              [ USING <expression> ]
1712                          */
1713                         | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1714                                 {
1715                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1716                                         n->subtype = AT_AlterColumnType;
1717                                         n->name = $3;
1718                                         n->def = (Node *) $6;
1719                                         n->transform = $7;
1720                                         $$ = (Node *)n;
1721                                 }
1722                         /* ALTER TABLE <name> ADD CONSTRAINT ... */
1723                         | ADD_P TableConstraint
1724                                 {
1725                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1726                                         n->subtype = AT_AddConstraint;
1727                                         n->def = $2;
1728                                         $$ = (Node *)n;
1729                                 }
1730                         /* ALTER TABLE <name> DROP CONSTRAINT IF EXISTS <name> [RESTRICT|CASCADE] */
1731                         | DROP CONSTRAINT IF_P EXISTS name opt_drop_behavior
1732                                 {
1733                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1734                                         n->subtype = AT_DropConstraint;
1735                                         n->name = $5;
1736                                         n->behavior = $6;
1737                                         n->missing_ok = TRUE;
1738                                         $$ = (Node *)n;
1739                                 }
1740                         /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1741                         | DROP CONSTRAINT name opt_drop_behavior
1742                                 {
1743                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1744                                         n->subtype = AT_DropConstraint;
1745                                         n->name = $3;
1746                                         n->behavior = $4;
1747                                         n->missing_ok = FALSE;
1748                                         $$ = (Node *)n;
1749                                 }
1750                         /* ALTER TABLE <name> SET WITH OIDS  */
1751                         | SET WITH OIDS
1752                                 {
1753                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1754                                         n->subtype = AT_AddOids;
1755                                         $$ = (Node *)n;
1756                                 }
1757                         /* ALTER TABLE <name> SET WITHOUT OIDS  */
1758                         | SET WITHOUT OIDS
1759                                 {
1760                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1761                                         n->subtype = AT_DropOids;
1762                                         $$ = (Node *)n;
1763                                 }
1764                         /* ALTER TABLE <name> CLUSTER ON <indexname> */
1765                         | CLUSTER ON name
1766                                 {
1767                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1768                                         n->subtype = AT_ClusterOn;
1769                                         n->name = $3;
1770                                         $$ = (Node *)n;
1771                                 }
1772                         /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1773                         | SET WITHOUT CLUSTER
1774                                 {
1775                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1776                                         n->subtype = AT_DropCluster;
1777                                         n->name = NULL;
1778                                         $$ = (Node *)n;
1779                                 }
1780                         /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1781                         | ENABLE_P TRIGGER name
1782                                 {
1783                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1784                                         n->subtype = AT_EnableTrig;
1785                                         n->name = $3;
1786                                         $$ = (Node *)n;
1787                                 }
1788                         /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1789                         | ENABLE_P ALWAYS TRIGGER name
1790                                 {
1791                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1792                                         n->subtype = AT_EnableAlwaysTrig;
1793                                         n->name = $4;
1794                                         $$ = (Node *)n;
1795                                 }
1796                         /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1797                         | ENABLE_P REPLICA TRIGGER name
1798                                 {
1799                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1800                                         n->subtype = AT_EnableReplicaTrig;
1801                                         n->name = $4;
1802                                         $$ = (Node *)n;
1803                                 }
1804                         /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1805                         | ENABLE_P TRIGGER ALL
1806                                 {
1807                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1808                                         n->subtype = AT_EnableTrigAll;
1809                                         $$ = (Node *)n;
1810                                 }
1811                         /* ALTER TABLE <name> ENABLE TRIGGER USER */
1812                         | ENABLE_P TRIGGER USER
1813                                 {
1814                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1815                                         n->subtype = AT_EnableTrigUser;
1816                                         $$ = (Node *)n;
1817                                 }
1818                         /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1819                         | DISABLE_P TRIGGER name
1820                                 {
1821                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1822                                         n->subtype = AT_DisableTrig;
1823                                         n->name = $3;
1824                                         $$ = (Node *)n;
1825                                 }
1826                         /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1827                         | DISABLE_P TRIGGER ALL
1828                                 {
1829                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1830                                         n->subtype = AT_DisableTrigAll;
1831                                         $$ = (Node *)n;
1832                                 }
1833                         /* ALTER TABLE <name> DISABLE TRIGGER USER */
1834                         | DISABLE_P TRIGGER USER
1835                                 {
1836                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1837                                         n->subtype = AT_DisableTrigUser;
1838                                         $$ = (Node *)n;
1839                                 }
1840                         /* ALTER TABLE <name> ENABLE RULE <rule> */
1841                         | ENABLE_P RULE name
1842                                 {
1843                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1844                                         n->subtype = AT_EnableRule;
1845                                         n->name = $3;
1846                                         $$ = (Node *)n;
1847                                 }
1848                         /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1849                         | ENABLE_P ALWAYS RULE name
1850                                 {
1851                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1852                                         n->subtype = AT_EnableAlwaysRule;
1853                                         n->name = $4;
1854                                         $$ = (Node *)n;
1855                                 }
1856                         /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1857                         | ENABLE_P REPLICA RULE name
1858                                 {
1859                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1860                                         n->subtype = AT_EnableReplicaRule;
1861                                         n->name = $4;
1862                                         $$ = (Node *)n;
1863                                 }
1864                         /* ALTER TABLE <name> DISABLE RULE <rule> */
1865                         | DISABLE_P RULE name
1866                                 {
1867                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1868                                         n->subtype = AT_DisableRule;
1869                                         n->name = $3;
1870                                         $$ = (Node *)n;
1871                                 }
1872                         /* ALTER TABLE <name> INHERIT <parent> */
1873                         | INHERIT qualified_name
1874                                 {
1875                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1876                                         n->subtype = AT_AddInherit;
1877                                         n->def = (Node *) $2;
1878                                         $$ = (Node *)n;
1879                                 }
1880                         /* ALTER TABLE <name> NO INHERIT <parent> */
1881                         | NO INHERIT qualified_name
1882                                 {
1883                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1884                                         n->subtype = AT_DropInherit;
1885                                         n->def = (Node *) $3;
1886                                         $$ = (Node *)n;
1887                                 }
1888                         /* ALTER TABLE <name> OWNER TO RoleId */
1889                         | OWNER TO RoleId
1890                                 {
1891                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1892                                         n->subtype = AT_ChangeOwner;
1893                                         n->name = $3;
1894                                         $$ = (Node *)n;
1895                                 }
1896                         /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1897                         | SET TABLESPACE name
1898                                 {
1899                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1900                                         n->subtype = AT_SetTableSpace;
1901                                         n->name = $3;
1902                                         $$ = (Node *)n;
1903                                 }
1904                         /* ALTER TABLE <name> SET (...) */
1905                         | SET reloptions
1906                                 {
1907                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1908                                         n->subtype = AT_SetRelOptions;
1909                                         n->def = (Node *)$2;
1910                                         $$ = (Node *)n;
1911                                 }
1912                         /* ALTER TABLE <name> RESET (...) */
1913                         | RESET reloptions
1914                                 {
1915                                         AlterTableCmd *n = makeNode(AlterTableCmd);
1916                                         n->subtype = AT_ResetRelOptions;
1917                                         n->def = (Node *)$2;
1918                                         $$ = (Node *)n;
1919                                 }
1920                 ;
1921
1922 alter_column_default:
1923                         SET DEFAULT a_expr                      { $$ = $3; }
1924                         | DROP DEFAULT                          { $$ = NULL; }
1925                 ;
1926
1927 opt_drop_behavior:
1928                         CASCADE                                         { $$ = DROP_CASCADE; }
1929                         | RESTRICT                                      { $$ = DROP_RESTRICT; }
1930                         | /* EMPTY */                           { $$ = DROP_RESTRICT; /* default */ }
1931                 ;
1932
1933 alter_using:
1934                         USING a_expr                            { $$ = $2; }
1935                         | /* EMPTY */                           { $$ = NULL; }
1936                 ;
1937
1938 reloptions:
1939                         '(' reloption_list ')'                                  { $$ = $2; }
1940                 ;
1941
1942 opt_reloptions:         WITH reloptions                                 { $$ = $2; }
1943                          |              /* EMPTY */                                             { $$ = NIL; }
1944                 ;
1945
1946 reloption_list:
1947                         reloption_elem                                                  { $$ = list_make1($1); }
1948                         | reloption_list ',' reloption_elem             { $$ = lappend($1, $3); }
1949                 ;
1950
1951 /* This should match def_elem and also allow qualified names */
1952 reloption_elem:
1953                         ColLabel '=' def_arg
1954                                 {
1955                                         $$ = makeDefElem($1, (Node *) $3);
1956                                 }
1957                         | ColLabel
1958                                 {
1959                                         $$ = makeDefElem($1, NULL);
1960                                 }
1961                         | ColLabel '.' ColLabel '=' def_arg
1962                                 {
1963                                         $$ = makeDefElemExtended($1, $3, (Node *) $5,
1964                                                                                          DEFELEM_UNSPEC);
1965                                 }
1966                         | ColLabel '.' ColLabel
1967                                 {
1968                                         $$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
1969                                 }
1970                 ;
1971
1972
1973 /*****************************************************************************
1974  *
1975  *      ALTER TYPE
1976  *
1977  * really variants of the ALTER TABLE subcommands with different spellings
1978  *****************************************************************************/
1979
1980 AlterCompositeTypeStmt:
1981                         ALTER TYPE_P any_name alter_type_cmds
1982                                 {
1983                                         AlterTableStmt *n = makeNode(AlterTableStmt);
1984
1985                                         /* can't use qualified_name, sigh */
1986                                         n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
1987                                         n->cmds = $4;
1988                                         n->relkind = OBJECT_TYPE;
1989                                         $$ = (Node *)n;
1990                                 }
1991                         ;
1992
1993 alter_type_cmds:
1994                         alter_type_cmd                                                  { $$ = list_make1($1); }
1995                         | alter_type_cmds ',' alter_type_cmd    { $$ = lappend($1, $3); }
1996                 ;
1997
1998 alter_type_cmd:
1999                         /* ALTER TYPE <name> ADD ATTRIBUTE <coldef> */
2000                         ADD_P ATTRIBUTE TableFuncElement
2001                                 {
2002                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2003                                         n->subtype = AT_AddColumn;
2004                                         n->def = $3;
2005                                         $$ = (Node *)n;
2006                                 }
2007                         /* ALTER TYPE <name> DROP ATTRIBUTE IF EXISTS <attname> */
2008                         | DROP ATTRIBUTE IF_P EXISTS ColId
2009                                 {
2010                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2011                                         n->subtype = AT_DropColumn;
2012                                         n->name = $5;
2013                                         n->behavior = DROP_RESTRICT; /* currently no effect */
2014                                         n->missing_ok = TRUE;
2015                                         $$ = (Node *)n;
2016                                 }
2017                         /* ALTER TYPE <name> DROP ATTRIBUTE <attname> */
2018                         | DROP ATTRIBUTE ColId opt_drop_behavior
2019                                 {
2020                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2021                                         n->subtype = AT_DropColumn;
2022                                         n->name = $3;
2023                                         n->behavior = DROP_RESTRICT; /* currently no effect */
2024                                         n->missing_ok = FALSE;
2025                                         $$ = (Node *)n;
2026                                 }
2027                         /* ALTER TYPE <name> ALTER ATTRIBUTE <attname> [SET DATA] TYPE <typename> */
2028                         | ALTER ATTRIBUTE ColId opt_set_data TYPE_P Typename
2029                                 {
2030                                         AlterTableCmd *n = makeNode(AlterTableCmd);
2031                                         n->subtype = AT_AlterColumnType;
2032                                         n->name = $3;
2033                                         n->def = (Node *) $6;
2034                                         $$ = (Node *)n;
2035                                 }
2036                 ;
2037
2038
2039 /*****************************************************************************
2040  *
2041  *              QUERY :
2042  *                              close <portalname>
2043  *
2044  *****************************************************************************/
2045
2046 ClosePortalStmt:
2047                         CLOSE cursor_name
2048                                 {
2049                                         ClosePortalStmt *n = makeNode(ClosePortalStmt);
2050                                         n->portalname = $2;
2051                                         $$ = (Node *)n;
2052                                 }
2053                         | CLOSE ALL
2054                                 {
2055                                         ClosePortalStmt *n = makeNode(ClosePortalStmt);
2056                                         n->portalname = NULL;
2057                                         $$ = (Node *)n;
2058                                 }
2059                 ;
2060
2061
2062 /*****************************************************************************
2063  *
2064  *              QUERY :
2065  *                              COPY relname [(columnList)] FROM/TO file [WITH] [(options)]
2066  *                              COPY ( SELECT ... ) TO file [WITH] [(options)]
2067  *
2068  *                              In the preferred syntax the options are comma-separated
2069  *                              and use generic identifiers instead of keywords.  The pre-9.0
2070  *                              syntax had a hard-wired, space-separated set of options.
2071  *
2072  *                              Really old syntax, from versions 7.2 and prior:
2073  *                              COPY [ BINARY ] table [ WITH OIDS ] FROM/TO file
2074  *                                      [ [ USING ] DELIMITERS 'delimiter' ] ]
2075  *                                      [ WITH NULL AS 'null string' ]
2076  *                              This option placement is not supported with COPY (SELECT...).
2077  *
2078  *****************************************************************************/
2079
2080 CopyStmt:       COPY opt_binary qualified_name opt_column_list opt_oids
2081                         copy_from copy_file_name copy_delimiter opt_with copy_options
2082                                 {
2083                                         CopyStmt *n = makeNode(CopyStmt);
2084                                         n->relation = $3;
2085                                         n->query = NULL;
2086                                         n->attlist = $4;
2087                                         n->is_from = $6;
2088                                         n->filename = $7;
2089
2090                                         n->options = NIL;
2091                                         /* Concatenate user-supplied flags */
2092                                         if ($2)
2093                                                 n->options = lappend(n->options, $2);
2094                                         if ($5)
2095                                                 n->options = lappend(n->options, $5);
2096                                         if ($8)
2097                                                 n->options = lappend(n->options, $8);
2098                                         if ($10)
2099                                                 n->options = list_concat(n->options, $10);
2100                                         $$ = (Node *)n;
2101                                 }
2102                         | COPY select_with_parens TO copy_file_name opt_with copy_options
2103                                 {
2104                                         CopyStmt *n = makeNode(CopyStmt);
2105                                         n->relation = NULL;
2106                                         n->query = $2;
2107                                         n->attlist = NIL;
2108                                         n->is_from = false;
2109                                         n->filename = $4;
2110                                         n->options = $6;
2111                                         $$ = (Node *)n;
2112                                 }
2113                 ;
2114
2115 copy_from:
2116                         FROM                                                                    { $$ = TRUE; }
2117                         | TO                                                                    { $$ = FALSE; }
2118                 ;
2119
2120 /*
2121  * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
2122  * used depends on the direction. (It really doesn't make sense to copy from
2123  * stdout. We silently correct the "typo".)              - AY 9/94
2124  */
2125 copy_file_name:
2126                         Sconst                                                                  { $$ = $1; }
2127                         | STDIN                                                                 { $$ = NULL; }
2128                         | STDOUT                                                                { $$ = NULL; }
2129                 ;
2130
2131 copy_options: copy_opt_list                                                     { $$ = $1; }
2132                         | '(' copy_generic_opt_list ')'                 { $$ = $2; }
2133                 ;
2134
2135 /* old COPY option syntax */
2136 copy_opt_list:
2137                         copy_opt_list copy_opt_item                             { $$ = lappend($1, $2); }
2138                         | /* EMPTY */                                                   { $$ = NIL; }
2139                 ;
2140
2141 copy_opt_item:
2142                         BINARY
2143                                 {
2144                                         $$ = makeDefElem("format", (Node *)makeString("binary"));
2145                                 }
2146                         | OIDS
2147                                 {
2148                                         $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2149                                 }
2150                         | DELIMITER opt_as Sconst
2151                                 {
2152                                         $$ = makeDefElem("delimiter", (Node *)makeString($3));
2153                                 }
2154                         | NULL_P opt_as Sconst
2155                                 {
2156                                         $$ = makeDefElem("null", (Node *)makeString($3));
2157                                 }
2158                         | CSV
2159                                 {
2160                                         $$ = makeDefElem("format", (Node *)makeString("csv"));
2161                                 }
2162                         | HEADER_P
2163                                 {
2164                                         $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
2165                                 }
2166                         | QUOTE opt_as Sconst
2167                                 {
2168                                         $$ = makeDefElem("quote", (Node *)makeString($3));
2169                                 }
2170                         | ESCAPE opt_as Sconst
2171                                 {
2172                                         $$ = makeDefElem("escape", (Node *)makeString($3));
2173                                 }
2174                         | FORCE QUOTE columnList
2175                                 {
2176                                         $$ = makeDefElem("force_quote", (Node *)$3);
2177                                 }
2178                         | FORCE QUOTE '*'
2179                                 {
2180                                         $$ = makeDefElem("force_quote", (Node *)makeNode(A_Star));
2181                                 }
2182                         | FORCE NOT NULL_P columnList
2183                                 {
2184                                         $$ = makeDefElem("force_not_null", (Node *)$4);
2185                                 }
2186                 ;
2187
2188 /* The following exist for backward compatibility with very old versions */
2189
2190 opt_binary:
2191                         BINARY
2192                                 {
2193                                         $$ = makeDefElem("format", (Node *)makeString("binary"));
2194                                 }
2195                         | /*EMPTY*/                                                             { $$ = NULL; }
2196                 ;
2197
2198 opt_oids:
2199                         WITH OIDS
2200                                 {
2201                                         $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2202                                 }
2203                         | /*EMPTY*/                                                             { $$ = NULL; }
2204                 ;
2205
2206 copy_delimiter:
2207                         opt_using DELIMITERS Sconst
2208                                 {
2209                                         $$ = makeDefElem("delimiter", (Node *)makeString($3));
2210                                 }
2211                         | /*EMPTY*/                                                             { $$ = NULL; }
2212                 ;
2213
2214 opt_using:
2215                         USING                                                                   {}
2216                         | /*EMPTY*/                                                             {}
2217                 ;
2218
2219 /* new COPY option syntax */
2220 copy_generic_opt_list:
2221                         copy_generic_opt_elem
2222                                 {
2223                                         $$ = list_make1($1);
2224                                 }
2225                         | copy_generic_opt_list ',' copy_generic_opt_elem
2226                                 {
2227                                         $$ = lappend($1, $3);
2228                                 }
2229                 ;
2230
2231 copy_generic_opt_elem:
2232                         ColLabel copy_generic_opt_arg
2233                                 {
2234                                         $$ = makeDefElem($1, $2);
2235                                 }
2236                 ;
2237
2238 copy_generic_opt_arg:
2239                         opt_boolean                                             { $$ = (Node *) makeString($1); }
2240                         | ColId_or_Sconst                               { $$ = (Node *) makeString($1); }
2241                         | NumericOnly                                   { $$ = (Node *) $1; }
2242                         | '*'                                                   { $$ = (Node *) makeNode(A_Star); }
2243                         | '(' copy_generic_opt_arg_list ')'             { $$ = (Node *) $2; }
2244                         | /* EMPTY */                                   { $$ = NULL; }
2245                 ;
2246
2247 copy_generic_opt_arg_list:
2248                           copy_generic_opt_arg_list_item
2249                                 {
2250                                         $$ = list_make1($1);
2251                                 }
2252                         | copy_generic_opt_arg_list ',' copy_generic_opt_arg_list_item
2253                                 {
2254                                         $$ = lappend($1, $3);
2255                                 }
2256                 ;
2257
2258 /* beware of emitting non-string list elements here; see commands/define.c */
2259 copy_generic_opt_arg_list_item:
2260                         opt_boolean                             { $$ = (Node *) makeString($1); }
2261                         | ColId_or_Sconst               { $$ = (Node *) makeString($1); }
2262                 ;
2263
2264
2265 /*****************************************************************************
2266  *
2267  *              QUERY :
2268  *                              CREATE TABLE relname
2269  *
2270  *****************************************************************************/
2271
2272 CreateStmt:     CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2273                         OptInherit OptWith OnCommitOption OptTableSpace
2274                                 {
2275                                         CreateStmt *n = makeNode(CreateStmt);
2276                                         $4->istemp = $2;
2277                                         n->relation = $4;
2278                                         n->tableElts = $6;
2279                                         n->inhRelations = $8;
2280                                         n->constraints = NIL;
2281                                         n->options = $9;
2282                                         n->oncommit = $10;
2283                                         n->tablespacename = $11;
2284                                         n->if_not_exists = false;
2285                                         $$ = (Node *)n;
2286                                 }
2287                 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name '('
2288                         OptTableElementList ')' OptInherit OptWith OnCommitOption
2289                         OptTableSpace
2290                                 {
2291                                         CreateStmt *n = makeNode(CreateStmt);
2292                                         $7->istemp = $2;
2293                                         n->relation = $7;
2294                                         n->tableElts = $9;
2295                                         n->inhRelations = $11;
2296                                         n->constraints = NIL;
2297                                         n->options = $12;
2298                                         n->oncommit = $13;
2299                                         n->tablespacename = $14;
2300                                         n->if_not_exists = true;
2301                                         $$ = (Node *)n;
2302                                 }
2303                 | CREATE OptTemp TABLE qualified_name OF any_name
2304                         OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2305                                 {
2306                                         CreateStmt *n = makeNode(CreateStmt);
2307                                         $4->istemp = $2;
2308                                         n->relation = $4;
2309                                         n->tableElts = $7;
2310                                         n->ofTypename = makeTypeNameFromNameList($6);
2311                                         n->ofTypename->location = @6;
2312                                         n->constraints = NIL;
2313                                         n->options = $8;
2314                                         n->oncommit = $9;
2315                                         n->tablespacename = $10;
2316                                         n->if_not_exists = false;
2317                                         $$ = (Node *)n;
2318                                 }
2319                 | CREATE OptTemp TABLE IF_P NOT EXISTS qualified_name OF any_name
2320                         OptTypedTableElementList OptWith OnCommitOption OptTableSpace
2321                                 {
2322                                         CreateStmt *n = makeNode(CreateStmt);
2323                                         $7->istemp = $2;
2324                                         n->relation = $7;
2325                                         n->tableElts = $10;
2326                                         n->ofTypename = makeTypeNameFromNameList($9);
2327                                         n->ofTypename->location = @9;
2328                                         n->constraints = NIL;
2329                                         n->options = $11;
2330                                         n->oncommit = $12;
2331                                         n->tablespacename = $13;
2332                                         n->if_not_exists = true;
2333                                         $$ = (Node *)n;
2334                                 }
2335                 ;
2336
2337 /*
2338  * Redundancy here is needed to avoid shift/reduce conflicts,
2339  * since TEMP is not a reserved word.  See also OptTempTableName.
2340  *
2341  * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2342  * the LOCAL keyword is really meaningless.
2343  */
2344 OptTemp:        TEMPORARY                                               { $$ = TRUE; }
2345                         | TEMP                                                  { $$ = TRUE; }
2346                         | LOCAL TEMPORARY                               { $$ = TRUE; }
2347                         | LOCAL TEMP                                    { $$ = TRUE; }
2348                         | GLOBAL TEMPORARY                              { $$ = TRUE; }
2349                         | GLOBAL TEMP                                   { $$ = TRUE; }
2350                         | /*EMPTY*/                                             { $$ = FALSE; }
2351                 ;
2352
2353 OptTableElementList:
2354                         TableElementList                                        { $$ = $1; }
2355                         | /*EMPTY*/                                                     { $$ = NIL; }
2356                 ;
2357
2358 OptTypedTableElementList:
2359                         '(' TypedTableElementList ')'           { $$ = $2; }
2360                         | /*EMPTY*/                                                     { $$ = NIL; }
2361                 ;
2362
2363 TableElementList:
2364                         TableElement
2365                                 {
2366                                         $$ = list_make1($1);
2367                                 }
2368                         | TableElementList ',' TableElement
2369                                 {
2370                                         $$ = lappend($1, $3);
2371                                 }
2372                 ;
2373
2374 TypedTableElementList:
2375                         TypedTableElement
2376                                 {
2377                                         $$ = list_make1($1);
2378                                 }
2379                         | TypedTableElementList ',' TypedTableElement
2380                                 {
2381                                         $$ = lappend($1, $3);
2382                                 }
2383                 ;
2384
2385 TableElement:
2386                         columnDef                                                       { $$ = $1; }
2387                         | TableLikeClause                                       { $$ = $1; }
2388                         | TableConstraint                                       { $$ = $1; }
2389                 ;
2390
2391 TypedTableElement:
2392                         columnOptions                                           { $$ = $1; }
2393                         | TableConstraint                                       { $$ = $1; }
2394                 ;
2395
2396 columnDef:      ColId Typename ColQualList
2397                                 {
2398                                         ColumnDef *n = makeNode(ColumnDef);
2399                                         n->colname = $1;
2400                                         n->typeName = $2;
2401                                         n->constraints = $3;
2402                                         n->is_local = true;
2403                                         $$ = (Node *)n;
2404                                 }
2405                 ;
2406
2407 columnOptions:  ColId WITH OPTIONS ColQualList
2408                                 {
2409                                         ColumnDef *n = makeNode(ColumnDef);
2410                                         n->colname = $1;
2411                                         n->constraints = $4;
2412                                         n->is_local = true;
2413                                         $$ = (Node *)n;
2414                                 }
2415                 ;
2416
2417 ColQualList:
2418                         ColQualList ColConstraint                               { $$ = lappend($1, $2); }
2419                         | /*EMPTY*/                                                             { $$ = NIL; }
2420                 ;
2421
2422 ColConstraint:
2423                         CONSTRAINT name ColConstraintElem
2424                                 {
2425                                         Constraint *n = (Constraint *) $3;
2426                                         Assert(IsA(n, Constraint));
2427                                         n->conname = $2;
2428                                         n->location = @1;
2429                                         $$ = (Node *) n;
2430                                 }
2431                         | ColConstraintElem                                             { $$ = $1; }
2432                         | ConstraintAttr                                                { $$ = $1; }
2433                 ;
2434
2435 /* DEFAULT NULL is already the default for Postgres.
2436  * But define it here and carry it forward into the system
2437  * to make it explicit.
2438  * - thomas 1998-09-13
2439  *
2440  * WITH NULL and NULL are not SQL92-standard syntax elements,
2441  * so leave them out. Use DEFAULT NULL to explicitly indicate
2442  * that a column may have that value. WITH NULL leads to
2443  * shift/reduce conflicts with WITH TIME ZONE anyway.
2444  * - thomas 1999-01-08
2445  *
2446  * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2447  * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2448  * or be part of a_expr NOT LIKE or similar constructs).
2449  */
2450 ColConstraintElem:
2451                         NOT NULL_P
2452                                 {
2453                                         Constraint *n = makeNode(Constraint);
2454                                         n->contype = CONSTR_NOTNULL;
2455                                         n->location = @1;
2456                                         $$ = (Node *)n;
2457                                 }
2458                         | NULL_P
2459                                 {
2460                                         Constraint *n = makeNode(Constraint);
2461                                         n->contype = CONSTR_NULL;
2462                                         n->location = @1;
2463                                         $$ = (Node *)n;
2464                                 }
2465                         | UNIQUE opt_definition OptConsTableSpace
2466                                 {
2467                                         Constraint *n = makeNode(Constraint);
2468                                         n->contype = CONSTR_UNIQUE;
2469                                         n->location = @1;
2470                                         n->keys = NULL;
2471                                         n->options = $2;
2472                                         n->indexspace = $3;
2473                                         $$ = (Node *)n;
2474                                 }
2475                         | PRIMARY KEY opt_definition OptConsTableSpace
2476                                 {
2477                                         Constraint *n = makeNode(Constraint);
2478                                         n->contype = CONSTR_PRIMARY;
2479                                         n->location = @1;
2480                                         n->keys = NULL;
2481                                         n->options = $3;
2482                                         n->indexspace = $4;
2483                                         $$ = (Node *)n;
2484                                 }
2485                         | CHECK '(' a_expr ')'
2486                                 {
2487                                         Constraint *n = makeNode(Constraint);
2488                                         n->contype = CONSTR_CHECK;
2489                                         n->location = @1;
2490                                         n->raw_expr = $3;
2491                                         n->cooked_expr = NULL;
2492                                         $$ = (Node *)n;
2493                                 }
2494                         | DEFAULT b_expr
2495                                 {
2496                                         Constraint *n = makeNode(Constraint);
2497                                         n->contype = CONSTR_DEFAULT;
2498                                         n->location = @1;
2499                                         n->raw_expr = $2;
2500                                         n->cooked_expr = NULL;
2501                                         $$ = (Node *)n;
2502                                 }
2503                         | REFERENCES qualified_name opt_column_list key_match key_actions
2504                                 {
2505                                         Constraint *n = makeNode(Constraint);
2506                                         n->contype = CONSTR_FOREIGN;
2507                                         n->location = @1;
2508                                         n->pktable                      = $2;
2509                                         n->fk_attrs                     = NIL;
2510                                         n->pk_attrs                     = $3;
2511                                         n->fk_matchtype         = $4;
2512                                         n->fk_upd_action        = (char) ($5 >> 8);
2513                                         n->fk_del_action        = (char) ($5 & 0xFF);
2514                                         n->skip_validation  = FALSE;
2515                                         $$ = (Node *)n;
2516                                 }
2517                 ;
2518
2519 /*
2520  * ConstraintAttr represents constraint attributes, which we parse as if
2521  * they were independent constraint clauses, in order to avoid shift/reduce
2522  * conflicts (since NOT might start either an independent NOT NULL clause
2523  * or an attribute).  parse_utilcmd.c is responsible for attaching the
2524  * attribute information to the preceding "real" constraint node, and for
2525  * complaining if attribute clauses appear in the wrong place or wrong
2526  * combinations.
2527  *
2528  * See also ConstraintAttributeSpec, which can be used in places where
2529  * there is no parsing conflict.
2530  */
2531 ConstraintAttr:
2532                         DEFERRABLE
2533                                 {
2534                                         Constraint *n = makeNode(Constraint);
2535                                         n->contype = CONSTR_ATTR_DEFERRABLE;
2536                                         n->location = @1;
2537                                         $$ = (Node *)n;
2538                                 }
2539                         | NOT DEFERRABLE
2540                                 {
2541                                         Constraint *n = makeNode(Constraint);
2542                                         n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2543                                         n->location = @1;
2544                                         $$ = (Node *)n;
2545                                 }
2546                         | INITIALLY DEFERRED
2547                                 {
2548                                         Constraint *n = makeNode(Constraint);
2549                                         n->contype = CONSTR_ATTR_DEFERRED;
2550                                         n->location = @1;
2551                                         $$ = (Node *)n;
2552                                 }
2553                         | INITIALLY IMMEDIATE
2554                                 {
2555                                         Constraint *n = makeNode(Constraint);
2556                                         n->contype = CONSTR_ATTR_IMMEDIATE;
2557                                         n->location = @1;
2558                                         $$ = (Node *)n;
2559                                 }
2560                 ;
2561
2562
2563 /*
2564  * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2565  * This seems to be a poor man's inheritance capability, with the resulting
2566  * tables completely decoupled except for the original commonality in definitions.
2567  *
2568  * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2569  * which is a part of SQL:2003.
2570  */
2571 TableLikeClause:
2572                         LIKE qualified_name TableLikeOptionList
2573                                 {
2574                                         InhRelation *n = makeNode(InhRelation);
2575                                         n->relation = $2;
2576                                         n->options = $3;
2577                                         $$ = (Node *)n;
2578                                 }
2579                 ;
2580
2581 TableLikeOptionList:
2582                                 TableLikeOptionList INCLUDING TableLikeOption   { $$ = $1 | $3; }
2583                                 | TableLikeOptionList EXCLUDING TableLikeOption { $$ = $1 & ~$3; }
2584                                 | /* EMPTY */                                           { $$ = 0; }
2585                 ;
2586
2587 TableLikeOption:
2588                                 DEFAULTS                        { $$ = CREATE_TABLE_LIKE_DEFAULTS; }
2589                                 | CONSTRAINTS           { $$ = CREATE_TABLE_LIKE_CONSTRAINTS; }
2590                                 | INDEXES                       { $$ = CREATE_TABLE_LIKE_INDEXES; }
2591                                 | STORAGE                       { $$ = CREATE_TABLE_LIKE_STORAGE; }
2592                                 | COMMENTS                      { $$ = CREATE_TABLE_LIKE_COMMENTS; }
2593                                 | ALL                           { $$ = CREATE_TABLE_LIKE_ALL; }
2594                 ;
2595
2596
2597 /* ConstraintElem specifies constraint syntax which is not embedded into
2598  *      a column definition. ColConstraintElem specifies the embedded form.
2599  * - thomas 1997-12-03
2600  */
2601 TableConstraint:
2602                         CONSTRAINT name ConstraintElem
2603                                 {
2604                                         Constraint *n = (Constraint *) $3;
2605                                         Assert(IsA(n, Constraint));
2606                                         n->conname = $2;
2607                                         n->location = @1;
2608                                         $$ = (Node *) n;
2609                                 }
2610                         | ConstraintElem                                                { $$ = $1; }
2611                 ;
2612
2613 ConstraintElem:
2614                         CHECK '(' a_expr ')' ConstraintAttributeSpec
2615                                 {
2616                                         Constraint *n = makeNode(Constraint);
2617                                         n->contype = CONSTR_CHECK;
2618                                         n->location = @1;
2619                                         n->raw_expr = $3;
2620                                         n->cooked_expr = NULL;
2621                                         if ($5 != 0)
2622                                                 ereport(ERROR,
2623                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2624                                                                  errmsg("CHECK constraints cannot be deferred"),
2625                                                                  parser_errposition(@5)));
2626                                         $$ = (Node *)n;
2627                                 }
2628                         | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2629                                 ConstraintAttributeSpec
2630                                 {
2631                                         Constraint *n = makeNode(Constraint);
2632                                         n->contype = CONSTR_UNIQUE;
2633                                         n->location = @1;
2634                                         n->keys = $3;
2635                                         n->options = $5;
2636                                         n->indexspace = $6;
2637                                         n->deferrable = ($7 & 1) != 0;
2638                                         n->initdeferred = ($7 & 2) != 0;
2639                                         $$ = (Node *)n;
2640                                 }
2641                         | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2642                                 ConstraintAttributeSpec
2643                                 {
2644                                         Constraint *n = makeNode(Constraint);
2645                                         n->contype = CONSTR_PRIMARY;
2646                                         n->location = @1;
2647                                         n->keys = $4;
2648                                         n->options = $6;
2649                                         n->indexspace = $7;
2650                                         n->deferrable = ($8 & 1) != 0;
2651                                         n->initdeferred = ($8 & 2) != 0;
2652                                         $$ = (Node *)n;
2653                                 }
2654                         | EXCLUDE access_method_clause '(' ExclusionConstraintList ')'
2655                                 opt_definition OptConsTableSpace ExclusionWhereClause
2656                                 ConstraintAttributeSpec
2657                                 {
2658                                         Constraint *n = makeNode(Constraint);
2659                                         n->contype = CONSTR_EXCLUSION;
2660                                         n->location = @1;
2661                                         n->access_method        = $2;
2662                                         n->exclusions           = $4;
2663                                         n->options                      = $6;
2664                                         n->indexspace           = $7;
2665                                         n->where_clause         = $8;
2666                                         n->deferrable           = ($9 & 1) != 0;
2667                                         n->initdeferred         = ($9 & 2) != 0;
2668                                         $$ = (Node *)n;
2669                                 }
2670                         | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2671                                 opt_column_list key_match key_actions ConstraintAttributeSpec
2672                                 {
2673                                         Constraint *n = makeNode(Constraint);
2674                                         n->contype = CONSTR_FOREIGN;
2675                                         n->location = @1;
2676                                         n->pktable                      = $7;
2677                                         n->fk_attrs                     = $4;
2678                                         n->pk_attrs                     = $8;
2679                                         n->fk_matchtype         = $9;
2680                                         n->fk_upd_action        = (char) ($10 >> 8);
2681                                         n->fk_del_action        = (char) ($10 & 0xFF);
2682                                         n->skip_validation  = FALSE;
2683                                         n->deferrable           = ($11 & 1) != 0;
2684                                         n->initdeferred         = ($11 & 2) != 0;
2685                                         $$ = (Node *)n;
2686                                 }
2687                 ;
2688
2689 opt_column_list:
2690                         '(' columnList ')'                                              { $$ = $2; }
2691                         | /*EMPTY*/                                                             { $$ = NIL; }
2692                 ;
2693
2694 columnList:
2695                         columnElem                                                              { $$ = list_make1($1); }
2696                         | columnList ',' columnElem                             { $$ = lappend($1, $3); }
2697                 ;
2698
2699 columnElem: ColId
2700                                 {
2701                                         $$ = (Node *) makeString($1);
2702                                 }
2703                 ;
2704
2705 key_match:  MATCH FULL
2706                         {
2707                                 $$ = FKCONSTR_MATCH_FULL;
2708                         }
2709                 | MATCH PARTIAL
2710                         {
2711                                 ereport(ERROR,
2712                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2713                                                  errmsg("MATCH PARTIAL not yet implemented"),
2714                                                  parser_errposition(@1)));
2715                                 $$ = FKCONSTR_MATCH_PARTIAL;
2716                         }
2717                 | MATCH SIMPLE
2718                         {
2719                                 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2720                         }
2721                 | /*EMPTY*/
2722                         {
2723                                 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2724                         }
2725                 ;
2726
2727 ExclusionConstraintList:
2728                         ExclusionConstraintElem                                 { $$ = list_make1($1); }
2729                         | ExclusionConstraintList ',' ExclusionConstraintElem
2730                                                                                                         { $$ = lappend($1, $3); }
2731                 ;
2732
2733 ExclusionConstraintElem: index_elem WITH any_operator
2734                         {
2735                                 $$ = list_make2($1, $3);
2736                         }
2737                         /* allow OPERATOR() decoration for the benefit of ruleutils.c */
2738                         | index_elem WITH OPERATOR '(' any_operator ')'
2739                         {
2740                                 $$ = list_make2($1, $5);
2741                         }
2742                 ;
2743
2744 ExclusionWhereClause:
2745                         WHERE '(' a_expr ')'                                    { $$ = $3; }
2746                         | /*EMPTY*/                                                             { $$ = NULL; }
2747                 ;
2748
2749 /*
2750  * We combine the update and delete actions into one value temporarily
2751  * for simplicity of parsing, and then break them down again in the
2752  * calling production.  update is in the left 8 bits, delete in the right.
2753  * Note that NOACTION is the default.
2754  */
2755 key_actions:
2756                         key_update
2757                                 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2758                         | key_delete
2759                                 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2760                         | key_update key_delete
2761                                 { $$ = ($1 << 8) | ($2 & 0xFF); }
2762                         | key_delete key_update
2763                                 { $$ = ($2 << 8) | ($1 & 0xFF); }
2764                         | /*EMPTY*/
2765                                 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2766                 ;
2767
2768 key_update: ON UPDATE key_action                { $$ = $3; }
2769                 ;
2770
2771 key_delete: ON DELETE_P key_action              { $$ = $3; }
2772                 ;
2773
2774 key_action:
2775                         NO ACTION                                       { $$ = FKCONSTR_ACTION_NOACTION; }
2776                         | RESTRICT                                      { $$ = FKCONSTR_ACTION_RESTRICT; }
2777                         | CASCADE                                       { $$ = FKCONSTR_ACTION_CASCADE; }
2778                         | SET NULL_P                            { $$ = FKCONSTR_ACTION_SETNULL; }
2779                         | SET DEFAULT                           { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2780                 ;
2781
2782 OptInherit: INHERITS '(' qualified_name_list ')'        { $$ = $3; }
2783                         | /*EMPTY*/                                                             { $$ = NIL; }
2784                 ;
2785
2786 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2787 OptWith:
2788                         WITH reloptions                         { $$ = $2; }
2789                         | WITH OIDS                                     { $$ = list_make1(defWithOids(true)); }
2790                         | WITHOUT OIDS                          { $$ = list_make1(defWithOids(false)); }
2791                         | /*EMPTY*/                                     { $$ = NIL; }
2792                 ;
2793
2794 OnCommitOption:  ON COMMIT DROP                         { $$ = ONCOMMIT_DROP; }
2795                         | ON COMMIT DELETE_P ROWS               { $$ = ONCOMMIT_DELETE_ROWS; }
2796                         | ON COMMIT PRESERVE ROWS               { $$ = ONCOMMIT_PRESERVE_ROWS; }
2797                         | /*EMPTY*/                                             { $$ = ONCOMMIT_NOOP; }
2798                 ;
2799
2800 OptTableSpace:   TABLESPACE name                                        { $$ = $2; }
2801                         | /*EMPTY*/                                                             { $$ = NULL; }
2802                 ;
2803
2804 OptConsTableSpace:   USING INDEX TABLESPACE name        { $$ = $4; }
2805                         | /*EMPTY*/                                                             { $$ = NULL; }
2806                 ;
2807
2808
2809 /*
2810  * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2811  * SELECT ... INTO.
2812  */
2813
2814 CreateAsStmt:
2815                 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2816                                 {
2817                                         /*
2818                                          * When the SelectStmt is a set-operation tree, we must
2819                                          * stuff the INTO information into the leftmost component
2820                                          * Select, because that's where analyze.c will expect
2821                                          * to find it.  Similarly, the output column names must
2822                                          * be attached to that Select's target list.
2823                                          */
2824                                         SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2825                                         if (n->intoClause != NULL)
2826                                                 ereport(ERROR,
2827                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2828                                                                  errmsg("CREATE TABLE AS cannot specify INTO"),
2829                                                                  parser_errposition(exprLocation((Node *) n->intoClause))));
2830                                         $4->rel->istemp = $2;
2831                                         n->intoClause = $4;
2832                                         /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2833                                         if (!$7)
2834                                                 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2835                                         $$ = $6;
2836                                 }
2837                 ;
2838
2839 create_as_target:
2840                         qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2841                                 {
2842                                         $$ = makeNode(IntoClause);
2843                                         $$->rel = $1;
2844                                         $$->colNames = $2;
2845                                         $$->options = $3;
2846                                         $$->onCommit = $4;
2847                                         $$->tableSpaceName = $5;
2848                                 }
2849                 ;
2850
2851 OptCreateAs:
2852                         '(' CreateAsList ')'                                    { $$ = $2; }
2853                         | /*EMPTY*/                                                             { $$ = NIL; }
2854                 ;
2855
2856 CreateAsList:
2857                         CreateAsElement                                                 { $$ = list_make1($1); }
2858                         | CreateAsList ',' CreateAsElement              { $$ = lappend($1, $3); }
2859                 ;
2860
2861 CreateAsElement:
2862                         ColId
2863                                 {
2864                                         ColumnDef *n = makeNode(ColumnDef);
2865                                         n->colname = $1;
2866                                         n->typeName = NULL;
2867                                         n->inhcount = 0;
2868                                         n->is_local = true;
2869                                         n->is_not_null = false;
2870                                         n->raw_default = NULL;
2871                                         n->cooked_default = NULL;
2872                                         n->constraints = NIL;
2873                                         $$ = (Node *)n;
2874                                 }
2875                 ;
2876
2877 opt_with_data:
2878                         WITH DATA_P                                                             { $$ = TRUE; }
2879                         | WITH NO DATA_P                                                { $$ = FALSE; }
2880                         | /*EMPTY*/                                                             { $$ = TRUE; }
2881                 ;
2882
2883
2884 /*****************************************************************************
2885  *
2886  *              QUERY :
2887  *                              CREATE SEQUENCE seqname
2888  *                              ALTER SEQUENCE seqname
2889  *
2890  *****************************************************************************/
2891
2892 CreateSeqStmt:
2893                         CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2894                                 {
2895                                         CreateSeqStmt *n = makeNode(CreateSeqStmt);
2896                                         $4->istemp = $2;
2897                                         n->sequence = $4;
2898                                         n->options = $5;
2899                                         n->ownerId = InvalidOid;
2900                                         $$ = (Node *)n;
2901                                 }
2902                 ;
2903
2904 AlterSeqStmt:
2905                         ALTER SEQUENCE qualified_name SeqOptList
2906                                 {
2907                                         AlterSeqStmt *n = makeNode(AlterSeqStmt);
2908                                         n->sequence = $3;
2909                                         n->options = $4;
2910                                         $$ = (Node *)n;
2911                                 }
2912                 ;
2913
2914 OptSeqOptList: SeqOptList                                                       { $$ = $1; }
2915                         | /*EMPTY*/                                                             { $$ = NIL; }
2916                 ;
2917
2918 SeqOptList: SeqOptElem                                                          { $$ = list_make1($1); }
2919                         | SeqOptList SeqOptElem                                 { $$ = lappend($1, $2); }
2920                 ;
2921
2922 SeqOptElem: CACHE NumericOnly
2923                                 {
2924                                         $$ = makeDefElem("cache", (Node *)$2);
2925                                 }
2926                         | CYCLE
2927                                 {
2928                                         $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2929                                 }
2930                         | NO CYCLE
2931                                 {
2932                                         $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2933                                 }
2934                         | INCREMENT opt_by NumericOnly
2935                                 {
2936                                         $$ = makeDefElem("increment", (Node *)$3);
2937                                 }
2938                         | MAXVALUE NumericOnly
2939                                 {
2940                                         $$ = makeDefElem("maxvalue", (Node *)$2);
2941                                 }
2942                         | MINVALUE NumericOnly
2943                                 {
2944                                         $$ = makeDefElem("minvalue", (Node *)$2);
2945                                 }
2946                         | NO MAXVALUE
2947                                 {
2948                                         $$ = makeDefElem("maxvalue", NULL);
2949                                 }
2950                         | NO MINVALUE
2951                                 {
2952                                         $$ = makeDefElem("minvalue", NULL);
2953                                 }
2954                         | OWNED BY any_name
2955                                 {
2956                                         $$ = makeDefElem("owned_by", (Node *)$3);
2957                                 }
2958                         | START opt_with NumericOnly
2959                                 {
2960                                         $$ = makeDefElem("start", (Node *)$3);
2961                                 }
2962                         | RESTART
2963                                 {
2964                                         $$ = makeDefElem("restart", NULL);
2965                                 }
2966                         | RESTART opt_with NumericOnly
2967                                 {
2968                                         $$ = makeDefElem("restart", (Node *)$3);
2969                                 }
2970                 ;
2971
2972 opt_by:         BY                              {}
2973                         | /* empty */   {}
2974           ;
2975
2976 NumericOnly:
2977                         FCONST                                                          { $$ = makeFloat($1); }
2978                         | '-' FCONST
2979                                 {
2980                                         $$ = makeFloat($2);
2981                                         doNegateFloat($$);
2982                                 }
2983                         | SignedIconst                                          { $$ = makeInteger($1); }
2984                 ;
2985
2986 NumericOnly_list:       NumericOnly                                             { $$ = list_make1($1); }
2987                                 | NumericOnly_list ',' NumericOnly      { $$ = lappend($1, $3); }
2988                 ;
2989
2990 /*****************************************************************************
2991  *
2992  *              QUERIES :
2993  *                              CREATE [OR REPLACE] [TRUSTED] [PROCEDURAL] LANGUAGE ...
2994  *                              DROP [PROCEDURAL] LANGUAGE ...
2995  *
2996  *****************************************************************************/
2997
2998 CreatePLangStmt:
2999                         CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
3000                         {
3001                                 CreatePLangStmt *n = makeNode(CreatePLangStmt);
3002                                 n->replace = $2;
3003                                 n->plname = $6;
3004                                 /* parameters are all to be supplied by system */
3005                                 n->plhandler = NIL;
3006                                 n->plinline = NIL;
3007                                 n->plvalidator = NIL;
3008                                 n->pltrusted = false;
3009                                 $$ = (Node *)n;
3010                         }
3011                         | CREATE opt_or_replace opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
3012                           HANDLER handler_name opt_inline_handler opt_validator
3013                         {
3014                                 CreatePLangStmt *n = makeNode(CreatePLangStmt);
3015                                 n->replace = $2;
3016                                 n->plname = $6;
3017                                 n->plhandler = $8;
3018                                 n->plinline = $9;
3019                                 n->plvalidator = $10;
3020                                 n->pltrusted = $3;
3021                                 $$ = (Node *)n;
3022                         }
3023                 ;
3024
3025 opt_trusted:
3026                         TRUSTED                                                                 { $$ = TRUE; }
3027                         | /*EMPTY*/                                                             { $$ = FALSE; }
3028                 ;
3029
3030 /* This ought to be just func_name, but that causes reduce/reduce conflicts
3031  * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
3032  * Work around by using simple names, instead.
3033  */
3034 handler_name:
3035                         name                                            { $$ = list_make1(makeString($1)); }
3036                         | name attrs                            { $$ = lcons(makeString($1), $2); }
3037                 ;
3038
3039 opt_inline_handler:
3040                         INLINE_P handler_name                                   { $$ = $2; }
3041                         | /*EMPTY*/                                                             { $$ = NIL; }
3042                 ;
3043
3044 validator_clause:
3045                         VALIDATOR handler_name                                  { $$ = $2; }
3046                         | NO VALIDATOR                                                  { $$ = NIL; }
3047                 ;
3048
3049 opt_validator:
3050                         validator_clause                                                { $$ = $1; }
3051                         | /*EMPTY*/                                                             { $$ = NIL; }
3052                 ;
3053
3054 DropPLangStmt:
3055                         DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
3056                                 {
3057                                         DropPLangStmt *n = makeNode(DropPLangStmt);
3058                                         n->plname = $4;
3059                                         n->behavior = $5;
3060                                         n->missing_ok = false;
3061                                         $$ = (Node *)n;
3062                                 }
3063                         | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
3064                                 {
3065                                         DropPLangStmt *n = makeNode(DropPLangStmt);
3066                                         n->plname = $6;
3067                                         n->behavior = $7;
3068                                         n->missing_ok = true;
3069                                         $$ = (Node *)n;
3070                                 }
3071                 ;
3072
3073 opt_procedural:
3074                         PROCEDURAL                                                              {}
3075                         | /*EMPTY*/                                                             {}
3076                 ;
3077
3078 /*****************************************************************************
3079  *
3080  *              QUERY:
3081  *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
3082  *
3083  *****************************************************************************/
3084
3085 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
3086                                 {
3087                                         CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
3088                                         n->tablespacename = $3;
3089                                         n->owner = $4;
3090                                         n->location = $6;
3091                                         $$ = (Node *) n;
3092                                 }
3093                 ;
3094
3095 OptTableSpaceOwner: OWNER name                  { $$ = $2; }
3096                         | /*EMPTY */                            { $$ = NULL; }
3097                 ;
3098
3099 /*****************************************************************************
3100  *
3101  *              QUERY :
3102  *                              DROP TABLESPACE <tablespace>
3103  *
3104  *              No need for drop behaviour as we cannot implement dependencies for
3105  *              objects in other databases; we can only support RESTRICT.
3106  *
3107  ****************************************************************************/
3108
3109 DropTableSpaceStmt: DROP TABLESPACE name
3110                                 {
3111                                         DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3112                                         n->tablespacename = $3;
3113                                         n->missing_ok = false;
3114                                         $$ = (Node *) n;
3115                                 }
3116                                 |  DROP TABLESPACE IF_P EXISTS name
3117                 {
3118                                         DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
3119                                         n->tablespacename = $5;
3120                                         n->missing_ok = true;
3121                                         $$ = (Node *) n;
3122                                 }
3123                 ;
3124
3125 /*****************************************************************************
3126  *
3127  *              QUERY:
3128  *             CREATE FOREIGN DATA WRAPPER name [ VALIDATOR name ]
3129  *
3130  *****************************************************************************/
3131
3132 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_validator create_generic_options
3133                                 {
3134                                         CreateFdwStmt *n = makeNode(CreateFdwStmt);
3135                                         n->fdwname = $5;
3136                                         n->validator = $6;
3137                                         n->options = $7;
3138                                         $$ = (Node *) n;
3139                                 }
3140                 ;
3141
3142 /*****************************************************************************
3143  *
3144  *              QUERY :
3145  *                              DROP FOREIGN DATA WRAPPER name
3146  *
3147  ****************************************************************************/
3148
3149 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
3150                                 {
3151                                         DropFdwStmt *n = makeNode(DropFdwStmt);
3152                                         n->fdwname = $5;
3153                                         n->missing_ok = false;
3154                                         n->behavior = $6;
3155                                         $$ = (Node *) n;
3156                                 }
3157                                 |  DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
3158                 {
3159                                         DropFdwStmt *n = makeNode(DropFdwStmt);
3160                                         n->fdwname = $7;
3161                                         n->missing_ok = true;
3162                                         n->behavior = $8;
3163                                         $$ = (Node *) n;
3164                                 }
3165                 ;
3166
3167 /*****************************************************************************
3168  *
3169  *              QUERY :
3170  *                              ALTER FOREIGN DATA WRAPPER name
3171  *
3172  ****************************************************************************/
3173
3174 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name validator_clause alter_generic_options
3175                                 {
3176                                         AlterFdwStmt *n = makeNode(AlterFdwStmt);
3177                                         n->fdwname = $5;
3178                                         n->validator = $6;
3179                                         n->change_validator = true;
3180                                         n->options = $7;
3181                                         $$ = (Node *) n;
3182                                 }
3183                         | ALTER FOREIGN DATA_P WRAPPER name validator_clause
3184                                 {
3185                                         AlterFdwStmt *n = makeNode(AlterFdwStmt);
3186                                         n->fdwname = $5;
3187                                         n->validator = $6;
3188                                         n->change_validator = true;
3189                                         $$ = (Node *) n;
3190                                 }
3191                         | ALTER FOREIGN DATA_P WRAPPER name alter_generic_options
3192                                 {
3193                                         AlterFdwStmt *n = makeNode(AlterFdwStmt);
3194                                         n->fdwname = $5;
3195                                         n->options = $6;
3196                                         $$ = (Node *) n;
3197                                 }
3198                 ;
3199
3200 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
3201 create_generic_options:
3202                         OPTIONS '(' generic_option_list ')'                     { $$ = $3; }
3203                         | /*EMPTY*/                                                                     { $$ = NIL; }
3204                 ;
3205
3206 generic_option_list:
3207                         generic_option_elem
3208                                 {
3209                                         $$ = list_make1($1);
3210                                 }
3211                         | generic_option_list ',' generic_option_elem
3212                                 {
3213                                         $$ = lappend($1, $3);
3214                                 }
3215                 ;
3216
3217 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
3218 alter_generic_options:
3219                         OPTIONS '(' alter_generic_option_list ')'               { $$ = $3; }
3220                 ;
3221
3222 alter_generic_option_list:
3223                         alter_generic_option_elem
3224                                 {
3225                                         $$ = list_make1($1);
3226                                 }
3227                         | alter_generic_option_list ',' alter_generic_option_elem
3228                                 {
3229                                         $$ = lappend($1, $3);
3230                                 }
3231                 ;
3232
3233 alter_generic_option_elem:
3234                         generic_option_elem
3235                                 {
3236                                         $$ = $1;
3237                                 }
3238                         | SET generic_option_elem
3239                                 {
3240                                         $$ = $2;
3241                                         $$->defaction = DEFELEM_SET;
3242                                 }
3243                         | ADD_P generic_option_elem
3244                                 {
3245                                         $$ = $2;
3246                                         $$->defaction = DEFELEM_ADD;
3247                                 }
3248                         | DROP generic_option_name
3249                                 {
3250                                         $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
3251                                 }
3252                 ;
3253
3254 generic_option_elem:
3255                         generic_option_name generic_option_arg
3256                                 {
3257                                         $$ = makeDefElem($1, $2);
3258                                 }
3259                 ;
3260
3261 generic_option_name:
3262                                 ColLabel                        { $$ = $1; }
3263                 ;
3264
3265 /* We could use def_arg here, but the spec only requires string literals */
3266 generic_option_arg:
3267                                 Sconst                          { $$ = (Node *) makeString($1); }
3268                 ;
3269
3270 /*****************************************************************************
3271  *
3272  *              QUERY:
3273  *             CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
3274  *
3275  *****************************************************************************/
3276
3277 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
3278                                                  FOREIGN DATA_P WRAPPER name create_generic_options
3279                                 {
3280                                         CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
3281                                         n->servername = $3;
3282                                         n->servertype = $4;
3283                                         n->version = $5;
3284                                         n->fdwname = $9;
3285                                         n->options = $10;
3286                                         $$ = (Node *) n;
3287                                 }
3288                 ;
3289
3290 opt_type:
3291                         TYPE_P Sconst                   { $$ = $2; }
3292                         | /*EMPTY*/                             { $$ = NULL; }
3293                 ;
3294
3295
3296 foreign_server_version:
3297                         VERSION_P Sconst                { $$ = $2; }
3298                 |       VERSION_P NULL_P                { $$ = NULL; }
3299                 ;
3300
3301 opt_foreign_server_version:
3302                         foreign_server_version  { $$ = $1; }
3303                         | /*EMPTY*/                             { $$ = NULL; }
3304                 ;
3305
3306 /*****************************************************************************
3307  *
3308  *              QUERY :
3309  *                              DROP SERVER name
3310  *
3311  ****************************************************************************/
3312
3313 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
3314                                 {
3315                                         DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3316                                         n->servername = $3;
3317                                         n->missing_ok = false;
3318                                         n->behavior = $4;
3319                                         $$ = (Node *) n;
3320                                 }
3321                                 |  DROP SERVER IF_P EXISTS name opt_drop_behavior
3322                 {
3323                                         DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3324                                         n->servername = $5;
3325                                         n->missing_ok = true;
3326                                         n->behavior = $6;
3327                                         $$ = (Node *) n;
3328                                 }
3329                 ;
3330
3331 /*****************************************************************************
3332  *
3333  *              QUERY :
3334  *                              ALTER SERVER name [VERSION] [OPTIONS]
3335  *
3336  ****************************************************************************/
3337
3338 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
3339                                 {
3340                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3341                                         n->servername = $3;
3342                                         n->version = $4;
3343                                         n->options = $5;
3344                                         n->has_version = true;
3345                                         $$ = (Node *) n;
3346                                 }
3347                         | ALTER SERVER name foreign_server_version
3348                                 {
3349                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3350                                         n->servername = $3;
3351                                         n->version = $4;
3352                                         n->has_version = true;
3353                                         $$ = (Node *) n;
3354                                 }
3355                         | ALTER SERVER name alter_generic_options
3356                                 {
3357                                         AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3358                                         n->servername = $3;
3359                                         n->options = $4;
3360                                         $$ = (Node *) n;
3361                                 }
3362                 ;
3363
3364 /*****************************************************************************
3365  *
3366  *              QUERY:
3367  *             CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
3368  *
3369  *****************************************************************************/
3370
3371 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
3372                                 {
3373                                         CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3374                                         n->username = $5;
3375                                         n->servername = $7;
3376                                         n->options = $8;
3377                                         $$ = (Node *) n;
3378                                 }
3379                 ;
3380
3381 /* User mapping authorization identifier */
3382 auth_ident:
3383                         CURRENT_USER    { $$ = "current_user"; }
3384                 |       USER                    { $$ = "current_user"; }
3385                 |       RoleId                  { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3386                 ;
3387
3388 /*****************************************************************************
3389  *
3390  *              QUERY :
3391  *                              DROP USER MAPPING FOR auth_ident SERVER name
3392  *
3393  ****************************************************************************/
3394
3395 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3396                                 {
3397                                         DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3398                                         n->username = $5;
3399                                         n->servername = $7;
3400                                         n->missing_ok = false;
3401                                         $$ = (Node *) n;
3402                                 }
3403                                 |  DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3404                 {
3405                                         DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3406                                         n->username = $7;
3407                                         n->servername = $9;
3408                                         n->missing_ok = true;
3409                                         $$ = (Node *) n;
3410                                 }
3411                 ;
3412
3413 /*****************************************************************************
3414  *
3415  *              QUERY :
3416  *                              ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3417  *
3418  ****************************************************************************/
3419
3420 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3421                                 {
3422                                         AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3423                                         n->username = $5;
3424                                         n->servername = $7;
3425                                         n->options = $8;
3426                                         $$ = (Node *) n;
3427                                 }
3428                 ;
3429
3430 /*****************************************************************************
3431  *
3432  *              QUERIES :
3433  *                              CREATE TRIGGER ...
3434  *                              DROP TRIGGER ...
3435  *
3436  *****************************************************************************/
3437
3438 CreateTrigStmt:
3439                         CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3440                         qualified_name TriggerForSpec TriggerWhen
3441                         EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
3442                                 {
3443                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
3444                                         n->trigname = $3;
3445                                         n->relation = $7;
3446                                         n->funcname = $12;
3447                                         n->args = $14;
3448                                         n->before = $4;
3449                                         n->row = $8;
3450                                         n->events = intVal(linitial($5));
3451                                         n->columns = (List *) lsecond($5);
3452                                         n->whenClause = $9;
3453                                         n->isconstraint  = FALSE;
3454                                         n->deferrable    = FALSE;
3455                                         n->initdeferred  = FALSE;
3456                                         n->constrrel = NULL;
3457                                         $$ = (Node *)n;
3458                                 }
3459                         | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
3460                         qualified_name OptConstrFromTable ConstraintAttributeSpec
3461                         FOR EACH ROW TriggerWhen
3462                         EXECUTE PROCEDURE func_name '(' TriggerFuncArgs ')'
3463                                 {
3464                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
3465                                         n->trigname = $4;
3466                                         n->relation = $8;
3467                                         n->funcname = $17;
3468                                         n->args = $19;
3469                                         n->before = FALSE;
3470                                         n->row = TRUE;
3471                                         n->events = intVal(linitial($6));
3472                                         n->columns = (List *) lsecond($6);
3473                                         n->whenClause = $14;
3474                                         n->isconstraint  = TRUE;
3475                                         n->deferrable = ($10 & 1) != 0;
3476                                         n->initdeferred = ($10 & 2) != 0;
3477                                         n->constrrel = $9;
3478                                         $$ = (Node *)n;
3479                                 }
3480                 ;
3481
3482 TriggerActionTime:
3483                         BEFORE                                                                  { $$ = TRUE; }
3484                         | AFTER                                                                 { $$ = FALSE; }
3485                 ;
3486
3487 TriggerEvents:
3488                         TriggerOneEvent
3489                                 { $$ = $1; }
3490                         | TriggerEvents OR TriggerOneEvent
3491                                 {
3492                                         int             events1 = intVal(linitial($1));
3493                                         int             events2 = intVal(linitial($3));
3494                                         List   *columns1 = (List *) lsecond($1);
3495                                         List   *columns2 = (List *) lsecond($3);
3496
3497                                         if (events1 & events2)
3498                                                 parser_yyerror("duplicate trigger events specified");
3499                                         /*
3500                                          * concat'ing the columns lists loses information about
3501                                          * which columns went with which event, but so long as
3502                                          * only UPDATE carries columns and we disallow multiple
3503                                          * UPDATE items, it doesn't matter.  Command execution
3504                                          * should just ignore the columns for non-UPDATE events.
3505                                          */
3506                                         $$ = list_make2(makeInteger(events1 | events2),
3507                                                                         list_concat(columns1, columns2));
3508                                 }
3509                 ;
3510
3511 TriggerOneEvent:
3512                         INSERT
3513                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_INSERT), NIL); }
3514                         | DELETE_P
3515                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_DELETE), NIL); }
3516                         | UPDATE
3517                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), NIL); }
3518                         | UPDATE OF columnList
3519                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_UPDATE), $3); }
3520                         | TRUNCATE
3521                                 { $$ = list_make2(makeInteger(TRIGGER_TYPE_TRUNCATE), NIL); }
3522                 ;
3523
3524 TriggerForSpec:
3525                         FOR TriggerForOpt TriggerForType
3526                                 {
3527                                         $$ = $3;
3528                                 }
3529                         | /* EMPTY */
3530                                 {
3531                                         /*
3532                                          * If ROW/STATEMENT not specified, default to
3533                                          * STATEMENT, per SQL
3534                                          */
3535                                         $$ = FALSE;
3536                                 }
3537                 ;
3538
3539 TriggerForOpt:
3540                         EACH                                                                    {}
3541                         | /*EMPTY*/                                                             {}
3542                 ;
3543
3544 TriggerForType:
3545                         ROW                                                                             { $$ = TRUE; }
3546                         | STATEMENT                                                             { $$ = FALSE; }
3547                 ;
3548
3549 TriggerWhen:
3550                         WHEN '(' a_expr ')'                                             { $$ = $3; }
3551                         | /*EMPTY*/                                                             { $$ = NULL; }
3552                 ;
3553
3554 TriggerFuncArgs:
3555                         TriggerFuncArg                                                  { $$ = list_make1($1); }
3556                         | TriggerFuncArgs ',' TriggerFuncArg    { $$ = lappend($1, $3); }
3557                         | /*EMPTY*/                                                             { $$ = NIL; }
3558                 ;
3559
3560 TriggerFuncArg:
3561                         Iconst
3562                                 {
3563                                         char buf[64];
3564                                         snprintf(buf, sizeof(buf), "%d", $1);
3565                                         $$ = makeString(pstrdup(buf));
3566                                 }
3567                         | FCONST                                                                { $$ = makeString($1); }
3568                         | Sconst                                                                { $$ = makeString($1); }
3569                         | BCONST                                                                { $$ = makeString($1); }
3570                         | XCONST                                                                { $$ = makeString($1); }
3571                         | ColId                                                                 { $$ = makeString($1); }
3572                 ;
3573
3574 OptConstrFromTable:
3575                         FROM qualified_name                                             { $$ = $2; }
3576                         | /*EMPTY*/                                                             { $$ = NULL; }
3577                 ;
3578
3579 ConstraintAttributeSpec:
3580                         ConstraintDeferrabilitySpec
3581                                 { $$ = $1; }
3582                         | ConstraintDeferrabilitySpec ConstraintTimeSpec
3583                                 {
3584                                         if ($1 == 0 && $2 != 0)
3585                                                 ereport(ERROR,
3586                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
3587                                                                  errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3588                                                                  parser_errposition(@1)));
3589                                         $$ = $1 | $2;
3590                                 }
3591                         | ConstraintTimeSpec
3592                                 {
3593                                         if ($1 != 0)
3594                                                 $$ = 3;
3595                                         else
3596                                                 $$ = 0;
3597                                 }
3598                         | ConstraintTimeSpec ConstraintDeferrabilitySpec
3599                                 {
3600                                         if ($2 == 0 && $1 != 0)
3601                                                 ereport(ERROR,
3602                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
3603                                                                  errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3604                                                                  parser_errposition(@1)));
3605                                         $$ = $1 | $2;
3606                                 }
3607                         | /*EMPTY*/
3608                                 { $$ = 0; }
3609                 ;
3610
3611 ConstraintDeferrabilitySpec:
3612                         NOT DEFERRABLE                                                  { $$ = 0; }
3613                         | DEFERRABLE                                                    { $$ = 1; }
3614                 ;
3615
3616 ConstraintTimeSpec:
3617                         INITIALLY IMMEDIATE                                             { $$ = 0; }
3618                         | INITIALLY DEFERRED                                    { $$ = 2; }
3619                 ;
3620
3621
3622 DropTrigStmt:
3623                         DROP TRIGGER name ON qualified_name opt_drop_behavior
3624                                 {
3625                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
3626                                         n->relation = $5;
3627                                         n->property = $3;
3628                                         n->behavior = $6;
3629                                         n->removeType = OBJECT_TRIGGER;
3630                                         n->missing_ok = false;
3631                                         $$ = (Node *) n;
3632                                 }
3633                         | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
3634                                 {
3635                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
3636                                         n->relation = $7;
3637                                         n->property = $5;
3638                                         n->behavior = $8;
3639                                         n->removeType = OBJECT_TRIGGER;
3640                                         n->missing_ok = true;
3641                                         $$ = (Node *) n;
3642                                 }
3643                 ;
3644
3645
3646 /*****************************************************************************
3647  *
3648  *              QUERIES :
3649  *                              CREATE ASSERTION ...
3650  *                              DROP ASSERTION ...
3651  *
3652  *****************************************************************************/
3653
3654 CreateAssertStmt:
3655                         CREATE ASSERTION name CHECK '(' a_expr ')'
3656                         ConstraintAttributeSpec
3657                                 {
3658                                         CreateTrigStmt *n = makeNode(CreateTrigStmt);
3659                                         n->trigname = $3;
3660                                         n->args = list_make1($6);
3661                                         n->isconstraint  = TRUE;
3662                                         n->deferrable = ($8 & 1) != 0;
3663                                         n->initdeferred = ($8 & 2) != 0;
3664
3665                                         ereport(ERROR,
3666                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3667                                                          errmsg("CREATE ASSERTION is not yet implemented")));
3668
3669                                         $$ = (Node *)n;
3670                                 }
3671                 ;
3672
3673 DropAssertStmt:
3674                         DROP ASSERTION name opt_drop_behavior
3675                                 {
3676                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
3677                                         n->relation = NULL;
3678                                         n->property = $3;
3679                                         n->behavior = $4;
3680                                         n->removeType = OBJECT_TRIGGER; /* XXX */
3681                                         ereport(ERROR,
3682                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3683                                                          errmsg("DROP ASSERTION is not yet implemented")));
3684                                         $$ = (Node *) n;
3685                                 }
3686                 ;
3687
3688
3689 /*****************************************************************************
3690  *
3691  *              QUERY :
3692  *                              define (aggregate,operator,type)
3693  *
3694  *****************************************************************************/
3695
3696 DefineStmt:
3697                         CREATE AGGREGATE func_name aggr_args definition
3698                                 {
3699                                         DefineStmt *n = makeNode(DefineStmt);
3700                                         n->kind = OBJECT_AGGREGATE;
3701                                         n->oldstyle = false;
3702                                         n->defnames = $3;
3703                                         n->args = $4;
3704                                         n->definition = $5;
3705                                         $$ = (Node *)n;
3706                                 }
3707                         | CREATE AGGREGATE func_name old_aggr_definition
3708                                 {
3709                                         /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
3710                                         DefineStmt *n = makeNode(DefineStmt);
3711                                         n->kind = OBJECT_AGGREGATE;
3712                                         n->oldstyle = true;
3713                                         n->defnames = $3;
3714                                         n->args = NIL;
3715                                         n->definition = $4;
3716                                         $$ = (Node *)n;
3717                                 }
3718                         | CREATE OPERATOR any_operator definition
3719                                 {
3720                                         DefineStmt *n = makeNode(DefineStmt);
3721                                         n->kind = OBJECT_OPERATOR;
3722                                         n->oldstyle = false;
3723                                         n->defnames = $3;
3724                                         n->args = NIL;
3725                                         n->definition = $4;
3726                                         $$ = (Node *)n;
3727                                 }
3728                         | CREATE TYPE_P any_name definition
3729                                 {
3730                                         DefineStmt *n = makeNode(DefineStmt);
3731                                         n->kind = OBJECT_TYPE;
3732                                         n->oldstyle = false;
3733                                         n->defnames = $3;
3734                                         n->args = NIL;
3735                                         n->definition = $4;
3736                                         $$ = (Node *)n;
3737                                 }
3738                         | CREATE TYPE_P any_name
3739                                 {
3740                                         /* Shell type (identified by lack of definition) */
3741                                         DefineStmt *n = makeNode(DefineStmt);
3742                                         n->kind = OBJECT_TYPE;
3743                                         n->oldstyle = false;
3744                                         n->defnames = $3;
3745                                         n->args = NIL;
3746                                         n->definition = NIL;
3747                                         $$ = (Node *)n;
3748                                 }
3749                         | CREATE TYPE_P any_name AS '(' OptTableFuncElementList ')'
3750                                 {
3751                                         CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3752
3753                                         /* can't use qualified_name, sigh */
3754                                         n->typevar = makeRangeVarFromAnyName($3, @3, yyscanner);
3755                                         n->coldeflist = $6;
3756                                         $$ = (Node *)n;
3757                                 }
3758                         | CREATE TYPE_P any_name AS ENUM_P '(' opt_enum_val_list ')'
3759                                 {
3760                                         CreateEnumStmt *n = makeNode(CreateEnumStmt);
3761                                         n->typeName = $3;
3762                                         n->vals = $7;
3763                                         $$ = (Node *)n;
3764                                 }
3765                         | CREATE TEXT_P SEARCH PARSER any_name definition
3766                                 {
3767                                         DefineStmt *n = makeNode(DefineStmt);
3768                                         n->kind = OBJECT_TSPARSER;
3769                                         n->args = NIL;
3770                                         n->defnames = $5;
3771                                         n->definition = $6;
3772                                         $$ = (Node *)n;
3773                                 }
3774                         | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3775                                 {
3776                                         DefineStmt *n = makeNode(DefineStmt);
3777                                         n->kind = OBJECT_TSDICTIONARY;
3778                                         n->args = NIL;
3779                                         n->defnames = $5;
3780                                         n->definition = $6;
3781                                         $$ = (Node *)n;
3782                                 }
3783                         | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3784                                 {
3785                                         DefineStmt *n = makeNode(DefineStmt);
3786                                         n->kind = OBJECT_TSTEMPLATE;
3787                                         n->args = NIL;
3788                                         n->defnames = $5;
3789                                         n->definition = $6;
3790                                         $$ = (Node *)n;
3791                                 }
3792                         | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3793                                 {
3794                                         DefineStmt *n = makeNode(DefineStmt);
3795                                         n->kind = OBJECT_TSCONFIGURATION;
3796                                         n->args = NIL;
3797                                         n->defnames = $5;
3798                                         n->definition = $6;
3799                                         $$ = (Node *)n;
3800                                 }
3801                 ;
3802
3803 definition: '(' def_list ')'                                            { $$ = $2; }
3804                 ;
3805
3806 def_list:       def_elem                                                                { $$ = list_make1($1); }
3807                         | def_list ',' def_elem                                 { $$ = lappend($1, $3); }
3808                 ;
3809
3810 def_elem:       ColLabel '=' def_arg
3811                                 {
3812                                         $$ = makeDefElem($1, (Node *) $3);
3813                                 }
3814                         | ColLabel
3815                                 {
3816                                         $$ = makeDefElem($1, NULL);
3817                                 }
3818                 ;
3819
3820 /* Note: any simple identifier will be returned as a type name! */
3821 def_arg:        func_type                                               { $$ = (Node *)$1; }
3822                         | reserved_keyword                              { $$ = (Node *)makeString(pstrdup($1)); }
3823                         | qual_all_Op                                   { $$ = (Node *)$1; }
3824                         | NumericOnly                                   { $$ = (Node *)$1; }
3825                         | Sconst                                                { $$ = (Node *)makeString($1); }
3826                 ;
3827
3828 aggr_args:      '(' type_list ')'                                               { $$ = $2; }
3829                         | '(' '*' ')'                                                   { $$ = NIL; }
3830                 ;
3831
3832 old_aggr_definition: '(' old_aggr_list ')'                      { $$ = $2; }
3833                 ;
3834
3835 old_aggr_list: old_aggr_elem                                            { $$ = list_make1($1); }
3836                         | old_aggr_list ',' old_aggr_elem               { $$ = lappend($1, $3); }
3837                 ;
3838
3839 /*
3840  * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
3841  * the item names needed in old aggregate definitions are likely to become
3842  * SQL keywords.
3843  */
3844 old_aggr_elem:  IDENT '=' def_arg
3845                                 {
3846                                         $$ = makeDefElem($1, (Node *)$3);
3847                                 }
3848                 ;
3849
3850 opt_enum_val_list:
3851                 enum_val_list                                                   { $$ = $1; }
3852                 | /*EMPTY*/                                                             { $$ = NIL; }
3853                 ;
3854
3855 enum_val_list:  Sconst
3856                                 { $$ = list_make1(makeString($1)); }
3857                         | enum_val_list ',' Sconst
3858                                 { $$ = lappend($1, makeString($3)); }
3859                 ;
3860
3861
3862 /*****************************************************************************
3863  *
3864  *              QUERIES :
3865  *                              CREATE OPERATOR CLASS ...
3866  *                              CREATE OPERATOR FAMILY ...
3867  *                              ALTER OPERATOR FAMILY ...
3868  *                              DROP OPERATOR CLASS ...
3869  *                              DROP OPERATOR FAMILY ...
3870  *
3871  *****************************************************************************/
3872
3873 CreateOpClassStmt:
3874                         CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3875                         USING access_method opt_opfamily AS opclass_item_list
3876                                 {
3877                                         CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3878                                         n->opclassname = $4;
3879                                         n->isDefault = $5;
3880                                         n->datatype = $8;
3881                                         n->amname = $10;
3882                                         n->opfamilyname = $11;
3883                                         n->items = $13;
3884                                         $$ = (Node *) n;
3885                                 }
3886                 ;
3887
3888 opclass_item_list:
3889                         opclass_item                                                    { $$ = list_make1($1); }
3890                         | opclass_item_list ',' opclass_item    { $$ = lappend($1, $3); }
3891                 ;
3892
3893 opclass_item:
3894                         OPERATOR Iconst any_operator opt_recheck
3895                                 {
3896                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
3897                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
3898                                         n->name = $3;
3899                                         n->args = NIL;
3900                                         n->number = $2;
3901                                         $$ = (Node *) n;
3902                                 }
3903                         | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3904                                 {
3905                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
3906                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
3907                                         n->name = $3;
3908                                         n->args = $4;
3909                                         n->number = $2;
3910                                         $$ = (Node *) n;
3911                                 }
3912                         | FUNCTION Iconst func_name func_args
3913                                 {
3914                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
3915                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
3916                                         n->name = $3;
3917                                         n->args = extractArgTypes($4);
3918                                         n->number = $2;
3919                                         $$ = (Node *) n;
3920                                 }
3921                         | FUNCTION Iconst '(' type_list ')' func_name func_args
3922                                 {
3923                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
3924                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
3925                                         n->name = $6;
3926                                         n->args = extractArgTypes($7);
3927                                         n->number = $2;
3928                                         n->class_args = $4;
3929                                         $$ = (Node *) n;
3930                                 }
3931                         | STORAGE Typename
3932                                 {
3933                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
3934                                         n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3935                                         n->storedtype = $2;
3936                                         $$ = (Node *) n;
3937                                 }
3938                 ;
3939
3940 opt_default:    DEFAULT                                         { $$ = TRUE; }
3941                         | /*EMPTY*/                                             { $$ = FALSE; }
3942                 ;
3943
3944 opt_opfamily:   FAMILY any_name                         { $$ = $2; }
3945                         | /*EMPTY*/                                             { $$ = NIL; }
3946                 ;
3947
3948 opt_recheck:    RECHECK
3949                                 {
3950                                         /*
3951                                          * RECHECK no longer does anything in opclass definitions,
3952                                          * but we still accept it to ease porting of old database
3953                                          * dumps.
3954                                          */
3955                                         ereport(NOTICE,
3956                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3957                                                          errmsg("RECHECK is no longer required"),
3958                                                          errhint("Update your data type."),
3959                                                          parser_errposition(@1)));
3960                                         $$ = TRUE;
3961                                 }
3962                         | /*EMPTY*/                                             { $$ = FALSE; }
3963                 ;
3964
3965
3966 CreateOpFamilyStmt:
3967                         CREATE OPERATOR FAMILY any_name USING access_method
3968                                 {
3969                                         CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3970                                         n->opfamilyname = $4;
3971                                         n->amname = $6;
3972                                         $$ = (Node *) n;
3973                                 }
3974                 ;
3975
3976 AlterOpFamilyStmt:
3977                         ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3978                                 {
3979                                         AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3980                                         n->opfamilyname = $4;
3981                                         n->amname = $6;
3982                                         n->isDrop = false;
3983                                         n->items = $8;
3984                                         $$ = (Node *) n;
3985                                 }
3986                         | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3987                                 {
3988                                         AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3989                                         n->opfamilyname = $4;
3990                                         n->amname = $6;
3991                                         n->isDrop = true;
3992                                         n->items = $8;
3993                                         $$ = (Node *) n;
3994                                 }
3995                 ;
3996
3997 opclass_drop_list:
3998                         opclass_drop                                                    { $$ = list_make1($1); }
3999                         | opclass_drop_list ',' opclass_drop    { $$ = lappend($1, $3); }
4000                 ;
4001
4002 opclass_drop:
4003                         OPERATOR Iconst '(' type_list ')'
4004                                 {
4005                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4006                                         n->itemtype = OPCLASS_ITEM_OPERATOR;
4007                                         n->number = $2;
4008                                         n->args = $4;
4009                                         $$ = (Node *) n;
4010                                 }
4011                         | FUNCTION Iconst '(' type_list ')'
4012                                 {
4013                                         CreateOpClassItem *n = makeNode(CreateOpClassItem);
4014                                         n->itemtype = OPCLASS_ITEM_FUNCTION;
4015                                         n->number = $2;
4016                                         n->args = $4;
4017                                         $$ = (Node *) n;
4018                                 }
4019                 ;
4020
4021
4022 DropOpClassStmt:
4023                         DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
4024                                 {
4025                                         RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
4026                                         n->opclassname = $4;
4027                                         n->amname = $6;
4028                                         n->behavior = $7;
4029                                         n->missing_ok = false;
4030                                         $$ = (Node *) n;
4031                                 }
4032                         | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
4033                                 {
4034                                         RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
4035                                         n->opclassname = $6;
4036                                         n->amname = $8;
4037                                         n->behavior = $9;
4038                                         n->missing_ok = true;
4039                                         $$ = (Node *) n;
4040                                 }
4041                 ;
4042
4043 DropOpFamilyStmt:
4044                         DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
4045                                 {
4046                                         RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
4047                                         n->opfamilyname = $4;
4048                                         n->amname = $6;
4049                                         n->behavior = $7;
4050                                         n->missing_ok = false;
4051                                         $$ = (Node *) n;
4052                                 }
4053                         | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
4054                                 {
4055                                         RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
4056                                         n->opfamilyname = $6;
4057                                         n->amname = $8;
4058                                         n->behavior = $9;
4059                                         n->missing_ok = true;
4060                                         $$ = (Node *) n;
4061                                 }
4062                 ;
4063
4064
4065 /*****************************************************************************
4066  *
4067  *              QUERY:
4068  *
4069  *              DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
4070  *              REASSIGN OWNED BY username [, username ...] TO username
4071  *
4072  *****************************************************************************/
4073 DropOwnedStmt:
4074                         DROP OWNED BY name_list opt_drop_behavior
4075                                 {
4076                                         DropOwnedStmt *n = makeNode(DropOwnedStmt);
4077                                         n->roles = $4;
4078                                         n->behavior = $5;
4079                                         $$ = (Node *)n;
4080                                 }
4081                 ;
4082
4083 ReassignOwnedStmt:
4084                         REASSIGN OWNED BY name_list TO name
4085                                 {
4086                                         ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
4087                                         n->roles = $4;
4088                                         n->newrole = $6;
4089                                         $$ = (Node *)n;
4090                                 }
4091                 ;
4092
4093 /*****************************************************************************
4094  *
4095  *              QUERY:
4096  *
4097  *              DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
4098  *           [ RESTRICT | CASCADE ]
4099  *
4100  *****************************************************************************/
4101
4102 DropStmt:       DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
4103                                 {
4104                                         DropStmt *n = makeNode(DropStmt);
4105                                         n->removeType = $2;
4106                                         n->missing_ok = TRUE;
4107                                         n->objects = $5;
4108                                         n->behavior = $6;
4109                                         $$ = (Node *)n;
4110                                 }
4111                         | DROP drop_type any_name_list opt_drop_behavior
4112                                 {
4113                                         DropStmt *n = makeNode(DropStmt);
4114                                         n->removeType = $2;
4115                                         n->missing_ok = FALSE;
4116                                         n->objects = $3;
4117                                         n->behavior = $4;
4118                                         $$ = (Node *)n;
4119                                 }
4120                 ;
4121
4122
4123 drop_type:      TABLE                                                                   { $$ = OBJECT_TABLE; }
4124                         | SEQUENCE                                                              { $$ = OBJECT_SEQUENCE; }
4125                         | VIEW                                                                  { $$ = OBJECT_VIEW; }
4126                         | INDEX                                                                 { $$ = OBJECT_INDEX; }
4127                         | TYPE_P                                                                { $$ = OBJECT_TYPE; }
4128                         | DOMAIN_P                                                              { $$ = OBJECT_DOMAIN; }
4129                         | CONVERSION_P                                                  { $$ = OBJECT_CONVERSION; }
4130                         | SCHEMA                                                                { $$ = OBJECT_SCHEMA; }
4131                         | TEXT_P SEARCH PARSER                                  { $$ = OBJECT_TSPARSER; }
4132                         | TEXT_P SEARCH DICTIONARY                              { $$ = OBJECT_TSDICTIONARY; }
4133                         | TEXT_P SEARCH TEMPLATE                                { $$ = OBJECT_TSTEMPLATE; }
4134                         | TEXT_P SEARCH CONFIGURATION                   { $$ = OBJECT_TSCONFIGURATION; }
4135                 ;
4136
4137 any_name_list:
4138                         any_name                                                                { $$ = list_make1($1); }
4139                         | any_name_list ',' any_name                    { $$ = lappend($1, $3); }
4140                 ;
4141
4142 any_name:       ColId                                           { $$ = list_make1(makeString($1)); }
4143                         | ColId attrs                           { $$ = lcons(makeString($1), $2); }
4144                 ;
4145
4146 attrs:          '.' attr_name
4147                                         { $$ = list_make1(makeString($2)); }
4148                         | attrs '.' attr_name
4149                                         { $$ = lappend($1, makeString($3)); }
4150                 ;
4151
4152
4153 /*****************************************************************************
4154  *
4155  *              QUERY:
4156  *                              truncate table relname1, relname2, ...
4157  *
4158  *****************************************************************************/
4159
4160 TruncateStmt:
4161                         TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
4162                                 {
4163                                         TruncateStmt *n = makeNode(TruncateStmt);
4164                                         n->relations = $3;
4165                                         n->restart_seqs = $4;
4166                                         n->behavior = $5;
4167                                         $$ = (Node *)n;
4168                                 }
4169                 ;
4170
4171 opt_restart_seqs:
4172                         CONTINUE_P IDENTITY_P           { $$ = false; }
4173                         | RESTART IDENTITY_P            { $$ = true; }
4174                         | /* EMPTY */                           { $$ = false; }
4175                 ;
4176
4177 /*****************************************************************************
4178  *
4179  *      The COMMENT ON statement can take different forms based upon the type of
4180  *      the object associated with the comment. The form of the statement is:
4181  *
4182  *      COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
4183  *                                 CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
4184  *                                 CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
4185  *                                 TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
4186  *                                 TEXT SEARCH TEMPLATE |
4187  *                                 TEXT SEARCH CONFIGURATION ] <objname> |
4188  *                               AGGREGATE <aggname> (arg1, ...) |
4189  *                               FUNCTION <funcname> (arg1, arg2, ...) |
4190  *                               OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
4191  *                               TRIGGER <triggername> ON <relname> |
4192  *                               CONSTRAINT <constraintname> ON <relname> |
4193  *                               RULE <rulename> ON <relname> ]
4194  *                         IS 'text'
4195  *
4196  *****************************************************************************/
4197
4198 CommentStmt:
4199                         COMMENT ON comment_type any_name IS comment_text
4200                                 {
4201                                         CommentStmt *n = makeNode(CommentStmt);
4202                                         n->objtype = $3;
4203                                         n->objname = $4;
4204                                         n->objargs = NIL;
4205                                         n->comment = $6;
4206                                         $$ = (Node *) n;
4207                                 }
4208                         | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
4209                                 {
4210                                         CommentStmt *n = makeNode(CommentStmt);
4211                                         n->objtype = OBJECT_AGGREGATE;
4212                                         n->objname = $4;
4213                                         n->objargs = $5;
4214                                         n->comment = $7;
4215                                         $$ = (Node *) n;
4216                                 }
4217                         | COMMENT ON FUNCTION func_name func_args IS comment_text
4218                                 {
4219                                         CommentStmt *n = makeNode(CommentStmt);
4220                                         n->objtype = OBJECT_FUNCTION;
4221                                         n->objname = $4;
4222                                         n->objargs = extractArgTypes($5);
4223                                         n->comment = $7;
4224                                         $$ = (Node *) n;
4225                                 }
4226                         | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
4227                                 {
4228                                         CommentStmt *n = makeNode(CommentStmt);
4229                                         n->objtype = OBJECT_OPERATOR;
4230                                         n->objname = $4;
4231                                         n->objargs = $5;
4232                                         n->comment = $7;
4233                                         $$ = (Node *) n;
4234                                 }
4235                         | COMMENT ON CONSTRAINT name ON any_name IS comment_text
4236                                 {
4237                                         CommentStmt *n = makeNode(CommentStmt);
4238                                         n->objtype = OBJECT_CONSTRAINT;
4239                                         n->objname = lappend($6, makeString($4));
4240                                         n->objargs = NIL;
4241                                         n->comment = $8;
4242                                         $$ = (Node *) n;
4243                                 }
4244                         | COMMENT ON RULE name ON any_name IS comment_text
4245                                 {
4246                                         CommentStmt *n = makeNode(CommentStmt);
4247                                         n->objtype = OBJECT_RULE;
4248                                         n->objname = lappend($6, makeString($4));
4249                                         n->objargs = NIL;
4250                                         n->comment = $8;
4251                                         $$ = (Node *) n;
4252                                 }
4253                         | COMMENT ON RULE name IS comment_text
4254                                 {
4255                                         /* Obsolete syntax supported for awhile for compatibility */
4256                                         CommentStmt *n = makeNode(CommentStmt);
4257                                         n->objtype = OBJECT_RULE;
4258                                         n->objname = list_make1(makeString($4));
4259                                         n->objargs = NIL;
4260                                         n->comment = $6;
4261                                         $$ = (Node *) n;
4262                                 }
4263                         | COMMENT ON TRIGGER name ON any_name IS comment_text
4264                                 {
4265                                         CommentStmt *n = makeNode(CommentStmt);
4266                                         n->objtype = OBJECT_TRIGGER;
4267                                         n->objname = lappend($6, makeString($4));
4268                                         n->objargs = NIL;
4269                                         n->comment = $8;
4270                                         $$ = (Node *) n;
4271                                 }
4272                         | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
4273                                 {
4274                                         CommentStmt *n = makeNode(CommentStmt);
4275                                         n->objtype = OBJECT_OPCLASS;
4276                                         n->objname = $5;
4277                                         n->objargs = list_make1(makeString($7));
4278                                         n->comment = $9;
4279                                         $$ = (Node *) n;
4280                                 }
4281                         | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
4282                                 {
4283                                         CommentStmt *n = makeNode(CommentStmt);
4284                                         n->objtype = OBJECT_OPFAMILY;
4285                                         n->objname = $5;
4286                                         n->objargs = list_make1(makeString($7));
4287                                         n->comment = $9;
4288                                         $$ = (Node *) n;
4289                                 }
4290                         | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
4291                                 {
4292                                         CommentStmt *n = makeNode(CommentStmt);
4293                                         n->objtype = OBJECT_LARGEOBJECT;
4294                                         n->objname = list_make1($5);
4295                                         n->objargs = NIL;
4296                                         n->comment = $7;
4297                                         $$ = (Node *) n;
4298                                 }
4299                         | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
4300                                 {
4301                                         CommentStmt *n = makeNode(CommentStmt);
4302                                         n->objtype = OBJECT_CAST;
4303                                         n->objname = list_make1($5);
4304                                         n->objargs = list_make1($7);
4305                                         n->comment = $10;
4306                                         $$ = (Node *) n;
4307                                 }
4308                         | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
4309                                 {
4310                                         CommentStmt *n = makeNode(CommentStmt);
4311                                         n->objtype = OBJECT_LANGUAGE;
4312                                         n->objname = $5;
4313                                         n->objargs = NIL;
4314                                         n->comment = $7;
4315                                         $$ = (Node *) n;
4316                                 }
4317                         | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
4318                                 {
4319                                         CommentStmt *n = makeNode(CommentStmt);
4320                                         n->objtype = OBJECT_TSPARSER;
4321                                         n->objname = $6;
4322                                         n->comment = $8;
4323                                         $$ = (Node *) n;
4324                                 }
4325                         | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
4326                                 {
4327                                         CommentStmt *n = makeNode(CommentStmt);
4328                                         n->objtype = OBJECT_TSDICTIONARY;
4329                                         n->objname = $6;
4330                                         n->comment = $8;
4331                                         $$ = (Node *) n;
4332                                 }
4333                         | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
4334                                 {
4335                                         CommentStmt *n = makeNode(CommentStmt);
4336                                         n->objtype = OBJECT_TSTEMPLATE;
4337                                         n->objname = $6;
4338                                         n->comment = $8;
4339                                         $$ = (Node *) n;
4340                                 }
4341                         | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
4342                                 {
4343                                         CommentStmt *n = makeNode(CommentStmt);
4344                                         n->objtype = OBJECT_TSCONFIGURATION;
4345                                         n->objname = $6;
4346                                         n->comment = $8;
4347                                         $$ = (Node *) n;
4348                                 }
4349                 ;
4350
4351 comment_type:
4352                         COLUMN                                                          { $$ = OBJECT_COLUMN; }
4353                         | DATABASE                                                      { $$ = OBJECT_DATABASE; }
4354                         | SCHEMA                                                        { $$ = OBJECT_SCHEMA; }
4355                         | INDEX                                                         { $$ = OBJECT_INDEX; }
4356                         | SEQUENCE                                                      { $$ = OBJECT_SEQUENCE; }
4357                         | TABLE                                                         { $$ = OBJECT_TABLE; }
4358                         | DOMAIN_P                                                      { $$ = OBJECT_TYPE; }
4359                         | TYPE_P                                                        { $$ = OBJECT_TYPE; }
4360                         | VIEW                                                          { $$ = OBJECT_VIEW; }
4361                         | CONVERSION_P                                          { $$ = OBJECT_CONVERSION; }
4362                         | TABLESPACE                                            { $$ = OBJECT_TABLESPACE; }
4363                         | ROLE                                                          { $$ = OBJECT_ROLE; }
4364                 ;
4365
4366 comment_text:
4367                         Sconst                                                          { $$ = $1; }
4368                         | NULL_P                                                        { $$ = NULL; }
4369                 ;
4370
4371 /*****************************************************************************
4372  *
4373  *              QUERY:
4374  *                      fetch/move
4375  *
4376  *****************************************************************************/
4377
4378 FetchStmt:      FETCH fetch_args
4379                                 {
4380                                         FetchStmt *n = (FetchStmt *) $2;
4381                                         n->ismove = FALSE;
4382                                         $$ = (Node *)n;
4383                                 }
4384                         | MOVE fetch_args
4385                                 {
4386                                         FetchStmt *n = (FetchStmt *) $2;
4387                                         n->ismove = TRUE;
4388                                         $$ = (Node *)n;
4389                                 }
4390                 ;
4391
4392 fetch_args:     cursor_name
4393                                 {
4394                                         FetchStmt *n = makeNode(FetchStmt);
4395                                         n->portalname = $1;
4396                                         n->direction = FETCH_FORWARD;
4397                                         n->howMany = 1;
4398                                         $$ = (Node *)n;
4399                                 }
4400                         | from_in cursor_name
4401                                 {
4402                                         FetchStmt *n = makeNode(FetchStmt);
4403                                         n->portalname = $2;
4404                                         n->direction = FETCH_FORWARD;
4405                                         n->howMany = 1;
4406                                         $$ = (Node *)n;
4407                                 }
4408                         | NEXT opt_from_in cursor_name
4409                                 {
4410                                         FetchStmt *n = makeNode(FetchStmt);
4411                                         n->portalname = $3;
4412                                         n->direction = FETCH_FORWARD;
4413                                         n->howMany = 1;
4414                                         $$ = (Node *)n;
4415                                 }
4416                         | PRIOR opt_from_in cursor_name
4417                                 {
4418                                         FetchStmt *n = makeNode(FetchStmt);
4419                                         n->portalname = $3;
4420                                         n->direction = FETCH_BACKWARD;
4421                                         n->howMany = 1;
4422                                         $$ = (Node *)n;
4423                                 }
4424                         | FIRST_P opt_from_in cursor_name
4425                                 {
4426                                         FetchStmt *n = makeNode(FetchStmt);
4427                                         n->portalname = $3;
4428                                         n->direction = FETCH_ABSOLUTE;
4429                                         n->howMany = 1;
4430                                         $$ = (Node *)n;
4431                                 }
4432                         | LAST_P opt_from_in cursor_name
4433                                 {
4434                                         FetchStmt *n = makeNode(FetchStmt);
4435                                         n->portalname = $3;
4436                                         n->direction = FETCH_ABSOLUTE;
4437                                         n->howMany = -1;
4438                                         $$ = (Node *)n;
4439                                 }
4440                         | ABSOLUTE_P SignedIconst opt_from_in cursor_name
4441                                 {
4442                                         FetchStmt *n = makeNode(FetchStmt);
4443                                         n->portalname = $4;
4444                                         n->direction = FETCH_ABSOLUTE;
4445                                         n->howMany = $2;
4446                                         $$ = (Node *)n;
4447                                 }
4448                         | RELATIVE_P SignedIconst opt_from_in cursor_name
4449                                 {
4450                                         FetchStmt *n = makeNode(FetchStmt);
4451                                         n->portalname = $4;
4452                                         n->direction = FETCH_RELATIVE;
4453                                         n->howMany = $2;
4454                                         $$ = (Node *)n;
4455                                 }
4456                         | SignedIconst opt_from_in cursor_name
4457                                 {
4458                                         FetchStmt *n = makeNode(FetchStmt);
4459                                         n->portalname = $3;
4460                                         n->direction = FETCH_FORWARD;
4461                                         n->howMany = $1;
4462                                         $$ = (Node *)n;
4463                                 }
4464                         | ALL opt_from_in cursor_name
4465                                 {
4466                                         FetchStmt *n = makeNode(FetchStmt);
4467                                         n->portalname = $3;
4468                                         n->direction = FETCH_FORWARD;
4469                                         n->howMany = FETCH_ALL;
4470                                         $$ = (Node *)n;
4471                                 }
4472                         | FORWARD opt_from_in cursor_name
4473                                 {
4474                                         FetchStmt *n = makeNode(FetchStmt);
4475                                         n->portalname = $3;
4476                                         n->direction = FETCH_FORWARD;
4477                                         n->howMany = 1;
4478                                         $$ = (Node *)n;
4479                                 }
4480                         | FORWARD SignedIconst opt_from_in cursor_name
4481                                 {
4482                                         FetchStmt *n = makeNode(FetchStmt);
4483                                         n->portalname = $4;
4484                                         n->direction = FETCH_FORWARD;
4485                                         n->howMany = $2;
4486                                         $$ = (Node *)n;
4487                                 }
4488                         | FORWARD ALL opt_from_in cursor_name
4489                                 {
4490                                         FetchStmt *n = makeNode(FetchStmt);
4491                                         n->portalname = $4;
4492                                         n->direction = FETCH_FORWARD;
4493                                         n->howMany = FETCH_ALL;
4494                                         $$ = (Node *)n;
4495                                 }
4496                         | BACKWARD opt_from_in cursor_name
4497                                 {
4498                                         FetchStmt *n = makeNode(FetchStmt);
4499                                         n->portalname = $3;
4500                                         n->direction = FETCH_BACKWARD;
4501                                         n->howMany = 1;
4502                                         $$ = (Node *)n;
4503                                 }
4504                         | BACKWARD SignedIconst opt_from_in cursor_name
4505                                 {
4506                                         FetchStmt *n = makeNode(FetchStmt);
4507                                         n->portalname = $4;
4508                                         n->direction = FETCH_BACKWARD;
4509                                         n->howMany = $2;
4510                                         $$ = (Node *)n;
4511                                 }
4512                         | BACKWARD ALL opt_from_in cursor_name
4513                                 {
4514                                         FetchStmt *n = makeNode(FetchStmt);
4515                                         n->portalname = $4;
4516                                         n->direction = FETCH_BACKWARD;
4517                                         n->howMany = FETCH_ALL;
4518                                         $$ = (Node *)n;
4519                                 }
4520                 ;
4521
4522 from_in:        FROM                                                                    {}
4523                         | IN_P                                                                  {}
4524                 ;
4525
4526 opt_from_in:    from_in                                                         {}
4527                         | /* EMPTY */                                                   {}
4528                 ;
4529
4530
4531 /*****************************************************************************
4532  *
4533  * GRANT and REVOKE statements
4534  *
4535  *****************************************************************************/
4536
4537 GrantStmt:      GRANT privileges ON privilege_target TO grantee_list
4538                         opt_grant_grant_option
4539                                 {
4540                                         GrantStmt *n = makeNode(GrantStmt);
4541                                         n->is_grant = true;
4542                                         n->privileges = $2;
4543                                         n->targtype = ($4)->targtype;
4544                                         n->objtype = ($4)->objtype;
4545                                         n->objects = ($4)->objs;
4546                                         n->grantees = $6;
4547                                         n->grant_option = $7;
4548                                         $$ = (Node*)n;
4549                                 }
4550                 ;
4551
4552 RevokeStmt:
4553                         REVOKE privileges ON privilege_target
4554                         FROM grantee_list opt_drop_behavior
4555                                 {
4556                                         GrantStmt *n = makeNode(GrantStmt);
4557                                         n->is_grant = false;
4558                                         n->grant_option = false;
4559                                         n->privileges = $2;
4560                                         n->targtype = ($4)->targtype;
4561                                         n->objtype = ($4)->objtype;
4562                                         n->objects = ($4)->objs;
4563                                         n->grantees = $6;
4564                                         n->behavior = $7;
4565                                         $$ = (Node *)n;
4566                                 }
4567                         | REVOKE GRANT OPTION FOR privileges ON privilege_target
4568                         FROM grantee_list opt_drop_behavior
4569                                 {
4570                                         GrantStmt *n = makeNode(GrantStmt);
4571                                         n->is_grant = false;
4572                                         n->grant_option = true;
4573                                         n->privileges = $5;
4574                                         n->targtype = ($7)->targtype;
4575                                         n->objtype = ($7)->objtype;
4576                                         n->objects = ($7)->objs;
4577                                         n->grantees = $9;
4578                                         n->behavior = $10;
4579                                         $$ = (Node *)n;
4580                                 }
4581                 ;
4582
4583
4584 /*
4585  * Privilege names are represented as strings; the validity of the privilege
4586  * names gets checked at execution.  This is a bit annoying but we have little
4587  * choice because of the syntactic conflict with lists of role names in
4588  * GRANT/REVOKE.  What's more, we have to call out in the "privilege"
4589  * production any reserved keywords that need to be usable as privilege names.
4590  */
4591
4592 /* either ALL [PRIVILEGES] or a list of individual privileges */
4593 privileges: privilege_list
4594                                 { $$ = $1; }
4595                         | ALL
4596                                 { $$ = NIL; }
4597                         | ALL PRIVILEGES
4598                                 { $$ = NIL; }
4599                         | ALL '(' columnList ')'
4600                                 {
4601                                         AccessPriv *n = makeNode(AccessPriv);
4602                                         n->priv_name = NULL;
4603                                         n->cols = $3;
4604                                         $$ = list_make1(n);
4605                                 }
4606                         | ALL PRIVILEGES '(' columnList ')'
4607                                 {
4608                                         AccessPriv *n = makeNode(AccessPriv);
4609                                         n->priv_name = NULL;
4610                                         n->cols = $4;
4611                                         $$ = list_make1(n);
4612                                 }
4613                 ;
4614
4615 privilege_list: privilege                                                       { $$ = list_make1($1); }
4616                         | privilege_list ',' privilege                  { $$ = lappend($1, $3); }
4617                 ;
4618
4619 privilege:      SELECT opt_column_list
4620                         {
4621                                 AccessPriv *n = makeNode(AccessPriv);
4622                                 n->priv_name = pstrdup($1);
4623                                 n->cols = $2;
4624                                 $$ = n;
4625                         }
4626                 | REFERENCES opt_column_list
4627                         {
4628                                 AccessPriv *n = makeNode(AccessPriv);
4629                                 n->priv_name = pstrdup($1);
4630                                 n->cols = $2;
4631                                 $$ = n;
4632                         }
4633                 | CREATE opt_column_list
4634                         {
4635                                 AccessPriv *n = makeNode(AccessPriv);
4636                                 n->priv_name = pstrdup($1);
4637                                 n->cols = $2;
4638                                 $$ = n;
4639                         }
4640                 | ColId opt_column_list
4641                         {
4642                                 AccessPriv *n = makeNode(AccessPriv);
4643                                 n->priv_name = $1;
4644                                 n->cols = $2;
4645                                 $$ = n;
4646                         }
4647                 ;
4648
4649
4650 /* Don't bother trying to fold the first two rules into one using
4651  * opt_table.  You're going to get conflicts.
4652  */
4653 privilege_target:
4654                         qualified_name_list
4655                                 {
4656                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4657                                         n->targtype = ACL_TARGET_OBJECT;
4658                                         n->objtype = ACL_OBJECT_RELATION;
4659                                         n->objs = $1;
4660                                         $$ = n;
4661                                 }
4662                         | TABLE qualified_name_list
4663                                 {
4664                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4665                                         n->targtype = ACL_TARGET_OBJECT;
4666                                         n->objtype = ACL_OBJECT_RELATION;
4667                                         n->objs = $2;
4668                                         $$ = n;
4669                                 }
4670                         | SEQUENCE qualified_name_list
4671                                 {
4672                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4673                                         n->targtype = ACL_TARGET_OBJECT;
4674                                         n->objtype = ACL_OBJECT_SEQUENCE;
4675                                         n->objs = $2;
4676                                         $$ = n;
4677                                 }
4678                         | FOREIGN DATA_P WRAPPER name_list
4679                                 {
4680                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4681                                         n->targtype = ACL_TARGET_OBJECT;
4682                                         n->objtype = ACL_OBJECT_FDW;
4683                                         n->objs = $4;
4684                                         $$ = n;
4685                                 }
4686                         | FOREIGN SERVER name_list
4687                                 {
4688                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4689                                         n->targtype = ACL_TARGET_OBJECT;
4690                                         n->objtype = ACL_OBJECT_FOREIGN_SERVER;
4691                                         n->objs = $3;
4692                                         $$ = n;
4693                                 }
4694                         | FUNCTION function_with_argtypes_list
4695                                 {
4696                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4697                                         n->targtype = ACL_TARGET_OBJECT;
4698                                         n->objtype = ACL_OBJECT_FUNCTION;
4699                                         n->objs = $2;
4700                                         $$ = n;
4701                                 }
4702                         | DATABASE name_list
4703                                 {
4704                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4705                                         n->targtype = ACL_TARGET_OBJECT;
4706                                         n->objtype = ACL_OBJECT_DATABASE;
4707                                         n->objs = $2;
4708                                         $$ = n;
4709                                 }
4710                         | LANGUAGE name_list
4711                                 {
4712                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4713                                         n->targtype = ACL_TARGET_OBJECT;
4714                                         n->objtype = ACL_OBJECT_LANGUAGE;
4715                                         n->objs = $2;
4716                                         $$ = n;
4717                                 }
4718                         | LARGE_P OBJECT_P NumericOnly_list
4719                                 {
4720                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4721                                         n->targtype = ACL_TARGET_OBJECT;
4722                                         n->objtype = ACL_OBJECT_LARGEOBJECT;
4723                                         n->objs = $3;
4724                                         $$ = n;
4725                                 }
4726                         | SCHEMA name_list
4727                                 {
4728                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4729                                         n->targtype = ACL_TARGET_OBJECT;
4730                                         n->objtype = ACL_OBJECT_NAMESPACE;
4731                                         n->objs = $2;
4732                                         $$ = n;
4733                                 }
4734                         | TABLESPACE name_list
4735                                 {
4736                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4737                                         n->targtype = ACL_TARGET_OBJECT;
4738                                         n->objtype = ACL_OBJECT_TABLESPACE;
4739                                         n->objs = $2;
4740                                         $$ = n;
4741                                 }
4742                         | ALL TABLES IN_P SCHEMA name_list
4743                                 {
4744                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4745                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
4746                                         n->objtype = ACL_OBJECT_RELATION;
4747                                         n->objs = $5;
4748                                         $$ = n;
4749                                 }
4750                         | ALL SEQUENCES IN_P SCHEMA name_list
4751                                 {
4752                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4753                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
4754                                         n->objtype = ACL_OBJECT_SEQUENCE;
4755                                         n->objs = $5;
4756                                         $$ = n;
4757                                 }
4758                         | ALL FUNCTIONS IN_P SCHEMA name_list
4759                                 {
4760                                         PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4761                                         n->targtype = ACL_TARGET_ALL_IN_SCHEMA;
4762                                         n->objtype = ACL_OBJECT_FUNCTION;
4763                                         n->objs = $5;
4764                                         $$ = n;
4765                                 }
4766                 ;
4767
4768
4769 grantee_list:
4770                         grantee                                                                 { $$ = list_make1($1); }
4771                         | grantee_list ',' grantee                              { $$ = lappend($1, $3); }
4772                 ;
4773
4774 grantee:        RoleId
4775                                 {
4776                                         PrivGrantee *n = makeNode(PrivGrantee);
4777                                         /* This hack lets us avoid reserving PUBLIC as a keyword*/
4778                                         if (strcmp($1, "public") == 0)
4779                                                 n->rolname = NULL;
4780                                         else
4781                                                 n->rolname = $1;
4782                                         $$ = (Node *)n;
4783                                 }
4784                         | GROUP_P RoleId
4785                                 {
4786                                         PrivGrantee *n = makeNode(PrivGrantee);
4787                                         /* Treat GROUP PUBLIC as a synonym for PUBLIC */
4788                                         if (strcmp($2, "public") == 0)
4789                                                 n->rolname = NULL;
4790                                         else
4791                                                 n->rolname = $2;
4792                                         $$ = (Node *)n;
4793                                 }
4794                 ;
4795
4796
4797 opt_grant_grant_option:
4798                         WITH GRANT OPTION { $$ = TRUE; }
4799                         | /*EMPTY*/ { $$ = FALSE; }
4800                 ;
4801
4802 function_with_argtypes_list:
4803                         function_with_argtypes                                  { $$ = list_make1($1); }
4804                         | function_with_argtypes_list ',' function_with_argtypes
4805                                                                                                         { $$ = lappend($1, $3); }
4806                 ;
4807
4808 function_with_argtypes:
4809                         func_name func_args
4810                                 {
4811                                         FuncWithArgs *n = makeNode(FuncWithArgs);
4812                                         n->funcname = $1;
4813                                         n->funcargs = extractArgTypes($2);
4814                                         $$ = n;
4815                                 }
4816                 ;
4817
4818 /*****************************************************************************
4819  *
4820  * GRANT and REVOKE ROLE statements
4821  *
4822  *****************************************************************************/
4823
4824 GrantRoleStmt:
4825                         GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4826                                 {
4827                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
4828                                         n->is_grant = true;
4829                                         n->granted_roles = $2;
4830                                         n->grantee_roles = $4;
4831                                         n->admin_opt = $5;
4832                                         n->grantor = $6;
4833                                         $$ = (Node*)n;
4834                                 }
4835                 ;
4836
4837 RevokeRoleStmt:
4838                         REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4839                                 {
4840                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
4841                                         n->is_grant = false;
4842                                         n->admin_opt = false;
4843                                         n->granted_roles = $2;
4844                                         n->grantee_roles = $4;
4845                                         n->behavior = $6;
4846                                         $$ = (Node*)n;
4847                                 }
4848                         | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4849                                 {
4850                                         GrantRoleStmt *n = makeNode(GrantRoleStmt);
4851                                         n->is_grant = false;
4852                                         n->admin_opt = true;
4853                                         n->granted_roles = $5;
4854                                         n->grantee_roles = $7;
4855                                         n->behavior = $9;
4856                                         $$ = (Node*)n;
4857                                 }
4858                 ;
4859
4860 opt_grant_admin_option: WITH ADMIN OPTION                               { $$ = TRUE; }
4861                         | /*EMPTY*/                                                                     { $$ = FALSE; }
4862                 ;
4863
4864 opt_granted_by: GRANTED BY RoleId                                               { $$ = $3; }
4865                         | /*EMPTY*/                                                                     { $$ = NULL; }
4866                 ;
4867
4868 /*****************************************************************************
4869  *
4870  * ALTER DEFAULT PRIVILEGES statement
4871  *
4872  *****************************************************************************/
4873
4874 AlterDefaultPrivilegesStmt:
4875                         ALTER DEFAULT PRIVILEGES DefACLOptionList DefACLAction
4876                                 {
4877                                         AlterDefaultPrivilegesStmt *n = makeNode(AlterDefaultPrivilegesStmt);
4878                                         n->options = $4;
4879                                         n->action = (GrantStmt *) $5;
4880                                         $$ = (Node*)n;
4881                                 }
4882                 ;
4883
4884 DefACLOptionList:
4885                         DefACLOptionList DefACLOption                   { $$ = lappend($1, $2); }
4886                         | /* EMPTY */                                                   { $$ = NIL; }
4887                 ;
4888
4889 DefACLOption:
4890                         IN_P SCHEMA name_list
4891                                 {
4892                                         $$ = makeDefElem("schemas", (Node *)$3);
4893                                 }
4894                         | FOR ROLE name_list
4895                                 {
4896                                         $$ = makeDefElem("roles", (Node *)$3);
4897                                 }
4898                         | FOR USER name_list
4899                                 {
4900                                         $$ = makeDefElem("roles", (Node *)$3);
4901                                 }
4902                 ;
4903
4904 /*
4905  * This should match GRANT/REVOKE, except that individual target objects
4906  * are not mentioned and we only allow a subset of object types.
4907  */
4908 DefACLAction:
4909                         GRANT privileges ON defacl_privilege_target TO grantee_list
4910                         opt_grant_grant_option
4911                                 {
4912                                         GrantStmt *n = makeNode(GrantStmt);
4913                                         n->is_grant = true;
4914                                         n->privileges = $2;
4915                                         n->targtype = ACL_TARGET_DEFAULTS;
4916                                         n->objtype = $4;
4917                                         n->objects = NIL;
4918                                         n->grantees = $6;
4919                                         n->grant_option = $7;
4920                                         $$ = (Node*)n;
4921                                 }
4922                         | REVOKE privileges ON defacl_privilege_target
4923                         FROM grantee_list opt_drop_behavior
4924                                 {
4925                                         GrantStmt *n = makeNode(GrantStmt);
4926                                         n->is_grant = false;
4927                                         n->grant_option = false;
4928                                         n->privileges = $2;
4929                                         n->targtype = ACL_TARGET_DEFAULTS;
4930                                         n->objtype = $4;
4931                                         n->objects = NIL;
4932                                         n->grantees = $6;
4933                                         n->behavior = $7;
4934                                         $$ = (Node *)n;
4935                                 }
4936                         | REVOKE GRANT OPTION FOR privileges ON defacl_privilege_target
4937                         FROM grantee_list opt_drop_behavior
4938                                 {
4939                                         GrantStmt *n = makeNode(GrantStmt);
4940                                         n->is_grant = false;
4941                                         n->grant_option = true;
4942                                         n->privileges = $5;
4943                                         n->targtype = ACL_TARGET_DEFAULTS;
4944                                         n->objtype = $7;
4945                                         n->objects = NIL;
4946                                         n->grantees = $9;
4947                                         n->behavior = $10;
4948                                         $$ = (Node *)n;
4949                                 }
4950                 ;
4951
4952 defacl_privilege_target:
4953                         TABLES                  { $$ = ACL_OBJECT_RELATION; }
4954                         | FUNCTIONS             { $$ = ACL_OBJECT_FUNCTION; }
4955                         | SEQUENCES             { $$ = ACL_OBJECT_SEQUENCE; }
4956                 ;
4957
4958
4959 /*****************************************************************************
4960  *
4961  *              QUERY: CREATE INDEX
4962  *
4963  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4964  * willing to make TABLESPACE a fully reserved word.
4965  *****************************************************************************/
4966
4967 IndexStmt:      CREATE opt_unique INDEX opt_concurrently opt_index_name
4968                         ON qualified_name access_method_clause '(' index_params ')'
4969                         opt_reloptions OptTableSpace where_clause
4970                                 {
4971                                         IndexStmt *n = makeNode(IndexStmt);
4972                                         n->unique = $2;
4973                                         n->concurrent = $4;
4974                                         n->idxname = $5;
4975                                         n->relation = $7;
4976                                         n->accessMethod = $8;
4977                                         n->indexParams = $10;
4978                                         n->options = $12;
4979                                         n->tableSpace = $13;
4980                                         n->whereClause = $14;
4981                                         $$ = (Node *)n;
4982                                 }
4983                 ;
4984
4985 opt_unique:
4986                         UNIQUE                                                                  { $$ = TRUE; }
4987                         | /*EMPTY*/                                                             { $$ = FALSE; }
4988                 ;
4989
4990 opt_concurrently:
4991                         CONCURRENTLY                                                    { $$ = TRUE; }
4992                         | /*EMPTY*/                                                             { $$ = FALSE; }
4993                 ;
4994
4995 opt_index_name:
4996                         index_name                                                              { $$ = $1; }
4997                         | /*EMPTY*/                                                             { $$ = NULL; }
4998                 ;
4999
5000 access_method_clause:
5001                         USING access_method                                             { $$ = $2; }
5002                         | /*EMPTY*/                                                             { $$ = DEFAULT_INDEX_TYPE; }
5003                 ;
5004
5005 index_params:   index_elem                                                      { $$ = list_make1($1); }
5006                         | index_params ',' index_elem                   { $$ = lappend($1, $3); }
5007                 ;
5008
5009 /*
5010  * Index attributes can be either simple column references, or arbitrary
5011  * expressions in parens.  For backwards-compatibility reasons, we allow
5012  * an expression that's just a function call to be written without parens.
5013  */
5014 index_elem:     ColId opt_class opt_asc_desc opt_nulls_order
5015                                 {
5016                                         $$ = makeNode(IndexElem);
5017                                         $$->name = $1;
5018                                         $$->expr = NULL;
5019                                         $$->indexcolname = NULL;
5020                                         $$->opclass = $2;
5021                                         $$->ordering = $3;
5022                                         $$->nulls_ordering = $4;
5023                                 }
5024                         | func_expr opt_class opt_asc_desc opt_nulls_order
5025                                 {
5026                                         $$ = makeNode(IndexElem);
5027                                         $$->name = NULL;
5028                                         $$->expr = $1;
5029                                         $$->indexcolname = NULL;
5030                                         $$->opclass = $2;
5031                                         $$->ordering = $3;
5032                                         $$->nulls_ordering = $4;
5033                                 }
5034                         | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
5035                                 {
5036                                         $$ = makeNode(IndexElem);
5037                                         $$->name = NULL;
5038                                         $$->expr = $2;
5039                                         $$->indexcolname = NULL;
5040                                         $$->opclass = $4;
5041                                         $$->ordering = $5;
5042                                         $$->nulls_ordering = $6;
5043                                 }
5044                 ;
5045
5046 opt_class:      any_name                                                                { $$ = $1; }
5047                         | USING any_name                                                { $$ = $2; }
5048                         | /*EMPTY*/                                                             { $$ = NIL; }
5049                 ;
5050
5051 opt_asc_desc: ASC                                                       { $$ = SORTBY_ASC; }
5052                         | DESC                                                  { $$ = SORTBY_DESC; }
5053                         | /*EMPTY*/                                             { $$ = SORTBY_DEFAULT; }
5054                 ;
5055
5056 opt_nulls_order: NULLS_FIRST                            { $$ = SORTBY_NULLS_FIRST; }
5057                         | NULLS_LAST                                    { $$ = SORTBY_NULLS_LAST; }
5058                         | /*EMPTY*/                                             { $$ = SORTBY_NULLS_DEFAULT; }
5059                 ;
5060
5061
5062 /*****************************************************************************
5063  *
5064  *              QUERY:
5065  *                              create [or replace] function <fname>
5066  *                                              [(<type-1> { , <type-n>})]
5067  *                                              returns <type-r>
5068  *                                              as <filename or code in language as appropriate>
5069  *                                              language <lang> [with parameters]
5070  *
5071  *****************************************************************************/
5072
5073 CreateFunctionStmt:
5074                         CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5075                         RETURNS func_return createfunc_opt_list opt_definition
5076                                 {
5077                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5078                                         n->replace = $2;
5079                                         n->funcname = $4;
5080                                         n->parameters = $5;
5081                                         n->returnType = $7;
5082                                         n->options = $8;
5083                                         n->withClause = $9;
5084                                         $$ = (Node *)n;
5085                                 }
5086                         | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5087                           RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
5088                                 {
5089                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5090                                         n->replace = $2;
5091                                         n->funcname = $4;
5092                                         n->parameters = mergeTableFuncParameters($5, $9);
5093                                         n->returnType = TableFuncTypeName($9);
5094                                         n->returnType->location = @7;
5095                                         n->options = $11;
5096                                         n->withClause = $12;
5097                                         $$ = (Node *)n;
5098                                 }
5099                         | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
5100                           createfunc_opt_list opt_definition
5101                                 {
5102                                         CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
5103                                         n->replace = $2;
5104                                         n->funcname = $4;
5105                                         n->parameters = $5;
5106                                         n->returnType = NULL;
5107                                         n->options = $6;
5108                                         n->withClause = $7;
5109                                         $$ = (Node *)n;
5110                                 }
5111                 ;
5112
5113 opt_or_replace:
5114                         OR REPLACE                                                              { $$ = TRUE; }
5115                         | /*EMPTY*/                                                             { $$ = FALSE; }
5116                 ;
5117
5118 func_args:      '(' func_args_list ')'                                  { $$ = $2; }
5119                         | '(' ')'                                                               { $$ = NIL; }
5120                 ;
5121
5122 func_args_list:
5123                         func_arg                                                                { $$ = list_make1($1); }
5124                         | func_args_list ',' func_arg                   { $$ = lappend($1, $3); }
5125                 ;
5126
5127 /*
5128  * func_args_with_defaults is separate because we only want to accept
5129  * defaults in CREATE FUNCTION, not in ALTER etc.
5130  */
5131 func_args_with_defaults:
5132                 '(' func_args_with_defaults_list ')'            { $$ = $2; }
5133                 | '(' ')'                                                                       { $$ = NIL; }
5134                 ;
5135
5136 func_args_with_defaults_list:
5137                 func_arg_with_default                                           { $$ = list_make1($1); }
5138                 | func_args_with_defaults_list ',' func_arg_with_default
5139                                                                                                         { $$ = lappend($1, $3); }
5140                 ;
5141
5142 /*
5143  * The style with arg_class first is SQL99 standard, but Oracle puts
5144  * param_name first; accept both since it's likely people will try both
5145  * anyway.  Don't bother trying to save productions by letting arg_class
5146  * have an empty alternative ... you'll get shift/reduce conflicts.
5147  *
5148  * We can catch over-specified arguments here if we want to,
5149  * but for now better to silently swallow typmod, etc.
5150  * - thomas 2000-03-22
5151  */
5152 func_arg:
5153                         arg_class param_name func_type
5154                                 {
5155                                         FunctionParameter *n = makeNode(FunctionParameter);
5156                                         n->name = $2;
5157                                         n->argType = $3;
5158                                         n->mode = $1;
5159                                         n->defexpr = NULL;
5160                                         $$ = n;
5161                                 }
5162                         | param_name arg_class func_type
5163                                 {
5164                                         FunctionParameter *n = makeNode(FunctionParameter);
5165                                         n->name = $1;
5166                                         n->argType = $3;
5167                                         n->mode = $2;
5168                                         n->defexpr = NULL;
5169                                         $$ = n;
5170                                 }
5171                         | param_name func_type
5172                                 {
5173                                         FunctionParameter *n = makeNode(FunctionParameter);
5174                                         n->name = $1;
5175                                         n->argType = $2;
5176                                         n->mode = FUNC_PARAM_IN;
5177                                         n->defexpr = NULL;
5178                                         $$ = n;
5179                                 }
5180                         | arg_class func_type
5181                                 {
5182                                         FunctionParameter *n = makeNode(FunctionParameter);
5183                                         n->name = NULL;
5184                                         n->argType = $2;
5185                                         n->mode = $1;
5186                                         n->defexpr = NULL;
5187                                         $$ = n;
5188                                 }
5189                         | func_type
5190                                 {
5191                                         FunctionParameter *n = makeNode(FunctionParameter);
5192                                         n->name = NULL;
5193                                         n->argType = $1;
5194                                         n->mode = FUNC_PARAM_IN;
5195                                         n->defexpr = NULL;
5196                                         $$ = n;
5197                                 }
5198                 ;
5199
5200 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
5201 arg_class:      IN_P                                                            { $$ = FUNC_PARAM_IN; }
5202                         | OUT_P                                                         { $$ = FUNC_PARAM_OUT; }
5203                         | INOUT                                                         { $$ = FUNC_PARAM_INOUT; }
5204                         | IN_P OUT_P                                            { $$ = FUNC_PARAM_INOUT; }
5205                         | VARIADIC                                                      { $$ = FUNC_PARAM_VARIADIC; }
5206                 ;
5207
5208 /*
5209  * Ideally param_name should be ColId, but that causes too many conflicts.
5210  */
5211 param_name:     type_function_name
5212                 ;
5213
5214 func_return:
5215                         func_type
5216                                 {
5217                                         /* We can catch over-specified results here if we want to,
5218                                          * but for now better to silently swallow typmod, etc.
5219                                          * - thomas 2000-03-22
5220                                          */
5221                                         $$ = $1;
5222                                 }
5223                 ;
5224
5225 /*
5226  * We would like to make the %TYPE productions here be ColId attrs etc,
5227  * but that causes reduce/reduce conflicts.  type_function_name
5228  * is next best choice.
5229  */
5230 func_type:      Typename                                                                { $$ = $1; }
5231                         | type_function_name attrs '%' TYPE_P
5232                                 {
5233                                         $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
5234                                         $$->pct_type = true;
5235                                         $$->location = @1;
5236                                 }
5237                         | SETOF type_function_name attrs '%' TYPE_P
5238                                 {
5239                                         $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
5240                                         $$->pct_type = true;
5241                                         $$->setof = TRUE;
5242                                         $$->location = @2;
5243                                 }
5244                 ;
5245
5246 func_arg_with_default:
5247                 func_arg
5248                             {
5249                                     $$ = $1;
5250                             }
5251                 | func_arg DEFAULT a_expr
5252                             {
5253                                     $$ = $1;
5254                                     $$->defexpr = $3;
5255                             }
5256                 | func_arg '=' a_expr
5257                             {
5258                                     $$ = $1;
5259                                     $$->defexpr = $3;
5260                             }
5261                 ;
5262
5263
5264 createfunc_opt_list:
5265                         /* Must be at least one to prevent conflict */
5266                         createfunc_opt_item                     { $$ = list_make1($1); }
5267                         | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
5268         ;
5269
5270 /*
5271  * Options common to both CREATE FUNCTION and ALTER FUNCTION
5272  */
5273 common_func_opt_item:
5274                         CALLED ON NULL_P INPUT_P
5275                                 {
5276                                         $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
5277                                 }
5278                         | RETURNS NULL_P ON NULL_P INPUT_P
5279                                 {
5280                                         $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
5281                                 }
5282                         | STRICT_P
5283                                 {
5284                                         $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
5285                                 }
5286                         | IMMUTABLE
5287                                 {
5288                                         $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
5289                                 }
5290                         | STABLE
5291                                 {
5292                                         $$ = makeDefElem("volatility", (Node *)makeString("stable"));
5293                                 }
5294                         | VOLATILE
5295                                 {
5296                                         $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
5297                                 }
5298                         | EXTERNAL SECURITY DEFINER
5299                                 {
5300                                         $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
5301                                 }
5302                         | EXTERNAL SECURITY INVOKER
5303                                 {
5304                                         $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
5305                                 }
5306                         | SECURITY DEFINER
5307                                 {
5308                                         $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
5309                                 }
5310                         | SECURITY INVOKER
5311                                 {
5312                                         $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
5313                                 }
5314                         | COST NumericOnly
5315                                 {
5316                                         $$ = makeDefElem("cost", (Node *)$2);
5317                                 }
5318                         | ROWS NumericOnly
5319                                 {
5320                                         $$ = makeDefElem("rows", (Node *)$2);
5321                                 }
5322                         | SetResetClause
5323                                 {
5324                                         /* we abuse the normal content of a DefElem here */
5325                                         $$ = makeDefElem("set", (Node *)$1);
5326                                 }
5327                 ;
5328
5329 createfunc_opt_item:
5330                         AS func_as
5331                                 {
5332                                         $$ = makeDefElem("as", (Node *)$2);
5333                                 }
5334                         | LANGUAGE ColId_or_Sconst
5335                                 {
5336                                         $$ = makeDefElem("language", (Node *)makeString($2));
5337                                 }
5338                         | WINDOW
5339                                 {
5340                                         $$ = makeDefElem("window", (Node *)makeInteger(TRUE));
5341                                 }
5342                         | common_func_opt_item
5343                                 {
5344                                         $$ = $1;
5345                                 }
5346                 ;
5347
5348 func_as:        Sconst                                          { $$ = list_make1(makeString($1)); }
5349                         | Sconst ',' Sconst
5350                                 {
5351                                         $$ = list_make2(makeString($1), makeString($3));
5352                                 }
5353                 ;
5354
5355 opt_definition:
5356                         WITH definition                                                 { $$ = $2; }
5357                         | /*EMPTY*/                                                             { $$ = NIL; }
5358                 ;
5359
5360 table_func_column:      param_name func_type
5361                                 {
5362                                         FunctionParameter *n = makeNode(FunctionParameter);
5363                                         n->name = $1;
5364                                         n->argType = $2;
5365                                         n->mode = FUNC_PARAM_TABLE;
5366                                         n->defexpr = NULL;
5367                                         $$ = n;
5368                                 }
5369                 ;
5370
5371 table_func_column_list:
5372                         table_func_column
5373                                 {
5374                                         $$ = list_make1($1);
5375                                 }
5376                         | table_func_column_list ',' table_func_column
5377                                 {
5378                                         $$ = lappend($1, $3);
5379                                 }
5380                 ;
5381
5382 /*****************************************************************************
5383  * ALTER FUNCTION
5384  *
5385  * RENAME and OWNER subcommands are already provided by the generic
5386  * ALTER infrastructure, here we just specify alterations that can
5387  * only be applied to functions.
5388  *
5389  *****************************************************************************/
5390 AlterFunctionStmt:
5391                         ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
5392                                 {
5393                                         AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
5394                                         n->func = $3;
5395                                         n->actions = $4;
5396                                         $$ = (Node *) n;
5397                                 }
5398                 ;
5399
5400 alterfunc_opt_list:
5401                         /* At least one option must be specified */
5402                         common_func_opt_item                                    { $$ = list_make1($1); }
5403                         | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
5404                 ;
5405
5406 /* Ignored, merely for SQL compliance */
5407 opt_restrict:
5408                         RESTRICT
5409                         | /* EMPTY */
5410                 ;
5411
5412
5413 /*****************************************************************************
5414  *
5415  *              QUERY:
5416  *
5417  *              DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
5418  *              DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
5419  *              DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
5420  *
5421  *****************************************************************************/
5422
5423 RemoveFuncStmt:
5424                         DROP FUNCTION func_name func_args opt_drop_behavior
5425                                 {
5426                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5427                                         n->kind = OBJECT_FUNCTION;
5428                                         n->name = $3;
5429                                         n->args = extractArgTypes($4);
5430                                         n->behavior = $5;
5431                                         n->missing_ok = false;
5432                                         $$ = (Node *)n;
5433                                 }
5434                         | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
5435                                 {
5436                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5437                                         n->kind = OBJECT_FUNCTION;
5438                                         n->name = $5;
5439                                         n->args = extractArgTypes($6);
5440                                         n->behavior = $7;
5441                                         n->missing_ok = true;
5442                                         $$ = (Node *)n;
5443                                 }
5444                 ;
5445
5446 RemoveAggrStmt:
5447                         DROP AGGREGATE func_name aggr_args opt_drop_behavior
5448                                 {
5449                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5450                                         n->kind = OBJECT_AGGREGATE;
5451                                         n->name = $3;
5452                                         n->args = $4;
5453                                         n->behavior = $5;
5454                                         n->missing_ok = false;
5455                                         $$ = (Node *)n;
5456                                 }
5457                         | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
5458                                 {
5459                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5460                                         n->kind = OBJECT_AGGREGATE;
5461                                         n->name = $5;
5462                                         n->args = $6;
5463                                         n->behavior = $7;
5464                                         n->missing_ok = true;
5465                                         $$ = (Node *)n;
5466                                 }
5467                 ;
5468
5469 RemoveOperStmt:
5470                         DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
5471                                 {
5472                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5473                                         n->kind = OBJECT_OPERATOR;
5474                                         n->name = $3;
5475                                         n->args = $4;
5476                                         n->behavior = $5;
5477                                         n->missing_ok = false;
5478                                         $$ = (Node *)n;
5479                                 }
5480                         | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
5481                                 {
5482                                         RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5483                                         n->kind = OBJECT_OPERATOR;
5484                                         n->name = $5;
5485                                         n->args = $6;
5486                                         n->behavior = $7;
5487                                         n->missing_ok = true;
5488                                         $$ = (Node *)n;
5489                                 }
5490                 ;
5491
5492 oper_argtypes:
5493                         '(' Typename ')'
5494                                 {
5495                                    ereport(ERROR,
5496                                                    (errcode(ERRCODE_SYNTAX_ERROR),
5497                                                         errmsg("missing argument"),
5498                                                         errhint("Use NONE to denote the missing argument of a unary operator."),
5499                                                         parser_errposition(@3)));
5500                                 }
5501                         | '(' Typename ',' Typename ')'
5502                                         { $$ = list_make2($2, $4); }
5503                         | '(' NONE ',' Typename ')'                                     /* left unary */
5504                                         { $$ = list_make2(NULL, $4); }
5505                         | '(' Typename ',' NONE ')'                                     /* right unary */
5506                                         { $$ = list_make2($2, NULL); }
5507                 ;
5508
5509 any_operator:
5510                         all_Op
5511                                         { $$ = list_make1(makeString($1)); }
5512                         | ColId '.' any_operator
5513                                         { $$ = lcons(makeString($1), $3); }
5514                 ;
5515
5516 /*****************************************************************************
5517  *
5518  *              DO <anonymous code block> [ LANGUAGE language ]
5519  *
5520  * We use a DefElem list for future extensibility, and to allow flexibility
5521  * in the clause order.
5522  *
5523  *****************************************************************************/
5524
5525 DoStmt: DO dostmt_opt_list
5526                                 {
5527                                         DoStmt *n = makeNode(DoStmt);
5528                                         n->args = $2;
5529                                         $$ = (Node *)n;
5530                                 }
5531                 ;
5532
5533 dostmt_opt_list:
5534                         dostmt_opt_item                                         { $$ = list_make1($1); }
5535                         | dostmt_opt_list dostmt_opt_item       { $$ = lappend($1, $2); }
5536                 ;
5537
5538 dostmt_opt_item:
5539                         Sconst
5540                                 {
5541                                         $$ = makeDefElem("as", (Node *)makeString($1));
5542                                 }
5543                         | LANGUAGE ColId_or_Sconst
5544                                 {
5545                                         $$ = makeDefElem("language", (Node *)makeString($2));
5546                                 }
5547                 ;
5548
5549 /*****************************************************************************
5550  *
5551  *              CREATE CAST / DROP CAST
5552  *
5553  *****************************************************************************/
5554
5555 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
5556                                         WITH FUNCTION function_with_argtypes cast_context
5557                                 {
5558                                         CreateCastStmt *n = makeNode(CreateCastStmt);
5559                                         n->sourcetype = $4;
5560                                         n->targettype = $6;
5561                                         n->func = $10;
5562                                         n->context = (CoercionContext) $11;
5563                                         n->inout = false;
5564                                         $$ = (Node *)n;
5565                                 }
5566                         | CREATE CAST '(' Typename AS Typename ')'
5567                                         WITHOUT FUNCTION cast_context
5568                                 {
5569                                         CreateCastStmt *n = makeNode(CreateCastStmt);
5570                                         n->sourcetype = $4;
5571                                         n->targettype = $6;
5572                                         n->func = NULL;
5573                                         n->context = (CoercionContext) $10;
5574                                         n->inout = false;
5575                                         $$ = (Node *)n;
5576                                 }
5577                         | CREATE CAST '(' Typename AS Typename ')'
5578                                         WITH INOUT cast_context
5579                                 {
5580                                         CreateCastStmt *n = makeNode(CreateCastStmt);
5581                                         n->sourcetype = $4;
5582                                         n->targettype = $6;
5583                                         n->func = NULL;
5584                                         n->context = (CoercionContext) $10;
5585                                         n->inout = true;
5586                                         $$ = (Node *)n;
5587                                 }
5588                 ;
5589
5590 cast_context:  AS IMPLICIT_P                                    { $$ = COERCION_IMPLICIT; }
5591                 | AS ASSIGNMENT                                                 { $$ = COERCION_ASSIGNMENT; }
5592                 | /*EMPTY*/                                                             { $$ = COERCION_EXPLICIT; }
5593                 ;
5594
5595
5596 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
5597                                 {
5598                                         DropCastStmt *n = makeNode(DropCastStmt);
5599                                         n->sourcetype = $5;
5600                                         n->targettype = $7;
5601                                         n->behavior = $9;
5602                                         n->missing_ok = $3;
5603                                         $$ = (Node *)n;
5604                                 }
5605                 ;
5606
5607 opt_if_exists: IF_P EXISTS                                              { $$ = TRUE; }
5608                 | /*EMPTY*/                                                             { $$ = FALSE; }
5609                 ;
5610
5611
5612 /*****************************************************************************
5613  *
5614  *              QUERY:
5615  *
5616  *              REINDEX type <name> [FORCE]
5617  *
5618  * FORCE no longer does anything, but we accept it for backwards compatibility
5619  *****************************************************************************/
5620
5621 ReindexStmt:
5622                         REINDEX reindex_type qualified_name opt_force
5623                                 {
5624                                         ReindexStmt *n = makeNode(ReindexStmt);
5625                                         n->kind = $2;
5626                                         n->relation = $3;
5627                                         n->name = NULL;
5628                                         $$ = (Node *)n;
5629                                 }
5630                         | REINDEX SYSTEM_P name opt_force
5631                                 {
5632                                         ReindexStmt *n = makeNode(ReindexStmt);
5633                                         n->kind = OBJECT_DATABASE;
5634                                         n->name = $3;
5635                                         n->relation = NULL;
5636                                         n->do_system = true;
5637                                         n->do_user = false;
5638                                         $$ = (Node *)n;
5639                                 }
5640                         | REINDEX DATABASE name opt_force
5641                                 {
5642                                         ReindexStmt *n = makeNode(ReindexStmt);
5643                                         n->kind = OBJECT_DATABASE;
5644                                         n->name = $3;
5645                                         n->relation = NULL;
5646                                         n->do_system = true;
5647                                         n->do_user = true;
5648                                         $$ = (Node *)n;
5649                                 }
5650                 ;
5651
5652 reindex_type:
5653                         INDEX                                                                   { $$ = OBJECT_INDEX; }
5654                         | TABLE                                                                 { $$ = OBJECT_TABLE; }
5655                 ;
5656
5657 opt_force:      FORCE                                                                   {  $$ = TRUE; }
5658                         | /* EMPTY */                                                   {  $$ = FALSE; }
5659                 ;
5660
5661
5662 /*****************************************************************************
5663  *
5664  * ALTER THING name RENAME TO newname
5665  *
5666  *****************************************************************************/
5667
5668 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
5669                                 {
5670                                         RenameStmt *n = makeNode(RenameStmt);
5671                                         n->renameType = OBJECT_AGGREGATE;
5672                                         n->object = $3;
5673                                         n->objarg = $4;
5674                                         n->newname = $7;
5675                                         $$ = (Node *)n;
5676                                 }
5677                         | ALTER CONVERSION_P any_name RENAME TO name
5678                                 {
5679                                         RenameStmt *n = makeNode(RenameStmt);
5680                                         n->renameType = OBJECT_CONVERSION;
5681                                         n->object = $3;
5682                                         n->newname = $6;
5683                                         $$ = (Node *)n;
5684                                 }
5685                         | ALTER DATABASE database_name RENAME TO database_name
5686                                 {
5687                                         RenameStmt *n = makeNode(RenameStmt);
5688                                         n->renameType = OBJECT_DATABASE;
5689                                         n->subname = $3;
5690                                         n->newname = $6;
5691                                         $$ = (Node *)n;
5692                                 }
5693                         | ALTER FUNCTION function_with_argtypes RENAME TO name
5694                                 {
5695                                         RenameStmt *n = makeNode(RenameStmt);
5696                                         n->renameType = OBJECT_FUNCTION;
5697                                         n->object = $3->funcname;
5698                                         n->objarg = $3->funcargs;
5699                                         n->newname = $6;
5700                                         $$ = (Node *)n;
5701                                 }
5702                         | ALTER GROUP_P RoleId RENAME TO RoleId
5703                                 {
5704                                         RenameStmt *n = makeNode(RenameStmt);
5705                                         n->renameType = OBJECT_ROLE;
5706                                         n->subname = $3;
5707                                         n->newname = $6;
5708                                         $$ = (Node *)n;
5709                                 }
5710                         | ALTER opt_procedural LANGUAGE name RENAME TO name
5711                                 {
5712                                         RenameStmt *n = makeNode(RenameStmt);
5713                                         n->renameType = OBJECT_LANGUAGE;
5714                                         n->subname = $4;
5715                                         n->newname = $7;
5716                                         $$ = (Node *)n;
5717                                 }
5718                         | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
5719                                 {
5720                                         RenameStmt *n = makeNode(RenameStmt);
5721                                         n->renameType = OBJECT_OPCLASS;
5722                                         n->object = $4;
5723                                         n->subname = $6;
5724                                         n->newname = $9;
5725                                         $$ = (Node *)n;
5726                                 }
5727                         | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
5728                                 {
5729                                         RenameStmt *n = makeNode(RenameStmt);
5730                                         n->renameType = OBJECT_OPFAMILY;
5731                                         n->object = $4;
5732                                         n->subname = $6;
5733                                         n->newname = $9;
5734                                         $$ = (Node *)n;
5735                                 }
5736                         | ALTER SCHEMA name RENAME TO name
5737                                 {
5738                                         RenameStmt *n = makeNode(RenameStmt);
5739                                         n->renameType = OBJECT_SCHEMA;
5740                                         n->subname = $3;
5741                                         n->newname = $6;
5742                                         $$ = (Node *)n;
5743                                 }
5744                         | ALTER TABLE relation_expr RENAME TO name
5745                                 {
5746                                         RenameStmt *n = makeNode(RenameStmt);
5747                                         n->renameType = OBJECT_TABLE;
5748                                         n->relation = $3;
5749                                         n->subname = NULL;
5750                                         n->newname = $6;
5751                                         $$ = (Node *)n;
5752                                 }
5753                         | ALTER SEQUENCE qualified_name RENAME TO name
5754                                 {
5755                                         RenameStmt *n = makeNode(RenameStmt);
5756                                         n->renameType = OBJECT_SEQUENCE;
5757                                         n->relation = $3;
5758                                         n->subname = NULL;
5759                                         n->newname = $6;
5760                                         $$ = (Node *)n;
5761                                 }
5762                         | ALTER VIEW qualified_name RENAME TO name
5763                                 {
5764                                         RenameStmt *n = makeNode(RenameStmt);
5765                                         n->renameType = OBJECT_VIEW;
5766                                         n->relation = $3;
5767                                         n->subname = NULL;
5768                                         n->newname = $6;
5769                                         $$ = (Node *)n;
5770                                 }
5771                         | ALTER INDEX qualified_name RENAME TO name
5772                                 {
5773                                         RenameStmt *n = makeNode(RenameStmt);
5774                                         n->renameType = OBJECT_INDEX;
5775                                         n->relation = $3;
5776                                         n->subname = NULL;
5777                                         n->newname = $6;
5778                                         $$ = (Node *)n;
5779                                 }
5780                         | ALTER TABLE relation_expr RENAME opt_column name TO name
5781                                 {
5782                                         RenameStmt *n = makeNode(RenameStmt);
5783                                         n->renameType = OBJECT_COLUMN;
5784                                         n->relation = $3;
5785                                         n->subname = $6;
5786                                         n->newname = $8;
5787                                         $$ = (Node *)n;
5788                                 }
5789                         | ALTER TRIGGER name ON qualified_name RENAME TO name
5790                                 {
5791                                         RenameStmt *n = makeNode(RenameStmt);
5792                                         n->renameType = OBJECT_TRIGGER;
5793                                         n->relation = $5;
5794                                         n->subname = $3;
5795                                         n->newname = $8;
5796                                         $$ = (Node *)n;
5797                                 }
5798                         | ALTER ROLE RoleId RENAME TO RoleId
5799                                 {
5800                                         RenameStmt *n = makeNode(RenameStmt);
5801                                         n->renameType = OBJECT_ROLE;
5802                                         n->subname = $3;
5803                                         n->newname = $6;
5804                                         $$ = (Node *)n;
5805                                 }
5806                         | ALTER USER RoleId RENAME TO RoleId
5807                                 {
5808                                         RenameStmt *n = makeNode(RenameStmt);
5809                                         n->renameType = OBJECT_ROLE;
5810                                         n->subname = $3;
5811                                         n->newname = $6;
5812                                         $$ = (Node *)n;
5813                                 }
5814                         | ALTER TABLESPACE name RENAME TO name
5815                                 {
5816                                         RenameStmt *n = makeNode(RenameStmt);
5817                                         n->renameType = OBJECT_TABLESPACE;
5818                                         n->subname = $3;
5819                                         n->newname = $6;
5820                                         $$ = (Node *)n;
5821                                 }
5822                         | ALTER TABLESPACE name SET reloptions
5823                                 {
5824                                         AlterTableSpaceOptionsStmt *n =
5825                                                 makeNode(AlterTableSpaceOptionsStmt);
5826                                         n->tablespacename = $3;
5827                                         n->options = $5;
5828                                         n->isReset = FALSE;
5829                                         $$ = (Node *)n;
5830                                 }
5831                         | ALTER TABLESPACE name RESET reloptions
5832                                 {
5833                                         AlterTableSpaceOptionsStmt *n =
5834                                                 makeNode(AlterTableSpaceOptionsStmt);
5835                                         n->tablespacename = $3;
5836                                         n->options = $5;
5837                                         n->isReset = TRUE;
5838                                         $$ = (Node *)n;
5839                                 }
5840                         | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
5841                                 {
5842                                         RenameStmt *n = makeNode(RenameStmt);
5843                                         n->renameType = OBJECT_TSPARSER;
5844                                         n->object = $5;
5845                                         n->newname = $8;
5846                                         $$ = (Node *)n;
5847                                 }
5848                         | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
5849                                 {
5850                                         RenameStmt *n = makeNode(RenameStmt);
5851                                         n->renameType = OBJECT_TSDICTIONARY;
5852                                         n->object = $5;
5853                                         n->newname = $8;
5854                                         $$ = (Node *)n;
5855                                 }
5856                         | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
5857                                 {
5858                                         RenameStmt *n = makeNode(RenameStmt);
5859                                         n->renameType = OBJECT_TSTEMPLATE;
5860                                         n->object = $5;
5861                                         n->newname = $8;
5862                                         $$ = (Node *)n;
5863                                 }
5864                         | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
5865                                 {
5866                                         RenameStmt *n = makeNode(RenameStmt);
5867                                         n->renameType = OBJECT_TSCONFIGURATION;
5868                                         n->object = $5;
5869                                         n->newname = $8;
5870                                         $$ = (Node *)n;
5871                                 }
5872                         | ALTER TYPE_P any_name RENAME TO name
5873                                 {
5874                                         RenameStmt *n = makeNode(RenameStmt);
5875                                         n->renameType = OBJECT_TYPE;
5876                                         n->object = $3;
5877                                         n->newname = $6;
5878                                         $$ = (Node *)n;
5879                                 }
5880                         | ALTER TYPE_P any_name RENAME ATTRIBUTE name TO name
5881                                 {
5882                                         RenameStmt *n = makeNode(RenameStmt);
5883                                         n->renameType = OBJECT_ATTRIBUTE;
5884                                         n->relation = makeRangeVarFromAnyName($3, @3, yyscanner);
5885                                         n->subname = $6;
5886                                         n->newname = $8;
5887                                         $$ = (Node *)n;
5888                                 }
5889                 ;
5890
5891 opt_column: COLUMN                                                                      { $$ = COLUMN; }
5892                         | /*EMPTY*/                                                             { $$ = 0; }
5893                 ;
5894
5895 opt_set_data: SET DATA_P                                                                        { $$ = 1; }
5896                         | /*EMPTY*/                                                             { $$ = 0; }
5897                 ;
5898
5899 /*****************************************************************************
5900  *
5901  * ALTER THING name SET SCHEMA name
5902  *
5903  *****************************************************************************/
5904
5905 AlterObjectSchemaStmt:
5906                         ALTER AGGREGATE func_name aggr_args SET SCHEMA name
5907                                 {
5908                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5909                                         n->objectType = OBJECT_AGGREGATE;
5910                                         n->object = $3;
5911                                         n->objarg = $4;
5912                                         n->newschema = $7;
5913                                         $$ = (Node *)n;
5914                                 }
5915                         | ALTER DOMAIN_P any_name SET SCHEMA name
5916                                 {
5917                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5918                                         n->objectType = OBJECT_DOMAIN;
5919                                         n->object = $3;
5920                                         n->newschema = $6;
5921                                         $$ = (Node *)n;
5922                                 }
5923                         | ALTER FUNCTION function_with_argtypes SET SCHEMA name
5924                                 {
5925                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5926                                         n->objectType = OBJECT_FUNCTION;
5927                                         n->object = $3->funcname;
5928                                         n->objarg = $3->funcargs;
5929                                         n->newschema = $6;
5930                                         $$ = (Node *)n;
5931                                 }
5932                         | ALTER TABLE relation_expr SET SCHEMA name
5933                                 {
5934                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5935                                         n->objectType = OBJECT_TABLE;
5936                                         n->relation = $3;
5937                                         n->newschema = $6;
5938                                         $$ = (Node *)n;
5939                                 }
5940                         | ALTER SEQUENCE qualified_name SET SCHEMA name
5941                                 {
5942                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5943                                         n->objectType = OBJECT_SEQUENCE;
5944                                         n->relation = $3;
5945                                         n->newschema = $6;
5946                                         $$ = (Node *)n;
5947                                 }
5948                         | ALTER VIEW qualified_name SET SCHEMA name
5949                                 {
5950                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5951                                         n->objectType = OBJECT_VIEW;
5952                                         n->relation = $3;
5953                                         n->newschema = $6;
5954                                         $$ = (Node *)n;
5955                                 }
5956                         | ALTER TYPE_P any_name SET SCHEMA name
5957                                 {
5958                                         AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5959                                         n->objectType = OBJECT_TYPE;
5960                                         n->object = $3;
5961                                         n->newschema = $6;
5962                                         $$ = (Node *)n;
5963                                 }
5964                 ;
5965
5966 /*****************************************************************************
5967  *
5968  * ALTER THING name OWNER TO newname
5969  *
5970  *****************************************************************************/
5971
5972 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5973                                 {
5974                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5975                                         n->objectType = OBJECT_AGGREGATE;
5976                                         n->object = $3;
5977                                         n->objarg = $4;
5978                                         n->newowner = $7;
5979                                         $$ = (Node *)n;
5980                                 }
5981                         | ALTER CONVERSION_P any_name OWNER TO RoleId
5982                                 {
5983                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5984                                         n->objectType = OBJECT_CONVERSION;
5985                                         n->object = $3;
5986                                         n->newowner = $6;
5987                                         $$ = (Node *)n;
5988                                 }
5989                         | ALTER DATABASE database_name OWNER TO RoleId
5990                                 {
5991                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5992                                         n->objectType = OBJECT_DATABASE;
5993                                         n->object = list_make1(makeString($3));
5994                                         n->newowner = $6;
5995                                         $$ = (Node *)n;
5996                                 }
5997                         | ALTER DOMAIN_P any_name OWNER TO RoleId
5998                                 {
5999                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6000                                         n->objectType = OBJECT_DOMAIN;
6001                                         n->object = $3;
6002                                         n->newowner = $6;
6003                                         $$ = (Node *)n;
6004                                 }
6005                         | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
6006                                 {
6007                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6008                                         n->objectType = OBJECT_FUNCTION;
6009                                         n->object = $3->funcname;
6010                                         n->objarg = $3->funcargs;
6011                                         n->newowner = $6;
6012                                         $$ = (Node *)n;
6013                                 }
6014                         | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
6015                                 {
6016                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6017                                         n->objectType = OBJECT_LANGUAGE;
6018                                         n->object = list_make1(makeString($4));
6019                                         n->newowner = $7;
6020                                         $$ = (Node *)n;
6021                                 }
6022                         | ALTER LARGE_P OBJECT_P NumericOnly OWNER TO RoleId
6023                                 {
6024                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6025                                         n->objectType = OBJECT_LARGEOBJECT;
6026                                         n->object = list_make1($4);
6027                                         n->newowner = $7;
6028                                         $$ = (Node *)n;
6029                                 }
6030                         | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
6031                                 {
6032                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6033                                         n->objectType = OBJECT_OPERATOR;
6034                                         n->object = $3;
6035                                         n->objarg = $4;
6036                                         n->newowner = $7;
6037                                         $$ = (Node *)n;
6038                                 }
6039                         | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
6040                                 {
6041                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6042                                         n->objectType = OBJECT_OPCLASS;
6043                                         n->object = $4;
6044                                         n->addname = $6;
6045                                         n->newowner = $9;
6046                                         $$ = (Node *)n;
6047                                 }
6048                         | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
6049                                 {
6050                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6051                                         n->objectType = OBJECT_OPFAMILY;
6052                                         n->object = $4;
6053                                         n->addname = $6;
6054                                         n->newowner = $9;
6055                                         $$ = (Node *)n;
6056                                 }
6057                         | ALTER SCHEMA name OWNER TO RoleId
6058                                 {
6059                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6060                                         n->objectType = OBJECT_SCHEMA;
6061                                         n->object = list_make1(makeString($3));
6062                                         n->newowner = $6;
6063                                         $$ = (Node *)n;
6064                                 }
6065                         | ALTER TYPE_P any_name OWNER TO RoleId
6066                                 {
6067                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6068                                         n->objectType = OBJECT_TYPE;
6069                                         n->object = $3;
6070                                         n->newowner = $6;
6071                                         $$ = (Node *)n;
6072                                 }
6073                         | ALTER TABLESPACE name OWNER TO RoleId
6074                                 {
6075                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6076                                         n->objectType = OBJECT_TABLESPACE;
6077                                         n->object = list_make1(makeString($3));
6078                                         n->newowner = $6;
6079                                         $$ = (Node *)n;
6080                                 }
6081                         | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
6082                                 {
6083                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6084                                         n->objectType = OBJECT_TSDICTIONARY;
6085                                         n->object = $5;
6086                                         n->newowner = $8;
6087                                         $$ = (Node *)n;
6088                                 }
6089                         | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
6090                                 {
6091                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6092                                         n->objectType = OBJECT_TSCONFIGURATION;
6093                                         n->object = $5;
6094                                         n->newowner = $8;
6095                                         $$ = (Node *)n;
6096                                 }
6097                         | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
6098                                 {
6099                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6100                                         n->objectType = OBJECT_FDW;
6101                                         n->object = list_make1(makeString($5));
6102                                         n->newowner = $8;
6103                                         $$ = (Node *)n;
6104                                 }
6105                         | ALTER SERVER name OWNER TO RoleId
6106                                 {
6107                                         AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
6108                                         n->objectType = OBJECT_FOREIGN_SERVER;
6109                                         n->object = list_make1(makeString($3));
6110                                         n->newowner = $6;
6111                                         $$ = (Node *)n;
6112                                 }
6113                 ;
6114
6115
6116 /*****************************************************************************
6117  *
6118  *              QUERY:  Define Rewrite Rule
6119  *
6120  *****************************************************************************/
6121
6122 RuleStmt:       CREATE opt_or_replace RULE name AS
6123                         ON event TO qualified_name where_clause
6124                         DO opt_instead RuleActionList
6125                                 {
6126                                         RuleStmt *n = makeNode(RuleStmt);
6127                                         n->replace = $2;
6128                                         n->relation = $9;
6129                                         n->rulename = $4;
6130                                         n->whereClause = $10;
6131                                         n->event = $7;
6132                                         n->instead = $12;
6133                                         n->actions = $13;
6134                                         $$ = (Node *)n;
6135                                 }
6136                 ;
6137
6138 RuleActionList:
6139                         NOTHING                                                                 { $$ = NIL; }
6140                         | RuleActionStmt                                                { $$ = list_make1($1); }
6141                         | '(' RuleActionMulti ')'                               { $$ = $2; }
6142                 ;
6143
6144 /* the thrashing around here is to discard "empty" statements... */
6145 RuleActionMulti:
6146                         RuleActionMulti ';' RuleActionStmtOrEmpty
6147                                 { if ($3 != NULL)
6148                                         $$ = lappend($1, $3);
6149                                   else
6150                                         $$ = $1;
6151                                 }
6152                         | RuleActionStmtOrEmpty
6153                                 { if ($1 != NULL)
6154                                         $$ = list_make1($1);
6155                                   else
6156                                         $$ = NIL;
6157                                 }
6158                 ;
6159
6160 RuleActionStmt:
6161                         SelectStmt
6162                         | InsertStmt
6163                         | UpdateStmt
6164                         | DeleteStmt
6165                         | NotifyStmt
6166                 ;
6167
6168 RuleActionStmtOrEmpty:
6169                         RuleActionStmt                                                  { $$ = $1; }
6170                         |       /*EMPTY*/                                                       { $$ = NULL; }
6171                 ;
6172
6173 event:          SELECT                                                                  { $$ = CMD_SELECT; }
6174                         | UPDATE                                                                { $$ = CMD_UPDATE; }
6175                         | DELETE_P                                                              { $$ = CMD_DELETE; }
6176                         | INSERT                                                                { $$ = CMD_INSERT; }
6177                  ;
6178
6179 opt_instead:
6180                         INSTEAD                                                                 { $$ = TRUE; }
6181                         | ALSO                                                                  { $$ = FALSE; }
6182                         | /*EMPTY*/                                                             { $$ = FALSE; }
6183                 ;
6184
6185
6186 DropRuleStmt:
6187                         DROP RULE name ON qualified_name opt_drop_behavior
6188                                 {
6189                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
6190                                         n->relation = $5;
6191                                         n->property = $3;
6192                                         n->behavior = $6;
6193                                         n->removeType = OBJECT_RULE;
6194                                         n->missing_ok = false;
6195                                         $$ = (Node *) n;
6196                                 }
6197                         | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
6198                                 {
6199                                         DropPropertyStmt *n = makeNode(DropPropertyStmt);
6200                                         n->relation = $7;
6201                                         n->property = $5;
6202                                         n->behavior = $8;
6203                                         n->removeType = OBJECT_RULE;
6204                                         n->missing_ok = true;
6205                                         $$ = (Node *) n;
6206                                 }
6207                 ;
6208
6209
6210 /*****************************************************************************
6211  *
6212  *              QUERY:
6213  *                              NOTIFY <identifier> can appear both in rule bodies and
6214  *                              as a query-level command
6215  *
6216  *****************************************************************************/
6217
6218 NotifyStmt: NOTIFY ColId notify_payload
6219                                 {
6220                                         NotifyStmt *n = makeNode(NotifyStmt);
6221                                         n->conditionname = $2;
6222                                         n->payload = $3;
6223                                         $$ = (Node *)n;
6224                                 }
6225                 ;
6226
6227 notify_payload:
6228                         ',' Sconst                                                      { $$ = $2; }
6229                         | /*EMPTY*/                                                     { $$ = NULL; }
6230                 ;
6231
6232 ListenStmt: LISTEN ColId
6233                                 {
6234                                         ListenStmt *n = makeNode(ListenStmt);
6235                                         n->conditionname = $2;
6236                                         $$ = (Node *)n;
6237                                 }
6238                 ;
6239
6240 UnlistenStmt:
6241                         UNLISTEN ColId
6242                                 {
6243                                         UnlistenStmt *n = makeNode(UnlistenStmt);
6244                                         n->conditionname = $2;
6245                                         $$ = (Node *)n;
6246                                 }
6247                         | UNLISTEN '*'
6248                                 {
6249                                         UnlistenStmt *n = makeNode(UnlistenStmt);
6250                                         n->conditionname = NULL;
6251                                         $$ = (Node *)n;
6252                                 }
6253                 ;
6254
6255
6256 /*****************************************************************************
6257  *
6258  *              Transactions:
6259  *
6260  *              BEGIN / COMMIT / ROLLBACK
6261  *              (also older versions END / ABORT)
6262  *
6263  *****************************************************************************/
6264
6265 TransactionStmt:
6266                         ABORT_P opt_transaction
6267                                 {
6268                                         TransactionStmt *n = makeNode(TransactionStmt);
6269                                         n->kind = TRANS_STMT_ROLLBACK;
6270                                         n->options = NIL;
6271                                         $$ = (Node *)n;
6272                                 }
6273                         | BEGIN_P opt_transaction transaction_mode_list_or_empty
6274                                 {
6275                                         TransactionStmt *n = makeNode(TransactionStmt);
6276                                         n->kind = TRANS_STMT_BEGIN;
6277                                         n->options = $3;
6278                                         $$ = (Node *)n;
6279                                 }
6280                         | START TRANSACTION transaction_mode_list_or_empty
6281                                 {
6282                                         TransactionStmt *n = makeNode(TransactionStmt);
6283                                         n->kind = TRANS_STMT_START;
6284                                         n->options = $3;
6285                                         $$ = (Node *)n;
6286                                 }
6287                         | COMMIT opt_transaction
6288                                 {
6289                                         TransactionStmt *n = makeNode(TransactionStmt);
6290                                         n->kind = TRANS_STMT_COMMIT;
6291                                         n->options = NIL;
6292                                         $$ = (Node *)n;
6293                                 }
6294                         | END_P opt_transaction
6295                                 {
6296                                         TransactionStmt *n = makeNode(TransactionStmt);
6297                                         n->kind = TRANS_STMT_COMMIT;
6298                                         n->options = NIL;
6299                                         $$ = (Node *)n;
6300                                 }
6301                         | ROLLBACK opt_transaction
6302                                 {
6303                                         TransactionStmt *n = makeNode(TransactionStmt);
6304                                         n->kind = TRANS_STMT_ROLLBACK;
6305                                         n->options = NIL;
6306                                         $$ = (Node *)n;
6307                                 }
6308                         | SAVEPOINT ColId
6309                                 {
6310                                         TransactionStmt *n = makeNode(TransactionStmt);
6311                                         n->kind = TRANS_STMT_SAVEPOINT;
6312                                         n->options = list_make1(makeDefElem("savepoint_name",
6313                                                                                                                 (Node *)makeString($2)));
6314                                         $$ = (Node *)n;
6315                                 }
6316                         | RELEASE SAVEPOINT ColId
6317                                 {
6318                                         TransactionStmt *n = makeNode(TransactionStmt);
6319                                         n->kind = TRANS_STMT_RELEASE;
6320                                         n->options = list_make1(makeDefElem("savepoint_name",
6321                                                                                                                 (Node *)makeString($3)));
6322                                         $$ = (Node *)n;
6323                                 }
6324                         | RELEASE ColId
6325                                 {
6326                                         TransactionStmt *n = makeNode(TransactionStmt);
6327                                         n->kind = TRANS_STMT_RELEASE;
6328                                         n->options = list_make1(makeDefElem("savepoint_name",
6329                                                                                                                 (Node *)makeString($2)));
6330                                         $$ = (Node *)n;
6331                                 }
6332                         | ROLLBACK opt_transaction TO SAVEPOINT ColId
6333                                 {
6334                                         TransactionStmt *n = makeNode(TransactionStmt);
6335                                         n->kind = TRANS_STMT_ROLLBACK_TO;
6336                                         n->options = list_make1(makeDefElem("savepoint_name",
6337                                                                                                                 (Node *)makeString($5)));
6338                                         $$ = (Node *)n;
6339                                 }
6340                         | ROLLBACK opt_transaction TO ColId
6341                                 {
6342                                         TransactionStmt *n = makeNode(TransactionStmt);
6343                                         n->kind = TRANS_STMT_ROLLBACK_TO;
6344                                         n->options = list_make1(makeDefElem("savepoint_name",
6345                                                                                                                 (Node *)makeString($4)));
6346                                         $$ = (Node *)n;
6347                                 }
6348                         | PREPARE TRANSACTION Sconst
6349                                 {
6350                                         TransactionStmt *n = makeNode(TransactionStmt);
6351                                         n->kind = TRANS_STMT_PREPARE;
6352                                         n->gid = $3;
6353                                         $$ = (Node *)n;
6354                                 }
6355                         | COMMIT PREPARED Sconst
6356                                 {
6357                                         TransactionStmt *n = makeNode(TransactionStmt);
6358                                         n->kind = TRANS_STMT_COMMIT_PREPARED;
6359                                         n->gid = $3;
6360                                         $$ = (Node *)n;
6361                                 }
6362                         | ROLLBACK PREPARED Sconst
6363                                 {
6364                                         TransactionStmt *n = makeNode(TransactionStmt);
6365                                         n->kind = TRANS_STMT_ROLLBACK_PREPARED;
6366                                         n->gid = $3;
6367                                         $$ = (Node *)n;
6368                                 }
6369                 ;
6370
6371 opt_transaction:        WORK                                                    {}
6372                         | TRANSACTION                                                   {}
6373                         | /*EMPTY*/                                                             {}
6374                 ;
6375
6376 transaction_mode_item:
6377                         ISOLATION LEVEL iso_level
6378                                         { $$ = makeDefElem("transaction_isolation",
6379                                                                            makeStringConst($3, @3)); }
6380                         | READ ONLY
6381                                         { $$ = makeDefElem("transaction_read_only",
6382                                                                            makeIntConst(TRUE, @1)); }
6383                         | READ WRITE
6384                                         { $$ = makeDefElem("transaction_read_only",
6385                                                                            makeIntConst(FALSE, @1)); }
6386                 ;
6387
6388 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
6389 transaction_mode_list:
6390                         transaction_mode_item
6391                                         { $$ = list_make1($1); }
6392                         | transaction_mode_list ',' transaction_mode_item
6393                                         { $$ = lappend($1, $3); }
6394                         | transaction_mode_list transaction_mode_item
6395                                         { $$ = lappend($1, $2); }
6396                 ;
6397
6398 transaction_mode_list_or_empty:
6399                         transaction_mode_list
6400                         | /* EMPTY */
6401                                         { $$ = NIL; }
6402                 ;
6403
6404
6405 /*****************************************************************************
6406  *
6407  *      QUERY:
6408  *              CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
6409  *                      AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
6410  *
6411  *****************************************************************************/
6412
6413 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
6414                                 AS SelectStmt opt_check_option
6415                                 {
6416                                         ViewStmt *n = makeNode(ViewStmt);
6417                                         n->view = $4;
6418                                         n->view->istemp = $2;
6419                                         n->aliases = $5;
6420                                         n->query = $7;
6421                                         n->replace = false;
6422                                         $$ = (Node *) n;
6423                                 }
6424                 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
6425                                 AS SelectStmt opt_check_option
6426                                 {
6427                                         ViewStmt *n = makeNode(ViewStmt);
6428                                         n->view = $6;
6429                                         n->view->istemp = $4;
6430                                         n->aliases = $7;
6431                                         n->query = $9;
6432                                         n->replace = true;
6433                                         $$ = (Node *) n;
6434                                 }
6435                 ;
6436
6437 opt_check_option:
6438                 WITH CHECK OPTION
6439                                 {
6440                                         ereport(ERROR,
6441                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6442                                                          errmsg("WITH CHECK OPTION is not implemented")));
6443                                 }
6444                 | WITH CASCADED CHECK OPTION
6445                                 {
6446                                         ereport(ERROR,
6447                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6448                                                          errmsg("WITH CHECK OPTION is not implemented")));
6449                                 }
6450                 | WITH LOCAL CHECK OPTION
6451                                 {
6452                                         ereport(ERROR,
6453                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6454                                                          errmsg("WITH CHECK OPTION is not implemented")));
6455                                 }
6456                 | /* EMPTY */                                                   { $$ = NIL; }
6457                 ;
6458
6459 /*****************************************************************************
6460  *
6461  *              QUERY:
6462  *                              LOAD "filename"
6463  *
6464  *****************************************************************************/
6465
6466 LoadStmt:       LOAD file_name
6467                                 {
6468                                         LoadStmt *n = makeNode(LoadStmt);
6469                                         n->filename = $2;
6470                                         $$ = (Node *)n;
6471                                 }
6472                 ;
6473
6474
6475 /*****************************************************************************
6476  *
6477  *              CREATE DATABASE
6478  *
6479  *****************************************************************************/
6480
6481 CreatedbStmt:
6482                         CREATE DATABASE database_name opt_with createdb_opt_list
6483                                 {
6484                                         CreatedbStmt *n = makeNode(CreatedbStmt);
6485                                         n->dbname = $3;
6486                                         n->options = $5;
6487                                         $$ = (Node *)n;
6488                                 }
6489                 ;
6490
6491 createdb_opt_list:
6492                         createdb_opt_list createdb_opt_item             { $$ = lappend($1, $2); }
6493                         | /* EMPTY */                                                   { $$ = NIL; }
6494                 ;
6495
6496 createdb_opt_item:
6497                         TABLESPACE opt_equal name
6498                                 {
6499                                         $$ = makeDefElem("tablespace", (Node *)makeString($3));
6500                                 }
6501                         | TABLESPACE opt_equal DEFAULT
6502                                 {
6503                                         $$ = makeDefElem("tablespace", NULL);
6504                                 }
6505                         | LOCATION opt_equal Sconst
6506                                 {
6507                                         $$ = makeDefElem("location", (Node *)makeString($3));
6508                                 }
6509                         | LOCATION opt_equal DEFAULT
6510                                 {
6511                                         $$ = makeDefElem("location", NULL);
6512                                 }
6513                         | TEMPLATE opt_equal name
6514                                 {
6515                                         $$ = makeDefElem("template", (Node *)makeString($3));
6516                                 }
6517                         | TEMPLATE opt_equal DEFAULT
6518                                 {
6519                                         $$ = makeDefElem("template", NULL);
6520                                 }
6521                         | ENCODING opt_equal Sconst
6522                                 {
6523                                         $$ = makeDefElem("encoding", (Node *)makeString($3));
6524                                 }
6525                         | ENCODING opt_equal Iconst
6526                                 {
6527                                         $$ = makeDefElem("encoding", (Node *)makeInteger($3));
6528                                 }
6529                         | ENCODING opt_equal DEFAULT
6530                                 {
6531                                         $$ = makeDefElem("encoding", NULL);
6532                                 }
6533                         | LC_COLLATE_P opt_equal Sconst
6534                                 {
6535                                         $$ = makeDefElem("lc_collate", (Node *)makeString($3));
6536                                 }
6537                         | LC_COLLATE_P opt_equal DEFAULT
6538                                 {
6539                                         $$ = makeDefElem("lc_collate", NULL);
6540                                 }
6541                         | LC_CTYPE_P opt_equal Sconst
6542                                 {
6543                                         $$ = makeDefElem("lc_ctype", (Node *)makeString($3));
6544                                 }
6545                         | LC_CTYPE_P opt_equal DEFAULT
6546                                 {
6547                                         $$ = makeDefElem("lc_ctype", NULL);
6548                                 }
6549                         | CONNECTION LIMIT opt_equal SignedIconst
6550                                 {
6551                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6552                                 }
6553                         | OWNER opt_equal name
6554                                 {
6555                                         $$ = makeDefElem("owner", (Node *)makeString($3));
6556                                 }
6557                         | OWNER opt_equal DEFAULT
6558                                 {
6559                                         $$ = makeDefElem("owner", NULL);
6560                                 }
6561                 ;
6562
6563 /*
6564  *      Though the equals sign doesn't match other WITH options, pg_dump uses
6565  *      equals for backward compatibility, and it doesn't seem worth removing it.
6566  */
6567 opt_equal:      '='                                                                             {}
6568                         | /*EMPTY*/                                                             {}
6569                 ;
6570
6571
6572 /*****************************************************************************
6573  *
6574  *              ALTER DATABASE
6575  *
6576  *****************************************************************************/
6577
6578 AlterDatabaseStmt:
6579                         ALTER DATABASE database_name opt_with alterdb_opt_list
6580                                  {
6581                                         AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6582                                         n->dbname = $3;
6583                                         n->options = $5;
6584                                         $$ = (Node *)n;
6585                                  }
6586                         | ALTER DATABASE database_name SET TABLESPACE name
6587                                  {
6588                                         AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6589                                         n->dbname = $3;
6590                                         n->options = list_make1(makeDefElem("tablespace",
6591                                                                                                         (Node *)makeString($6)));
6592                                         $$ = (Node *)n;
6593                                  }
6594                 ;
6595
6596 AlterDatabaseSetStmt:
6597                         ALTER DATABASE database_name SetResetClause
6598                                 {
6599                                         AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
6600                                         n->dbname = $3;
6601                                         n->setstmt = $4;
6602                                         $$ = (Node *)n;
6603                                 }
6604                 ;
6605
6606
6607 alterdb_opt_list:
6608                         alterdb_opt_list alterdb_opt_item               { $$ = lappend($1, $2); }
6609                         | /* EMPTY */                                                   { $$ = NIL; }
6610                 ;
6611
6612 alterdb_opt_item:
6613                         CONNECTION LIMIT opt_equal SignedIconst
6614                                 {
6615                                         $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6616                                 }
6617                 ;
6618
6619
6620 /*****************************************************************************
6621  *
6622  *              DROP DATABASE [ IF EXISTS ]
6623  *
6624  * This is implicitly CASCADE, no need for drop behavior
6625  *****************************************************************************/
6626
6627 DropdbStmt: DROP DATABASE database_name
6628                                 {
6629                                         DropdbStmt *n = makeNode(DropdbStmt);
6630                                         n->dbname = $3;
6631                                         n->missing_ok = FALSE;
6632                                         $$ = (Node *)n;
6633                                 }
6634                         | DROP DATABASE IF_P EXISTS database_name
6635                                 {
6636                                         DropdbStmt *n = makeNode(DropdbStmt);
6637                                         n->dbname = $5;
6638                                         n->missing_ok = TRUE;
6639                                         $$ = (Node *)n;
6640                                 }
6641                 ;
6642
6643
6644 /*****************************************************************************
6645  *
6646  * Manipulate a domain
6647  *
6648  *****************************************************************************/
6649
6650 CreateDomainStmt:
6651                         CREATE DOMAIN_P any_name opt_as Typename ColQualList
6652                                 {
6653                                         CreateDomainStmt *n = makeNode(CreateDomainStmt);
6654                                         n->domainname = $3;
6655                                         n->typeName = $5;
6656                                         n->constraints = $6;
6657                                         $$ = (Node *)n;
6658                                 }
6659                 ;
6660
6661 AlterDomainStmt:
6662                         /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
6663                         ALTER DOMAIN_P any_name alter_column_default
6664                                 {
6665                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
6666                                         n->subtype = 'T';
6667                                         n->typeName = $3;
6668                                         n->def = $4;
6669                                         $$ = (Node *)n;
6670                                 }
6671                         /* ALTER DOMAIN <domain> DROP NOT NULL */
6672                         | ALTER DOMAIN_P any_name DROP NOT NULL_P
6673                                 {
6674                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
6675                                         n->subtype = 'N';
6676                                         n->typeName = $3;
6677                                         $$ = (Node *)n;
6678                                 }
6679                         /* ALTER DOMAIN <domain> SET NOT NULL */
6680                         | ALTER DOMAIN_P any_name SET NOT NULL_P
6681                                 {
6682                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
6683                                         n->subtype = 'O';
6684                                         n->typeName = $3;
6685                                         $$ = (Node *)n;
6686                                 }
6687                         /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
6688                         | ALTER DOMAIN_P any_name ADD_P TableConstraint
6689                                 {
6690                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
6691                                         n->subtype = 'C';
6692                                         n->typeName = $3;
6693                                         n->def = $5;
6694                                         $$ = (Node *)n;
6695                                 }
6696                         /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
6697                         | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
6698                                 {
6699                                         AlterDomainStmt *n = makeNode(AlterDomainStmt);
6700                                         n->subtype = 'X';
6701                                         n->typeName = $3;
6702                                         n->name = $6;
6703                                         n->behavior = $7;
6704                                         $$ = (Node *)n;
6705                                 }
6706                         ;
6707
6708 opt_as:         AS                                                                              {}
6709                         | /* EMPTY */                                                   {}
6710                 ;
6711
6712
6713 /*****************************************************************************
6714  *
6715  * Manipulate a text search dictionary or configuration
6716  *
6717  *****************************************************************************/
6718
6719 AlterTSDictionaryStmt:
6720                         ALTER TEXT_P SEARCH DICTIONARY any_name definition
6721                                 {
6722                                         AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
6723                                         n->dictname = $5;
6724                                         n->options = $6;
6725                                         $$ = (Node *)n;
6726                                 }
6727                 ;
6728
6729 AlterTSConfigurationStmt:
6730                         ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
6731                                 {
6732                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6733                                         n->cfgname = $5;
6734                                         n->tokentype = $9;
6735                                         n->dicts = $11;
6736                                         n->override = false;
6737                                         n->replace = false;
6738                                         $$ = (Node*)n;
6739                                 }
6740                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
6741                                 {
6742                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6743                                         n->cfgname = $5;
6744                                         n->tokentype = $9;
6745                                         n->dicts = $11;
6746                                         n->override = true;
6747                                         n->replace = false;
6748                                         $$ = (Node*)n;
6749                                 }
6750                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
6751                                 {
6752                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6753                                         n->cfgname = $5;
6754                                         n->tokentype = NIL;
6755                                         n->dicts = list_make2($9,$11);
6756                                         n->override = false;
6757                                         n->replace = true;
6758                                         $$ = (Node*)n;
6759                                 }
6760                         | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
6761                                 {
6762                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6763                                         n->cfgname = $5;
6764                                         n->tokentype = $9;
6765                                         n->dicts = list_make2($11,$13);
6766                                         n->override = false;
6767                                         n->replace = true;
6768                                         $$ = (Node*)n;
6769                                 }
6770                         | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
6771                                 {
6772                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6773                                         n->cfgname = $5;
6774                                         n->tokentype = $9;
6775                                         n->missing_ok = false;
6776                                         $$ = (Node*)n;
6777                                 }
6778                         | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
6779                                 {
6780                                         AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6781                                         n->cfgname = $5;
6782                                         n->tokentype = $11;
6783                                         n->missing_ok = true;
6784                                         $$ = (Node*)n;
6785                                 }
6786                 ;
6787
6788
6789 /*****************************************************************************
6790  *
6791  * Manipulate a conversion
6792  *
6793  *              CREATE [DEFAULT] CONVERSION <conversion_name>
6794  *              FOR <encoding_name> TO <encoding_name> FROM <func_name>
6795  *
6796  *****************************************************************************/
6797
6798 CreateConversionStmt:
6799                         CREATE opt_default CONVERSION_P any_name FOR Sconst
6800                         TO Sconst FROM any_name
6801                         {
6802                           CreateConversionStmt *n = makeNode(CreateConversionStmt);
6803                           n->conversion_name = $4;
6804                           n->for_encoding_name = $6;
6805                           n->to_encoding_name = $8;
6806                           n->func_name = $10;
6807                           n->def = $2;
6808                           $$ = (Node *)n;
6809                         }
6810                 ;
6811
6812 /*****************************************************************************
6813  *
6814  *              QUERY:
6815  *                              CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
6816  *                              CLUSTER [VERBOSE]
6817  *                              CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
6818  *
6819  *****************************************************************************/
6820
6821 ClusterStmt:
6822                         CLUSTER opt_verbose qualified_name cluster_index_specification
6823                                 {
6824                                ClusterStmt *n = makeNode(ClusterStmt);
6825                                    n->relation = $3;
6826                                    n->indexname = $4;
6827                                    n->verbose = $2;
6828                                    $$ = (Node*)n;
6829                                 }
6830                         | CLUSTER opt_verbose
6831                             {
6832                                    ClusterStmt *n = makeNode(ClusterStmt);
6833                                    n->relation = NULL;
6834                                    n->indexname = NULL;
6835                                    n->verbose = $2;
6836                                    $$ = (Node*)n;
6837                                 }
6838                         /* kept for pre-8.3 compatibility */
6839                         | CLUSTER opt_verbose index_name ON qualified_name
6840                                 {
6841                                    ClusterStmt *n = makeNode(ClusterStmt);
6842                                    n->relation = $5;
6843                                    n->indexname = $3;
6844                                    n->verbose = $2;
6845                                    $$ = (Node*)n;
6846                                 }
6847                 ;
6848
6849 cluster_index_specification:
6850                         USING index_name                { $$ = $2; }
6851                         | /*EMPTY*/                             { $$ = NULL; }
6852                 ;
6853
6854
6855 /*****************************************************************************
6856  *
6857  *              QUERY:
6858  *                              VACUUM
6859  *                              ANALYZE
6860  *
6861  *****************************************************************************/
6862
6863 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
6864                                 {
6865                                         VacuumStmt *n = makeNode(VacuumStmt);
6866                                         n->options = VACOPT_VACUUM;
6867                                         if ($2)
6868                                                 n->options |= VACOPT_FULL;
6869                                         if ($4)
6870                                                 n->options |= VACOPT_VERBOSE;
6871                                         n->freeze_min_age = $3 ? 0 : -1;
6872                                         n->freeze_table_age = $3 ? 0 : -1;
6873                                         n->relation = NULL;
6874                                         n->va_cols = NIL;
6875                                         $$ = (Node *)n;
6876                                 }
6877                         | VACUUM opt_full opt_freeze opt_verbose qualified_name
6878                                 {
6879                                         VacuumStmt *n = makeNode(VacuumStmt);
6880                                         n->options = VACOPT_VACUUM;
6881                                         if ($2)
6882                                                 n->options |= VACOPT_FULL;
6883                                         if ($4)
6884                                                 n->options |= VACOPT_VERBOSE;
6885                                         n->freeze_min_age = $3 ? 0 : -1;
6886                                         n->freeze_table_age = $3 ? 0 : -1;
6887                                         n->relation = $5;
6888                                         n->va_cols = NIL;
6889                                         $$ = (Node *)n;
6890                                 }
6891                         | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
6892                                 {
6893                                         VacuumStmt *n = (VacuumStmt *) $5;
6894                                         n->options |= VACOPT_VACUUM;
6895                                         if ($2)
6896                                                 n->options |= VACOPT_FULL;
6897                                         if ($4)
6898                                                 n->options |= VACOPT_VERBOSE;
6899                                         n->freeze_min_age = $3 ? 0 : -1;
6900                                         n->freeze_table_age = $3 ? 0 : -1;
6901                                         $$ = (Node *)n;
6902                                 }
6903                         | VACUUM '(' vacuum_option_list ')'
6904                                 {
6905                                         VacuumStmt *n = makeNode(VacuumStmt);
6906                                         n->options = VACOPT_VACUUM | $3;
6907                                         if (n->options & VACOPT_FREEZE)
6908                                                 n->freeze_min_age = n->freeze_table_age = 0;
6909                                         else
6910                                                 n->freeze_min_age = n->freeze_table_age = -1;
6911                                         n->relation = NULL;
6912                                         n->va_cols = NIL;
6913                                         $$ = (Node *) n;
6914                                 }
6915                         | VACUUM '(' vacuum_option_list ')' qualified_name opt_name_list
6916                                 {
6917                                         VacuumStmt *n = makeNode(VacuumStmt);
6918                                         n->options = VACOPT_VACUUM | $3;
6919                                         if (n->options & VACOPT_FREEZE)
6920                                                 n->freeze_min_age = n->freeze_table_age = 0;
6921                                         else
6922                                                 n->freeze_min_age = n->freeze_table_age = -1;
6923                                         n->relation = $5;
6924                                         n->va_cols = $6;
6925                                         if (n->va_cols != NIL)  /* implies analyze */
6926                                                 n->options |= VACOPT_ANALYZE;
6927                                         $$ = (Node *) n;
6928                                 }
6929                 ;
6930
6931 vacuum_option_list:
6932                         vacuum_option_elem                                                              { $$ = $1; }
6933                         | vacuum_option_list ',' vacuum_option_elem             { $$ = $1 | $3; }
6934                 ;
6935
6936 vacuum_option_elem:
6937                         analyze_keyword         { $$ = VACOPT_ANALYZE; }
6938                         | VERBOSE                       { $$ = VACOPT_VERBOSE; }
6939                         | FREEZE                        { $$ = VACOPT_FREEZE; }
6940                         | FULL                          { $$ = VACOPT_FULL; }
6941                 ;
6942
6943 AnalyzeStmt:
6944                         analyze_keyword opt_verbose
6945                                 {
6946                                         VacuumStmt *n = makeNode(VacuumStmt);
6947                                         n->options = VACOPT_ANALYZE;
6948                                         if ($2)
6949                                                 n->options |= VACOPT_VERBOSE;
6950                                         n->freeze_min_age = -1;
6951                                         n->freeze_table_age = -1;
6952                                         n->relation = NULL;
6953                                         n->va_cols = NIL;
6954                                         $$ = (Node *)n;
6955                                 }
6956                         | analyze_keyword opt_verbose qualified_name opt_name_list
6957                                 {
6958                                         VacuumStmt *n = makeNode(VacuumStmt);
6959                                         n->options = VACOPT_ANALYZE;
6960                                         if ($2)
6961                                                 n->options |= VACOPT_VERBOSE;
6962                                         n->freeze_min_age = -1;
6963                                         n->freeze_table_age = -1;
6964                                         n->relation = $3;
6965                                         n->va_cols = $4;
6966                                         $$ = (Node *)n;
6967                                 }
6968                 ;
6969
6970 analyze_keyword:
6971                         ANALYZE                                                                 {}
6972                         | ANALYSE /* British */                                 {}
6973                 ;
6974
6975 opt_verbose:
6976                         VERBOSE                                                                 { $$ = TRUE; }
6977                         | /*EMPTY*/                                                             { $$ = FALSE; }
6978                 ;
6979
6980 opt_full:       FULL                                                                    { $$ = TRUE; }
6981                         | /*EMPTY*/                                                             { $$ = FALSE; }
6982                 ;
6983
6984 opt_freeze: FREEZE                                                                      { $$ = TRUE; }
6985                         | /*EMPTY*/                                                             { $$ = FALSE; }
6986                 ;
6987
6988 opt_name_list:
6989                         '(' name_list ')'                                               { $$ = $2; }
6990                         | /*EMPTY*/                                                             { $$ = NIL; }
6991                 ;
6992
6993
6994 /*****************************************************************************
6995  *
6996  *              QUERY:
6997  *                              EXPLAIN [ANALYZE] [VERBOSE] query
6998  *                              EXPLAIN ( options ) query
6999  *
7000  *****************************************************************************/
7001
7002 ExplainStmt:
7003                 EXPLAIN ExplainableStmt
7004                                 {
7005                                         ExplainStmt *n = makeNode(ExplainStmt);
7006                                         n->query = $2;
7007                                         n->options = NIL;
7008                                         $$ = (Node *) n;
7009                                 }
7010                 | EXPLAIN analyze_keyword opt_verbose ExplainableStmt
7011                                 {
7012                                         ExplainStmt *n = makeNode(ExplainStmt);
7013                                         n->query = $4;
7014                                         n->options = list_make1(makeDefElem("analyze", NULL));
7015                                         if ($3)
7016                                                 n->options = lappend(n->options,
7017                                                                                          makeDefElem("verbose", NULL));
7018                                         $$ = (Node *) n;
7019                                 }
7020                 | EXPLAIN VERBOSE ExplainableStmt
7021                                 {
7022                                         ExplainStmt *n = makeNode(ExplainStmt);
7023                                         n->query = $3;
7024                                         n->options = list_make1(makeDefElem("verbose", NULL));
7025                                         $$ = (Node *) n;
7026                                 }
7027                 | EXPLAIN '(' explain_option_list ')' ExplainableStmt
7028                                 {
7029                                         ExplainStmt *n = makeNode(ExplainStmt);
7030                                         n->query = $5;
7031                                         n->options = $3;
7032                                         $$ = (Node *) n;
7033                                 }
7034                 ;
7035
7036 ExplainableStmt:
7037                         SelectStmt
7038                         | InsertStmt
7039                         | UpdateStmt
7040                         | DeleteStmt
7041                         | DeclareCursorStmt
7042                         | CreateAsStmt
7043                         | ExecuteStmt                                   /* by default all are $$=$1 */
7044                 ;
7045
7046 explain_option_list:
7047                         explain_option_elem
7048                                 {
7049                                         $$ = list_make1($1);
7050                                 }
7051                         | explain_option_list ',' explain_option_elem
7052                                 {
7053                                         $$ = lappend($1, $3);
7054                                 }
7055                 ;
7056
7057 explain_option_elem:
7058                         explain_option_name explain_option_arg
7059                                 {
7060                                         $$ = makeDefElem($1, $2);
7061                                 }
7062                 ;
7063
7064 explain_option_name:
7065                         ColId                                   { $$ = $1; }
7066                         | analyze_keyword               { $$ = "analyze"; }
7067                         | VERBOSE                               { $$ = "verbose"; }
7068                 ;
7069
7070 explain_option_arg:
7071                         opt_boolean                             { $$ = (Node *) makeString($1); }
7072                         | ColId_or_Sconst               { $$ = (Node *) makeString($1); }
7073                         | NumericOnly                   { $$ = (Node *) $1; }
7074                         | /* EMPTY */                   { $$ = NULL; }
7075                 ;
7076
7077 /*****************************************************************************
7078  *
7079  *              QUERY:
7080  *                              PREPARE <plan_name> [(args, ...)] AS <query>
7081  *
7082  *****************************************************************************/
7083
7084 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
7085                                 {
7086                                         PrepareStmt *n = makeNode(PrepareStmt);
7087                                         n->name = $2;
7088                                         n->argtypes = $3;
7089                                         n->query = $5;
7090                                         $$ = (Node *) n;
7091                                 }
7092                 ;
7093
7094 prep_type_clause: '(' type_list ')'                     { $$ = $2; }
7095                                 | /* EMPTY */                           { $$ = NIL; }
7096                 ;
7097
7098 PreparableStmt:
7099                         SelectStmt
7100                         | InsertStmt
7101                         | UpdateStmt
7102                         | DeleteStmt                                    /* by default all are $$=$1 */
7103                 ;
7104
7105 /*****************************************************************************
7106  *
7107  * EXECUTE <plan_name> [(params, ...)]
7108  * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
7109  *
7110  *****************************************************************************/
7111
7112 ExecuteStmt: EXECUTE name execute_param_clause
7113                                 {
7114                                         ExecuteStmt *n = makeNode(ExecuteStmt);
7115                                         n->name = $2;
7116                                         n->params = $3;
7117                                         n->into = NULL;
7118                                         $$ = (Node *) n;
7119                                 }
7120                         | CREATE OptTemp TABLE create_as_target AS
7121                                 EXECUTE name execute_param_clause
7122                                 {
7123                                         ExecuteStmt *n = makeNode(ExecuteStmt);
7124                                         n->name = $7;
7125                                         n->params = $8;
7126                                         $4->rel->istemp = $2;
7127                                         n->into = $4;
7128                                         if ($4->colNames)
7129                                                 ereport(ERROR,
7130                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7131                                                                  errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
7132                                         /* ... because it's not implemented, but it could be */
7133                                         $$ = (Node *) n;
7134                                 }
7135                 ;
7136
7137 execute_param_clause: '(' expr_list ')'                         { $$ = $2; }
7138                                         | /* EMPTY */                                   { $$ = NIL; }
7139                                         ;
7140
7141 /*****************************************************************************
7142  *
7143  *              QUERY:
7144  *                              DEALLOCATE [PREPARE] <plan_name>
7145  *
7146  *****************************************************************************/
7147
7148 DeallocateStmt: DEALLOCATE name
7149                                         {
7150                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
7151                                                 n->name = $2;
7152                                                 $$ = (Node *) n;
7153                                         }
7154                                 | DEALLOCATE PREPARE name
7155                                         {
7156                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
7157                                                 n->name = $3;
7158                                                 $$ = (Node *) n;
7159                                         }
7160                                 | DEALLOCATE ALL
7161                                         {
7162                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
7163                                                 n->name = NULL;
7164                                                 $$ = (Node *) n;
7165                                         }
7166                                 | DEALLOCATE PREPARE ALL
7167                                         {
7168                                                 DeallocateStmt *n = makeNode(DeallocateStmt);
7169                                                 n->name = NULL;
7170                                                 $$ = (Node *) n;
7171                                         }
7172                 ;
7173
7174 /*****************************************************************************
7175  *
7176  *              QUERY:
7177  *                              INSERT STATEMENTS
7178  *
7179  *****************************************************************************/
7180
7181 InsertStmt:
7182                         INSERT INTO qualified_name insert_rest returning_clause
7183                                 {
7184                                         $4->relation = $3;
7185                                         $4->returningList = $5;
7186                                         $$ = (Node *) $4;
7187                                 }
7188                 ;
7189
7190 insert_rest:
7191                         SelectStmt
7192                                 {
7193                                         $$ = makeNode(InsertStmt);
7194                                         $$->cols = NIL;
7195                                         $$->selectStmt = $1;
7196                                 }
7197                         | '(' insert_column_list ')' SelectStmt
7198                                 {
7199                                         $$ = makeNode(InsertStmt);
7200                                         $$->cols = $2;
7201                                         $$->selectStmt = $4;
7202                                 }
7203                         | DEFAULT VALUES
7204                                 {
7205                                         $$ = makeNode(InsertStmt);
7206                                         $$->cols = NIL;
7207                                         $$->selectStmt = NULL;
7208                                 }
7209                 ;
7210
7211 insert_column_list:
7212                         insert_column_item
7213                                         { $$ = list_make1($1); }
7214                         | insert_column_list ',' insert_column_item
7215                                         { $$ = lappend($1, $3); }
7216                 ;
7217
7218 insert_column_item:
7219                         ColId opt_indirection
7220                                 {
7221                                         $$ = makeNode(ResTarget);
7222                                         $$->name = $1;
7223                                         $$->indirection = check_indirection($2, yyscanner);
7224                                         $$->val = NULL;
7225                                         $$->location = @1;
7226                                 }
7227                 ;
7228
7229 returning_clause:
7230                         RETURNING target_list           { $$ = $2; }
7231                         | /* EMPTY */                           { $$ = NIL; }
7232                 ;
7233
7234
7235 /*****************************************************************************
7236  *
7237  *              QUERY:
7238  *                              DELETE STATEMENTS
7239  *
7240  *****************************************************************************/
7241
7242 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
7243                         using_clause where_or_current_clause returning_clause
7244                                 {
7245                                         DeleteStmt *n = makeNode(DeleteStmt);
7246                                         n->relation = $3;
7247                                         n->usingClause = $4;
7248                                         n->whereClause = $5;
7249                                         n->returningList = $6;
7250                                         $$ = (Node *)n;
7251                                 }
7252                 ;
7253
7254 using_clause:
7255                         USING from_list                                         { $$ = $2; }
7256                         | /*EMPTY*/                                                             { $$ = NIL; }
7257                 ;
7258
7259
7260 /*****************************************************************************
7261  *
7262  *              QUERY:
7263  *                              LOCK TABLE
7264  *
7265  *****************************************************************************/
7266
7267 LockStmt:       LOCK_P opt_table relation_expr_list opt_lock opt_nowait
7268                                 {
7269                                         LockStmt *n = makeNode(LockStmt);
7270
7271                                         n->relations = $3;
7272                                         n->mode = $4;
7273                                         n->nowait = $5;
7274                                         $$ = (Node *)n;
7275                                 }
7276                 ;
7277
7278 opt_lock:       IN_P lock_type MODE                     { $$ = $2; }
7279                         | /*EMPTY*/                                             { $$ = AccessExclusiveLock; }
7280                 ;
7281
7282 lock_type:      ACCESS SHARE                                    { $$ = AccessShareLock; }
7283                         | ROW SHARE                                             { $$ = RowShareLock; }
7284                         | ROW EXCLUSIVE                                 { $$ = RowExclusiveLock; }
7285                         | SHARE UPDATE EXCLUSIVE                { $$ = ShareUpdateExclusiveLock; }
7286                         | SHARE                                                 { $$ = ShareLock; }
7287                         | SHARE ROW EXCLUSIVE                   { $$ = ShareRowExclusiveLock; }
7288                         | EXCLUSIVE                                             { $$ = ExclusiveLock; }
7289                         | ACCESS EXCLUSIVE                              { $$ = AccessExclusiveLock; }
7290                 ;
7291
7292 opt_nowait:     NOWAIT                                                  { $$ = TRUE; }
7293                         | /*EMPTY*/                                             { $$ = FALSE; }
7294                 ;
7295
7296
7297 /*****************************************************************************
7298  *
7299  *              QUERY:
7300  *                              UpdateStmt (UPDATE)
7301  *
7302  *****************************************************************************/
7303
7304 UpdateStmt: UPDATE relation_expr_opt_alias
7305                         SET set_clause_list
7306                         from_clause
7307                         where_or_current_clause
7308                         returning_clause
7309                                 {
7310                                         UpdateStmt *n = makeNode(UpdateStmt);
7311                                         n->relation = $2;
7312                                         n->targetList = $4;
7313                                         n->fromClause = $5;
7314                                         n->whereClause = $6;
7315                                         n->returningList = $7;
7316                                         $$ = (Node *)n;
7317                                 }
7318                 ;
7319
7320 set_clause_list:
7321                         set_clause                                                      { $$ = $1; }
7322                         | set_clause_list ',' set_clause        { $$ = list_concat($1,$3); }
7323                 ;
7324
7325 set_clause:
7326                         single_set_clause                                               { $$ = list_make1($1); }
7327                         | multiple_set_clause                                   { $$ = $1; }
7328                 ;
7329
7330 single_set_clause:
7331                         set_target '=' ctext_expr
7332                                 {
7333                                         $$ = $1;
7334                                         $$->val = (Node *) $3;
7335                                 }
7336                 ;
7337
7338 multiple_set_clause:
7339                         '(' set_target_list ')' '=' ctext_row
7340                                 {
7341                                         ListCell *col_cell;
7342                                         ListCell *val_cell;
7343
7344                                         /*
7345                                          * Break the ctext_row apart, merge individual expressions
7346                                          * into the destination ResTargets.  XXX this approach
7347                                          * cannot work for general row expressions as sources.
7348                                          */
7349                                         if (list_length($2) != list_length($5))
7350                                                 ereport(ERROR,
7351                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
7352                                                                  errmsg("number of columns does not match number of values"),
7353                                                                  parser_errposition(@1)));
7354                                         forboth(col_cell, $2, val_cell, $5)
7355                                         {
7356                                                 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
7357                                                 Node *res_val = (Node *) lfirst(val_cell);
7358
7359                                                 res_col->val = res_val;
7360                                         }
7361
7362                                         $$ = $2;
7363                                 }
7364                 ;
7365
7366 set_target:
7367                         ColId opt_indirection
7368                                 {
7369                                         $$ = makeNode(ResTarget);
7370                                         $$->name = $1;
7371                                         $$->indirection = check_indirection($2, yyscanner);
7372                                         $$->val = NULL; /* upper production sets this */
7373                                         $$->location = @1;
7374                                 }
7375                 ;
7376
7377 set_target_list:
7378                         set_target                                                              { $$ = list_make1($1); }
7379                         | set_target_list ',' set_target                { $$ = lappend($1,$3); }
7380                 ;
7381
7382
7383 /*****************************************************************************
7384  *
7385  *              QUERY:
7386  *                              CURSOR STATEMENTS
7387  *
7388  *****************************************************************************/
7389 DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
7390                                 {
7391                                         DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
7392                                         n->portalname = $2;
7393                                         /* currently we always set FAST_PLAN option */
7394                                         n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
7395                                         n->query = $7;
7396                                         $$ = (Node *)n;
7397                                 }
7398                 ;
7399
7400 cursor_name:    name                                            { $$ = $1; }
7401                 ;
7402
7403 cursor_options: /*EMPTY*/                                       { $$ = 0; }
7404                         | cursor_options NO SCROLL              { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
7405                         | cursor_options SCROLL                 { $$ = $1 | CURSOR_OPT_SCROLL; }
7406                         | cursor_options BINARY                 { $$ = $1 | CURSOR_OPT_BINARY; }
7407                         | cursor_options INSENSITIVE    { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
7408                 ;
7409
7410 opt_hold: /* EMPTY */                                           { $$ = 0; }
7411                         | WITH HOLD                                             { $$ = CURSOR_OPT_HOLD; }
7412                         | WITHOUT HOLD                                  { $$ = 0; }
7413                 ;
7414
7415 /*****************************************************************************
7416  *
7417  *              QUERY:
7418  *                              SELECT STATEMENTS
7419  *
7420  *****************************************************************************/
7421
7422 /* A complete SELECT statement looks like this.
7423  *
7424  * The rule returns either a single SelectStmt node or a tree of them,
7425  * representing a set-operation tree.
7426  *
7427  * There is an ambiguity when a sub-SELECT is within an a_expr and there
7428  * are excess parentheses: do the parentheses belong to the sub-SELECT or
7429  * to the surrounding a_expr?  We don't really care, but bison wants to know.
7430  * To resolve the ambiguity, we are careful to define the grammar so that
7431  * the decision is staved off as long as possible: as long as we can keep
7432  * absorbing parentheses into the sub-SELECT, we will do so, and only when
7433  * it's no longer possible to do that will we decide that parens belong to
7434  * the expression.      For example, in "SELECT (((SELECT 2)) + 3)" the extra
7435  * parentheses are treated as part of the sub-select.  The necessity of doing
7436  * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".      Had we
7437  * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
7438  * SELECT viewpoint when we see the UNION.
7439  *
7440  * This approach is implemented by defining a nonterminal select_with_parens,
7441  * which represents a SELECT with at least one outer layer of parentheses,
7442  * and being careful to use select_with_parens, never '(' SelectStmt ')',
7443  * in the expression grammar.  We will then have shift-reduce conflicts
7444  * which we can resolve in favor of always treating '(' <select> ')' as
7445  * a select_with_parens.  To resolve the conflicts, the productions that
7446  * conflict with the select_with_parens productions are manually given
7447  * precedences lower than the precedence of ')', thereby ensuring that we
7448  * shift ')' (and then reduce to select_with_parens) rather than trying to
7449  * reduce the inner <select> nonterminal to something else.  We use UMINUS
7450  * precedence for this, which is a fairly arbitrary choice.
7451  *
7452  * To be able to define select_with_parens itself without ambiguity, we need
7453  * a nonterminal select_no_parens that represents a SELECT structure with no
7454  * outermost parentheses.  This is a little bit tedious, but it works.
7455  *
7456  * In non-expression contexts, we use SelectStmt which can represent a SELECT
7457  * with or without outer parentheses.
7458  */
7459
7460 SelectStmt: select_no_parens                    %prec UMINUS
7461                         | select_with_parens            %prec UMINUS
7462                 ;
7463
7464 select_with_parens:
7465                         '(' select_no_parens ')'                                { $$ = $2; }
7466                         | '(' select_with_parens ')'                    { $$ = $2; }
7467                 ;
7468
7469 /*
7470  * This rule parses the equivalent of the standard's <query expression>.
7471  * The duplicative productions are annoying, but hard to get rid of without
7472  * creating shift/reduce conflicts.
7473  *
7474  *      FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
7475  *      In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
7476  *      We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
7477  *      2002-08-28 bjm
7478  */
7479 select_no_parens:
7480                         simple_select                                           { $$ = $1; }
7481                         | select_clause sort_clause
7482                                 {
7483                                         insertSelectOptions((SelectStmt *) $1, $2, NIL,
7484                                                                                 NULL, NULL, NULL,
7485                                                                                 yyscanner);
7486                                         $$ = $1;
7487                                 }
7488                         | select_clause opt_sort_clause for_locking_clause opt_select_limit
7489                                 {
7490                                         insertSelectOptions((SelectStmt *) $1, $2, $3,
7491                                                                                 list_nth($4, 0), list_nth($4, 1),
7492                                                                                 NULL,
7493                                                                                 yyscanner);
7494                                         $$ = $1;
7495                                 }
7496                         | select_clause opt_sort_clause select_limit opt_for_locking_clause
7497                                 {
7498                                         insertSelectOptions((SelectStmt *) $1, $2, $4,
7499                                                                                 list_nth($3, 0), list_nth($3, 1),
7500                                                                                 NULL,
7501                                                                                 yyscanner);
7502                                         $$ = $1;
7503                                 }
7504                         | with_clause select_clause
7505                                 {
7506                                         insertSelectOptions((SelectStmt *) $2, NULL, NIL,
7507                                                                                 NULL, NULL,
7508                                                                                 $1,
7509                                                                                 yyscanner);
7510                                         $$ = $2;
7511                                 }
7512                         | with_clause select_clause sort_clause
7513                                 {
7514                                         insertSelectOptions((SelectStmt *) $2, $3, NIL,
7515                                                                                 NULL, NULL,
7516                                                                                 $1,
7517                                                                                 yyscanner);
7518                                         $$ = $2;
7519                                 }
7520                         | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
7521                                 {
7522                                         insertSelectOptions((SelectStmt *) $2, $3, $4,
7523                                                                                 list_nth($5, 0), list_nth($5, 1),
7524                                                                                 $1,
7525                                                                                 yyscanner);
7526                                         $$ = $2;
7527                                 }
7528                         | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
7529                                 {
7530                                         insertSelectOptions((SelectStmt *) $2, $3, $5,
7531                                                                                 list_nth($4, 0), list_nth($4, 1),
7532                                                                                 $1,
7533                                                                                 yyscanner);
7534                                         $$ = $2;
7535                                 }
7536                 ;
7537
7538 select_clause:
7539                         simple_select                                                   { $$ = $1; }
7540                         | select_with_parens                                    { $$ = $1; }
7541                 ;
7542
7543 /*
7544  * This rule parses SELECT statements that can appear within set operations,
7545  * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
7546  * the ordering of the set operations.  Without '(' and ')' we want the
7547  * operations to be ordered per the precedence specs at the head of this file.
7548  *
7549  * As with select_no_parens, simple_select cannot have outer parentheses,
7550  * but can have parenthesized subclauses.
7551  *
7552  * Note that sort clauses cannot be included at this level --- SQL92 requires
7553  *              SELECT foo UNION SELECT bar ORDER BY baz
7554  * to be parsed as
7555  *              (SELECT foo UNION SELECT bar) ORDER BY baz
7556  * not
7557  *              SELECT foo UNION (SELECT bar ORDER BY baz)
7558  * Likewise for WITH, FOR UPDATE and LIMIT.  Therefore, those clauses are
7559  * described as part of the select_no_parens production, not simple_select.
7560  * This does not limit functionality, because you can reintroduce these
7561  * clauses inside parentheses.
7562  *
7563  * NOTE: only the leftmost component SelectStmt should have INTO.
7564  * However, this is not checked by the grammar; parse analysis must check it.
7565  */
7566 simple_select:
7567                         SELECT opt_distinct target_list
7568                         into_clause from_clause where_clause
7569                         group_clause having_clause window_clause
7570                                 {
7571                                         SelectStmt *n = makeNode(SelectStmt);
7572                                         n->distinctClause = $2;
7573                                         n->targetList = $3;
7574                                         n->intoClause = $4;
7575                                         n->fromClause = $5;
7576                                         n->whereClause = $6;
7577                                         n->groupClause = $7;
7578                                         n->havingClause = $8;
7579                                         n->windowClause = $9;
7580                                         $$ = (Node *)n;
7581                                 }
7582                         | values_clause                                                 { $$ = $1; }
7583                         | TABLE relation_expr
7584                                 {
7585                                         /* same as SELECT * FROM relation_expr */
7586                                         ColumnRef *cr = makeNode(ColumnRef);
7587                                         ResTarget *rt = makeNode(ResTarget);
7588                                         SelectStmt *n = makeNode(SelectStmt);
7589
7590                                         cr->fields = list_make1(makeNode(A_Star));
7591                                         cr->location = -1;
7592
7593                                         rt->name = NULL;
7594                                         rt->indirection = NIL;
7595                                         rt->val = (Node *)cr;
7596                                         rt->location = -1;
7597
7598                                         n->targetList = list_make1(rt);
7599                                         n->fromClause = list_make1($2);
7600                                         $$ = (Node *)n;
7601                                 }
7602                         | select_clause UNION opt_all select_clause
7603                                 {
7604                                         $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
7605                                 }
7606                         | select_clause INTERSECT opt_all select_clause
7607                                 {
7608                                         $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
7609                                 }
7610                         | select_clause EXCEPT opt_all select_clause
7611                                 {
7612                                         $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
7613                                 }
7614                 ;
7615
7616 /*
7617  * SQL standard WITH clause looks like:
7618  *
7619  * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
7620  *              AS (query) [ SEARCH or CYCLE clause ]
7621  *
7622  * We don't currently support the SEARCH or CYCLE clause.
7623  */
7624 with_clause:
7625                 WITH cte_list
7626                         {
7627                                 $$ = makeNode(WithClause);
7628                                 $$->ctes = $2;
7629                                 $$->recursive = false;
7630                                 $$->location = @1;
7631                         }
7632                 | WITH RECURSIVE cte_list
7633                         {
7634                                 $$ = makeNode(WithClause);
7635                                 $$->ctes = $3;
7636                                 $$->recursive = true;
7637                                 $$->location = @1;
7638                         }
7639                 ;
7640
7641 cte_list:
7642                 common_table_expr                                               { $$ = list_make1($1); }
7643                 | cte_list ',' common_table_expr                { $$ = lappend($1, $3); }
7644                 ;
7645
7646 common_table_expr:  name opt_name_list AS select_with_parens
7647                         {
7648                                 CommonTableExpr *n = makeNode(CommonTableExpr);
7649                                 n->ctename = $1;
7650                                 n->aliascolnames = $2;
7651                                 n->ctequery = $4;
7652                                 n->location = @1;
7653                                 $$ = (Node *) n;
7654                         }
7655                 ;
7656
7657 into_clause:
7658                         INTO OptTempTableName
7659                                 {
7660                                         $$ = makeNode(IntoClause);
7661                                         $$->rel = $2;
7662                                         $$->colNames = NIL;
7663                                         $$->options = NIL;
7664                                         $$->onCommit = ONCOMMIT_NOOP;
7665                                         $$->tableSpaceName = NULL;
7666                                 }
7667                         | /*EMPTY*/
7668                                 { $$ = NULL; }
7669                 ;
7670
7671 /*
7672  * Redundancy here is needed to avoid shift/reduce conflicts,
7673  * since TEMP is not a reserved word.  See also OptTemp.
7674  */
7675 OptTempTableName:
7676                         TEMPORARY opt_table qualified_name
7677                                 {
7678                                         $$ = $3;
7679                                         $$->istemp = true;
7680                                 }
7681                         | TEMP opt_table qualified_name
7682                                 {
7683                                         $$ = $3;
7684                                         $$->istemp = true;
7685                                 }
7686                         | LOCAL TEMPORARY opt_table qualified_name
7687                                 {
7688                                         $$ = $4;
7689                                         $$->istemp = true;
7690                                 }
7691                         | LOCAL TEMP opt_table qualified_name
7692                                 {
7693                                         $$ = $4;
7694                                         $$->istemp = true;
7695                                 }
7696                         | GLOBAL TEMPORARY opt_table qualified_name
7697                                 {
7698                                         $$ = $4;
7699                                         $$->istemp = true;
7700                                 }
7701                         | GLOBAL TEMP opt_table qualified_name
7702                                 {
7703                                         $$ = $4;
7704                                         $$->istemp = true;
7705                                 }
7706                         | TABLE qualified_name
7707                                 {
7708                                         $$ = $2;
7709                                         $$->istemp = false;
7710                                 }
7711                         | qualified_name
7712                                 {
7713                                         $$ = $1;
7714                                         $$->istemp = false;
7715                                 }
7716                 ;
7717
7718 opt_table:      TABLE                                                                   {}
7719                         | /*EMPTY*/                                                             {}
7720                 ;
7721
7722 opt_all:        ALL                                                                             { $$ = TRUE; }
7723                         | DISTINCT                                                              { $$ = FALSE; }
7724                         | /*EMPTY*/                                                             { $$ = FALSE; }
7725                 ;
7726
7727 /* We use (NIL) as a placeholder to indicate that all target expressions
7728  * should be placed in the DISTINCT list during parsetree analysis.
7729  */
7730 opt_distinct:
7731                         DISTINCT                                                                { $$ = list_make1(NIL); }
7732                         | DISTINCT ON '(' expr_list ')'                 { $$ = $4; }
7733                         | ALL                                                                   { $$ = NIL; }
7734                         | /*EMPTY*/                                                             { $$ = NIL; }
7735                 ;
7736
7737 opt_sort_clause:
7738                         sort_clause                                                             { $$ = $1;}
7739                         | /*EMPTY*/                                                             { $$ = NIL; }
7740                 ;
7741
7742 sort_clause:
7743                         ORDER BY sortby_list                                    { $$ = $3; }
7744                 ;
7745
7746 sortby_list:
7747                         sortby                                                                  { $$ = list_make1($1); }
7748                         | sortby_list ',' sortby                                { $$ = lappend($1, $3); }
7749                 ;
7750
7751 sortby:         a_expr USING qual_all_Op opt_nulls_order
7752                                 {
7753                                         $$ = makeNode(SortBy);
7754                                         $$->node = $1;
7755                                         $$->sortby_dir = SORTBY_USING;
7756                                         $$->sortby_nulls = $4;
7757                                         $$->useOp = $3;
7758                                         $$->location = @3;
7759                                 }
7760                         | a_expr opt_asc_desc opt_nulls_order
7761                                 {
7762                                         $$ = makeNode(SortBy);
7763                                         $$->node = $1;
7764                                         $$->sortby_dir = $2;
7765                                         $$->sortby_nulls = $3;
7766                                         $$->useOp = NIL;
7767                                         $$->location = -1;              /* no operator */
7768                                 }
7769                 ;
7770
7771
7772 select_limit:
7773                         limit_clause offset_clause                      { $$ = list_make2($2, $1); }
7774                         | offset_clause limit_clause            { $$ = list_make2($1, $2); }
7775                         | limit_clause                                          { $$ = list_make2(NULL, $1); }
7776                         | offset_clause                                         { $$ = list_make2($1, NULL); }
7777                 ;
7778
7779 opt_select_limit:
7780                         select_limit                                            { $$ = $1; }
7781                         | /* EMPTY */                                           { $$ = list_make2(NULL,NULL); }
7782                 ;
7783
7784 limit_clause:
7785                         LIMIT select_limit_value
7786                                 { $$ = $2; }
7787                         | LIMIT select_limit_value ',' select_offset_value
7788                                 {
7789                                         /* Disabled because it was too confusing, bjm 2002-02-18 */
7790                                         ereport(ERROR,
7791                                                         (errcode(ERRCODE_SYNTAX_ERROR),
7792                                                          errmsg("LIMIT #,# syntax is not supported"),
7793                                                          errhint("Use separate LIMIT and OFFSET clauses."),
7794                                                          parser_errposition(@1)));
7795                                 }
7796                         /* SQL:2008 syntax */
7797                         | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7798                                 { $$ = $3; }
7799                 ;
7800
7801 offset_clause:
7802                         OFFSET select_offset_value
7803                                 { $$ = $2; }
7804                         /* SQL:2008 syntax */
7805                         | OFFSET select_offset_value2 row_or_rows
7806                                 { $$ = $2; }
7807                 ;
7808
7809 select_limit_value:
7810                         a_expr                                                                  { $$ = $1; }
7811                         | ALL
7812                                 {
7813                                         /* LIMIT ALL is represented as a NULL constant */
7814                                         $$ = makeNullAConst(@1);
7815                                 }
7816                 ;
7817
7818 select_offset_value:
7819                         a_expr                                                                  { $$ = $1; }
7820                 ;
7821
7822 /*
7823  * Allowing full expressions without parentheses causes various parsing
7824  * problems with the trailing ROW/ROWS key words.  SQL only calls for
7825  * constants, so we allow the rest only with parentheses.  If omitted,
7826  * default to 1.
7827  */
7828 opt_select_fetch_first_value:
7829                         SignedIconst                                            { $$ = makeIntConst($1, @1); }
7830                         | '(' a_expr ')'                                        { $$ = $2; }
7831                         | /*EMPTY*/                                                     { $$ = makeIntConst(1, -1); }
7832                 ;
7833
7834 /*
7835  * Again, the trailing ROW/ROWS in this case prevent the full expression
7836  * syntax.  c_expr is the best we can do.
7837  */
7838 select_offset_value2:
7839                         c_expr                                                                  { $$ = $1; }
7840                 ;
7841
7842 /* noise words */
7843 row_or_rows: ROW                                                                        { $$ = 0; }
7844                         | ROWS                                                                  { $$ = 0; }
7845                 ;
7846
7847 first_or_next: FIRST_P                                                          { $$ = 0; }
7848                         | NEXT                                                                  { $$ = 0; }
7849                 ;
7850
7851
7852 group_clause:
7853                         GROUP_P BY expr_list                                    { $$ = $3; }
7854                         | /*EMPTY*/                                                             { $$ = NIL; }
7855                 ;
7856
7857 having_clause:
7858                         HAVING a_expr                                                   { $$ = $2; }
7859                         | /*EMPTY*/                                                             { $$ = NULL; }
7860                 ;
7861
7862 for_locking_clause:
7863                         for_locking_items                                               { $$ = $1; }
7864                         | FOR READ ONLY                                                 { $$ = NIL; }
7865                 ;
7866
7867 opt_for_locking_clause:
7868                         for_locking_clause                                              { $$ = $1; }
7869                         | /* EMPTY */                                                   { $$ = NIL; }
7870                 ;
7871
7872 for_locking_items:
7873                         for_locking_item                                                { $$ = list_make1($1); }
7874                         | for_locking_items for_locking_item    { $$ = lappend($1, $2); }
7875                 ;
7876
7877 for_locking_item:
7878                         FOR UPDATE locked_rels_list opt_nowait
7879                                 {
7880                                         LockingClause *n = makeNode(LockingClause);
7881                                         n->lockedRels = $3;
7882                                         n->forUpdate = TRUE;
7883                                         n->noWait = $4;
7884                                         $$ = (Node *) n;
7885                                 }
7886                         | FOR SHARE locked_rels_list opt_nowait
7887                                 {
7888                                         LockingClause *n = makeNode(LockingClause);
7889                                         n->lockedRels = $3;
7890                                         n->forUpdate = FALSE;
7891                                         n->noWait = $4;
7892                                         $$ = (Node *) n;
7893                                 }
7894                 ;
7895
7896 locked_rels_list:
7897                         OF qualified_name_list                                  { $$ = $2; }
7898                         | /* EMPTY */                                                   { $$ = NIL; }
7899                 ;
7900
7901
7902 values_clause:
7903                         VALUES ctext_row
7904                                 {
7905                                         SelectStmt *n = makeNode(SelectStmt);
7906                                         n->valuesLists = list_make1($2);
7907                                         $$ = (Node *) n;
7908                                 }
7909                         | values_clause ',' ctext_row
7910                                 {
7911                                         SelectStmt *n = (SelectStmt *) $1;
7912                                         n->valuesLists = lappend(n->valuesLists, $3);
7913                                         $$ = (Node *) n;
7914                                 }
7915                 ;
7916
7917
7918 /*****************************************************************************
7919  *
7920  *      clauses common to all Optimizable Stmts:
7921  *              from_clause             - allow list of both JOIN expressions and table names
7922  *              where_clause    - qualifications for joins or restrictions
7923  *
7924  *****************************************************************************/
7925
7926 from_clause:
7927                         FROM from_list                                                  { $$ = $2; }
7928                         | /*EMPTY*/                                                             { $$ = NIL; }
7929                 ;
7930
7931 from_list:
7932                         table_ref                                                               { $$ = list_make1($1); }
7933                         | from_list ',' table_ref                               { $$ = lappend($1, $3); }
7934                 ;
7935
7936 /*
7937  * table_ref is where an alias clause can be attached.  Note we cannot make
7938  * alias_clause have an empty production because that causes parse conflicts
7939  * between table_ref := '(' joined_table ')' alias_clause
7940  * and joined_table := '(' joined_table ')'.  So, we must have the
7941  * redundant-looking productions here instead.
7942  */
7943 table_ref:      relation_expr
7944                                 {
7945                                         $$ = (Node *) $1;
7946                                 }
7947                         | relation_expr alias_clause
7948                                 {
7949                                         $1->alias = $2;
7950                                         $$ = (Node *) $1;
7951                                 }
7952                         | func_table
7953                                 {
7954                                         RangeFunction *n = makeNode(RangeFunction);
7955                                         n->funccallnode = $1;
7956                                         n->coldeflist = NIL;
7957                                         $$ = (Node *) n;
7958                                 }
7959                         | func_table alias_clause
7960                                 {
7961                                         RangeFunction *n = makeNode(RangeFunction);
7962                                         n->funccallnode = $1;
7963                                         n->alias = $2;
7964                                         n->coldeflist = NIL;
7965                                         $$ = (Node *) n;
7966                                 }
7967                         | func_table AS '(' TableFuncElementList ')'
7968                                 {
7969                                         RangeFunction *n = makeNode(RangeFunction);
7970                                         n->funccallnode = $1;
7971                                         n->coldeflist = $4;
7972                                         $$ = (Node *) n;
7973                                 }
7974                         | func_table AS ColId '(' TableFuncElementList ')'
7975                                 {
7976                                         RangeFunction *n = makeNode(RangeFunction);
7977                                         Alias *a = makeNode(Alias);
7978                                         n->funccallnode = $1;
7979                                         a->aliasname = $3;
7980                                         n->alias = a;
7981                                         n->coldeflist = $5;
7982                                         $$ = (Node *) n;
7983                                 }
7984                         | func_table ColId '(' TableFuncElementList ')'
7985                                 {
7986                                         RangeFunction *n = makeNode(RangeFunction);
7987                                         Alias *a = makeNode(Alias);
7988                                         n->funccallnode = $1;
7989                                         a->aliasname = $2;
7990                                         n->alias = a;
7991                                         n->coldeflist = $4;
7992                                         $$ = (Node *) n;
7993                                 }
7994                         | select_with_parens
7995                                 {
7996                                         /*
7997                                          * The SQL spec does not permit a subselect
7998                                          * (<derived_table>) without an alias clause,
7999                                          * so we don't either.  This avoids the problem
8000                                          * of needing to invent a unique refname for it.
8001                                          * That could be surmounted if there's sufficient
8002                                          * popular demand, but for now let's just implement
8003                                          * the spec and see if anyone complains.
8004                                          * However, it does seem like a good idea to emit
8005                                          * an error message that's better than "syntax error".
8006                                          */
8007                                         if (IsA($1, SelectStmt) &&
8008                                                 ((SelectStmt *) $1)->valuesLists)
8009                                                 ereport(ERROR,
8010                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
8011                                                                  errmsg("VALUES in FROM must have an alias"),
8012                                                                  errhint("For example, FROM (VALUES ...) [AS] foo."),
8013                                                                  parser_errposition(@1)));
8014                                         else
8015                                                 ereport(ERROR,
8016                                                                 (errcode(ERRCODE_SYNTAX_ERROR),
8017                                                                  errmsg("subquery in FROM must have an alias"),
8018                                                                  errhint("For example, FROM (SELECT ...) [AS] foo."),
8019                                                                  parser_errposition(@1)));
8020                                         $$ = NULL;
8021                                 }
8022                         | select_with_parens alias_clause
8023                                 {
8024                                         RangeSubselect *n = makeNode(RangeSubselect);
8025                                         n->subquery = $1;
8026                                         n->alias = $2;
8027                                         $$ = (Node *) n;
8028                                 }
8029                         | joined_table
8030                                 {
8031                                         $$ = (Node *) $1;
8032                                 }
8033                         | '(' joined_table ')' alias_clause
8034                                 {
8035                                         $2->alias = $4;
8036                                         $$ = (Node *) $2;
8037                                 }
8038                 ;
8039
8040
8041 /*
8042  * It may seem silly to separate joined_table from table_ref, but there is
8043  * method in SQL92's madness: if you don't do it this way you get reduce-
8044  * reduce conflicts, because it's not clear to the parser generator whether
8045  * to expect alias_clause after ')' or not.  For the same reason we must
8046  * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
8047  * join_type to expand to empty; if we try it, the parser generator can't
8048  * figure out when to reduce an empty join_type right after table_ref.
8049  *
8050  * Note that a CROSS JOIN is the same as an unqualified
8051  * INNER JOIN, and an INNER JOIN/ON has the same shape
8052  * but a qualification expression to limit membership.
8053  * A NATURAL JOIN implicitly matches column names between
8054  * tables and the shape is determined by which columns are
8055  * in common. We'll collect columns during the later transformations.
8056  */
8057
8058 joined_table:
8059                         '(' joined_table ')'
8060                                 {
8061                                         $$ = $2;
8062                                 }
8063                         | table_ref CROSS JOIN table_ref
8064                                 {
8065                                         /* CROSS JOIN is same as unqualified inner join */
8066                                         JoinExpr *n = makeNode(JoinExpr);
8067                                         n->jointype = JOIN_INNER;
8068                                         n->isNatural = FALSE;
8069                                         n->larg = $1;
8070                                         n->rarg = $4;
8071                                         n->usingClause = NIL;
8072                                         n->quals = NULL;
8073                                         $$ = n;
8074                                 }
8075                         | table_ref join_type JOIN table_ref join_qual
8076                                 {
8077                                         JoinExpr *n = makeNode(JoinExpr);
8078                                         n->jointype = $2;
8079                                         n->isNatural = FALSE;
8080                                         n->larg = $1;
8081                                         n->rarg = $4;
8082                                         if ($5 != NULL && IsA($5, List))
8083                                                 n->usingClause = (List *) $5; /* USING clause */
8084                                         else
8085                                                 n->quals = $5; /* ON clause */
8086                                         $$ = n;
8087                                 }
8088                         | table_ref JOIN table_ref join_qual
8089                                 {
8090                                         /* letting join_type reduce to empty doesn't work */
8091                                         JoinExpr *n = makeNode(JoinExpr);
8092                                         n->jointype = JOIN_INNER;
8093                                         n->isNatural = FALSE;
8094                                         n->larg = $1;
8095                                         n->rarg = $3;
8096                                         if ($4 != NULL && IsA($4, List))
8097                                                 n->usingClause = (List *) $4; /* USING clause */
8098                                         else
8099                                                 n->quals = $4; /* ON clause */
8100                                         $$ = n;
8101                                 }
8102                         | table_ref NATURAL join_type JOIN table_ref
8103                                 {
8104                                         JoinExpr *n = makeNode(JoinExpr);
8105                                         n->jointype = $3;
8106                                         n->isNatural = TRUE;
8107                                         n->larg = $1;
8108                                         n->rarg = $5;
8109                                         n->usingClause = NIL; /* figure out which columns later... */
8110                                         n->quals = NULL; /* fill later */
8111                                         $$ = n;
8112                                 }
8113                         | table_ref NATURAL JOIN table_ref
8114                                 {
8115                                         /* letting join_type reduce to empty doesn't work */
8116                                         JoinExpr *n = makeNode(JoinExpr);
8117                                         n->jointype = JOIN_INNER;
8118                                         n->isNatural = TRUE;
8119                                         n->larg = $1;
8120                                         n->rarg = $4;
8121                                         n->usingClause = NIL; /* figure out which columns later... */
8122                                         n->quals = NULL; /* fill later */
8123                                         $$ = n;
8124                                 }
8125                 ;
8126
8127 alias_clause:
8128                         AS ColId '(' name_list ')'
8129                                 {
8130                                         $$ = makeNode(Alias);
8131                                         $$->aliasname = $2;
8132                                         $$->colnames = $4;
8133                                 }
8134                         | AS ColId
8135                                 {
8136                                         $$ = makeNode(Alias);
8137                                         $$->aliasname = $2;
8138                                 }
8139                         | ColId '(' name_list ')'
8140                                 {
8141                                         $$ = makeNode(Alias);
8142                                         $$->aliasname = $1;
8143                                         $$->colnames = $3;
8144                                 }
8145                         | ColId
8146                                 {
8147                                         $$ = makeNode(Alias);
8148                                         $$->aliasname = $1;
8149                                 }
8150                 ;
8151
8152 join_type:      FULL join_outer                                                 { $$ = JOIN_FULL; }
8153                         | LEFT join_outer                                               { $$ = JOIN_LEFT; }
8154                         | RIGHT join_outer                                              { $$ = JOIN_RIGHT; }
8155                         | INNER_P                                                               { $$ = JOIN_INNER; }
8156                 ;
8157
8158 /* OUTER is just noise... */
8159 join_outer: OUTER_P                                                                     { $$ = NULL; }
8160                         | /*EMPTY*/                                                             { $$ = NULL; }
8161                 ;
8162
8163 /* JOIN qualification clauses
8164  * Possibilities are:
8165  *      USING ( column list ) allows only unqualified column names,
8166  *                                                which must match between tables.
8167  *      ON expr allows more general qualifications.
8168  *
8169  * We return USING as a List node, while an ON-expr will not be a List.
8170  */
8171
8172 join_qual:      USING '(' name_list ')'                                 { $$ = (Node *) $3; }
8173                         | ON a_expr                                                             { $$ = $2; }
8174                 ;
8175
8176
8177 relation_expr:
8178                         qualified_name
8179                                 {
8180                                         /* default inheritance */
8181                                         $$ = $1;
8182                                         $$->inhOpt = INH_DEFAULT;
8183                                         $$->alias = NULL;
8184                                 }
8185                         | qualified_name '*'
8186                                 {
8187                                         /* inheritance query */
8188                                         $$ = $1;
8189                                         $$->inhOpt = INH_YES;
8190                                         $$->alias = NULL;
8191                                 }
8192                         | ONLY qualified_name
8193                                 {
8194                                         /* no inheritance */
8195                                         $$ = $2;
8196                                         $$->inhOpt = INH_NO;
8197                                         $$->alias = NULL;
8198                                 }
8199                         | ONLY '(' qualified_name ')'
8200                                 {
8201                                         /* no inheritance, SQL99-style syntax */
8202                                         $$ = $3;
8203                                         $$->inhOpt = INH_NO;
8204                                         $$->alias = NULL;
8205                                 }
8206                 ;
8207
8208
8209 relation_expr_list:
8210                         relation_expr                                                   { $$ = list_make1($1); }
8211                         | relation_expr_list ',' relation_expr  { $$ = lappend($1, $3); }
8212                 ;
8213
8214
8215 /*
8216  * Given "UPDATE foo set set ...", we have to decide without looking any
8217  * further ahead whether the first "set" is an alias or the UPDATE's SET
8218  * keyword.  Since "set" is allowed as a column name both interpretations
8219  * are feasible.  We resolve the shift/reduce conflict by giving the first
8220  * relation_expr_opt_alias production a higher precedence than the SET token
8221  * has, causing the parser to prefer to reduce, in effect assuming that the
8222  * SET is not an alias.
8223  */
8224 relation_expr_opt_alias: relation_expr                                  %prec UMINUS
8225                                 {
8226                                         $$ = $1;
8227                                 }
8228                         | relation_expr ColId
8229                                 {
8230                                         Alias *alias = makeNode(Alias);
8231                                         alias->aliasname = $2;
8232                                         $1->alias = alias;
8233                                         $$ = $1;
8234                                 }
8235                         | relation_expr AS ColId
8236                                 {
8237                                         Alias *alias = makeNode(Alias);
8238                                         alias->aliasname = $3;
8239                                         $1->alias = alias;
8240                                         $$ = $1;
8241                                 }
8242                 ;
8243
8244
8245 func_table: func_expr                                                           { $$ = $1; }
8246                 ;
8247
8248
8249 where_clause:
8250                         WHERE a_expr                                                    { $$ = $2; }
8251                         | /*EMPTY*/                                                             { $$ = NULL; }
8252                 ;
8253
8254 /* variant for UPDATE and DELETE */
8255 where_or_current_clause:
8256                         WHERE a_expr                                                    { $$ = $2; }
8257                         | WHERE CURRENT_P OF name
8258                                 {
8259                                         CurrentOfExpr *n = makeNode(CurrentOfExpr);
8260                                         /* cvarno is filled in by parse analysis */
8261                                         n->cursor_name = $4;
8262                                         n->cursor_param = 0;
8263                                         $$ = (Node *) n;
8264                                 }
8265                         | /*EMPTY*/                                                             { $$ = NULL; }
8266                 ;
8267
8268
8269 OptTableFuncElementList:
8270                         TableFuncElementList                            { $$ = $1; }
8271                         | /*EMPTY*/                                                     { $$ = NIL; }
8272                 ;
8273
8274 TableFuncElementList:
8275                         TableFuncElement
8276                                 {
8277                                         $$ = list_make1($1);
8278                                 }
8279                         | TableFuncElementList ',' TableFuncElement
8280                                 {
8281                                         $$ = lappend($1, $3);
8282                                 }
8283                 ;
8284
8285 TableFuncElement:       ColId Typename
8286                                 {
8287                                         ColumnDef *n = makeNode(ColumnDef);
8288                                         n->colname = $1;
8289                                         n->typeName = $2;
8290                                         n->constraints = NIL;
8291                                         n->is_local = true;
8292                                         $$ = (Node *)n;
8293                                 }
8294                 ;
8295
8296 /*****************************************************************************
8297  *
8298  *      Type syntax
8299  *              SQL92 introduces a large amount of type-specific syntax.
8300  *              Define individual clauses to handle these cases, and use
8301  *               the generic case to handle regular type-extensible Postgres syntax.
8302  *              - thomas 1997-10-10
8303  *
8304  *****************************************************************************/
8305
8306 Typename:       SimpleTypename opt_array_bounds
8307                                 {
8308                                         $$ = $1;
8309                                         $$->arrayBounds = $2;
8310                                 }
8311                         | SETOF SimpleTypename opt_array_bounds
8312                                 {
8313                                         $$ = $2;
8314                                         $$->arrayBounds = $3;
8315                                         $$->setof = TRUE;
8316                                 }
8317                         /* SQL standard syntax, currently only one-dimensional */
8318                         | SimpleTypename ARRAY '[' Iconst ']'
8319                                 {
8320                                         $$ = $1;
8321                                         $$->arrayBounds = list_make1(makeInteger($4));
8322                                 }
8323                         | SETOF SimpleTypename ARRAY '[' Iconst ']'
8324                                 {
8325                                         $$ = $2;
8326                                         $$->arrayBounds = list_make1(makeInteger($5));
8327                                         $$->setof = TRUE;
8328                                 }
8329                         | SimpleTypename ARRAY
8330                                 {
8331                                         $$ = $1;
8332                                         $$->arrayBounds = list_make1(makeInteger(-1));
8333                                 }
8334                         | SETOF SimpleTypename ARRAY
8335                                 {
8336                                         $$ = $2;
8337                                         $$->arrayBounds = list_make1(makeInteger(-1));
8338                                         $$->setof = TRUE;
8339                                 }
8340                 ;
8341
8342 opt_array_bounds:
8343                         opt_array_bounds '[' ']'
8344                                         {  $$ = lappend($1, makeInteger(-1)); }
8345                         | opt_array_bounds '[' Iconst ']'
8346                                         {  $$ = lappend($1, makeInteger($3)); }
8347                         | /*EMPTY*/
8348                                         {  $$ = NIL; }
8349                 ;
8350
8351 SimpleTypename:
8352                         GenericType                                                             { $$ = $1; }
8353                         | Numeric                                                               { $$ = $1; }
8354                         | Bit                                                                   { $$ = $1; }
8355                         | Character                                                             { $$ = $1; }
8356                         | ConstDatetime                                                 { $$ = $1; }
8357                         | ConstInterval opt_interval
8358                                 {
8359                                         $$ = $1;
8360                                         $$->typmods = $2;
8361                                 }
8362                         | ConstInterval '(' Iconst ')' opt_interval
8363                                 {
8364                                         $$ = $1;
8365                                         if ($5 != NIL)
8366                                         {
8367                                                 if (list_length($5) != 1)
8368                                                         ereport(ERROR,
8369                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
8370                                                                          errmsg("interval precision specified twice"),
8371                                                                          parser_errposition(@1)));
8372                                                 $$->typmods = lappend($5, makeIntConst($3, @3));
8373                                         }
8374                                         else
8375                                                 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
8376                                                                                                  makeIntConst($3, @3));
8377                                 }
8378                 ;
8379
8380 /* We have a separate ConstTypename to allow defaulting fixed-length
8381  * types such as CHAR() and BIT() to an unspecified length.
8382  * SQL9x requires that these default to a length of one, but this
8383  * makes no sense for constructs like CHAR 'hi' and BIT '0101',
8384  * where there is an obvious better choice to make.
8385  * Note that ConstInterval is not included here since it must
8386  * be pushed up higher in the rules to accomodate the postfix
8387  * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
8388  * the generic-type-name case in AExprConst to avoid premature
8389  * reduce/reduce conflicts against function names.
8390  */
8391 ConstTypename:
8392                         Numeric                                                                 { $$ = $1; }
8393                         | ConstBit                                                              { $$ = $1; }
8394                         | ConstCharacter                                                { $$ = $1; }
8395                         | ConstDatetime                                                 { $$ = $1; }
8396                 ;
8397
8398 /*
8399  * GenericType covers all type names that don't have special syntax mandated
8400  * by the standard, including qualified names.  We also allow type modifiers.
8401  * To avoid parsing conflicts against function invocations, the modifiers
8402  * have to be shown as expr_list here, but parse analysis will only accept
8403  * constants for them.
8404  */
8405 GenericType:
8406                         type_function_name opt_type_modifiers
8407                                 {
8408                                         $$ = makeTypeName($1);
8409                                         $$->typmods = $2;
8410                                         $$->location = @1;
8411                                 }
8412                         | type_function_name attrs opt_type_modifiers
8413                                 {
8414                                         $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
8415                                         $$->typmods = $3;
8416                                         $$->location = @1;
8417                                 }
8418                 ;
8419
8420 opt_type_modifiers: '(' expr_list ')'                           { $$ = $2; }
8421                                         | /* EMPTY */                                   { $$ = NIL; }
8422                 ;
8423
8424 /*
8425  * SQL92 numeric data types
8426  */
8427 Numeric:        INT_P
8428                                 {
8429                                         $$ = SystemTypeName("int4");
8430                                         $$->location = @1;
8431                                 }
8432                         | INTEGER
8433                                 {
8434                                         $$ = SystemTypeName("int4");
8435                                         $$->location = @1;
8436                                 }
8437                         | SMALLINT
8438                                 {
8439                                         $$ = SystemTypeName("int2");
8440                                         $$->location = @1;
8441                                 }
8442                         | BIGINT
8443                                 {
8444                                         $$ = SystemTypeName("int8");
8445                                         $$->location = @1;
8446                                 }
8447                         | REAL
8448                                 {
8449                                         $$ = SystemTypeName("float4");
8450                                         $$->location = @1;
8451                                 }
8452                         | FLOAT_P opt_float
8453                                 {
8454                                         $$ = $2;
8455                                         $$->location = @1;
8456                                 }
8457                         | DOUBLE_P PRECISION
8458                                 {
8459                                         $$ = SystemTypeName("float8");
8460                                         $$->location = @1;
8461                                 }
8462                         | DECIMAL_P opt_type_modifiers
8463                                 {
8464                                         $$ = SystemTypeName("numeric");
8465                                         $$->typmods = $2;
8466                                         $$->location = @1;
8467                                 }
8468                         | DEC opt_type_modifiers
8469                                 {
8470                                         $$ = SystemTypeName("numeric");
8471                                         $$->typmods = $2;
8472                                         $$->location = @1;
8473                                 }
8474                         | NUMERIC opt_type_modifiers
8475                                 {
8476                                         $$ = SystemTypeName("numeric");
8477                                         $$->typmods = $2;
8478                                         $$->location = @1;
8479                                 }
8480                         | BOOLEAN_P
8481                                 {
8482                                         $$ = SystemTypeName("bool");
8483                                         $$->location = @1;
8484                                 }
8485                 ;
8486
8487 opt_float:      '(' Iconst ')'
8488                                 {
8489                                         /*
8490                                          * Check FLOAT() precision limits assuming IEEE floating
8491                                          * types - thomas 1997-09-18
8492                                          */
8493                                         if ($2 < 1)
8494                                                 ereport(ERROR,
8495                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8496                                                                  errmsg("precision for type float must be at least 1 bit"),
8497                                                                  parser_errposition(@2)));
8498                                         else if ($2 <= 24)
8499                                                 $$ = SystemTypeName("float4");
8500                                         else if ($2 <= 53)
8501                                                 $$ = SystemTypeName("float8");
8502                                         else
8503                                                 ereport(ERROR,
8504                                                                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8505                                                                  errmsg("precision for type float must be less than 54 bits"),
8506                                                                  parser_errposition(@2)));
8507                                 }
8508                         | /*EMPTY*/
8509                                 {
8510                                         $$ = SystemTypeName("float8");
8511                                 }
8512                 ;
8513
8514 /*
8515  * SQL92 bit-field data types
8516  * The following implements BIT() and BIT VARYING().
8517  */
8518 Bit:            BitWithLength
8519                                 {
8520                                         $$ = $1;
8521                                 }
8522                         | BitWithoutLength
8523                                 {
8524                                         $$ = $1;
8525                                 }
8526                 ;
8527
8528 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
8529 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
8530 ConstBit:       BitWithLength
8531                                 {
8532                                         $$ = $1;
8533                                 }
8534                         | BitWithoutLength
8535                                 {
8536                                         $$ = $1;
8537                                         $$->typmods = NIL;
8538                                 }
8539                 ;
8540
8541 BitWithLength:
8542                         BIT opt_varying '(' expr_list ')'
8543                                 {
8544                                         char *typname;
8545
8546                                         typname = $2 ? "varbit" : "bit";
8547                                         $$ = SystemTypeName(typname);
8548                                         $$->typmods = $4;
8549                                         $$->location = @1;
8550                                 }
8551                 ;
8552
8553 BitWithoutLength:
8554                         BIT opt_varying
8555                                 {
8556                                         /* bit defaults to bit(1), varbit to no limit */
8557                                         if ($2)
8558                                         {
8559                                                 $$ = SystemTypeName("varbit");
8560                                         }
8561                                         else
8562                                         {
8563                                                 $$ = SystemTypeName("bit");
8564                                                 $$->typmods = list_make1(makeIntConst(1, -1));
8565                                         }
8566                                         $$->location = @1;
8567                                 }
8568                 ;
8569
8570
8571 /*
8572  * SQL92 character data types
8573  * The following implements CHAR() and VARCHAR().
8574  */
8575 Character:  CharacterWithLength
8576                                 {
8577                                         $$ = $1;
8578                                 }
8579                         | CharacterWithoutLength
8580                                 {
8581                                         $$ = $1;
8582                                 }
8583                 ;
8584
8585 ConstCharacter:  CharacterWithLength
8586                                 {
8587                                         $$ = $1;
8588                                 }
8589                         | CharacterWithoutLength
8590                                 {
8591                                         /* Length was not specified so allow to be unrestricted.
8592                                          * This handles problems with fixed-length (bpchar) strings
8593                                          * which in column definitions must default to a length
8594                                          * of one, but should not be constrained if the length
8595                                          * was not specified.
8596                                          */
8597                                         $$ = $1;
8598                                         $$->typmods = NIL;
8599                                 }
8600                 ;
8601
8602 CharacterWithLength:  character '(' Iconst ')' opt_charset
8603                                 {
8604                                         if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
8605                                         {
8606                                                 char *type;
8607
8608                                                 type = palloc(strlen($1) + 1 + strlen($5) + 1);
8609                                                 strcpy(type, $1);
8610                                                 strcat(type, "_");
8611                                                 strcat(type, $5);
8612                                                 $1 = type;
8613                                         }
8614
8615                                         $$ = SystemTypeName($1);
8616                                         $$->typmods = list_make1(makeIntConst($3, @3));
8617                                         $$->location = @1;
8618                                 }
8619                 ;
8620
8621 CharacterWithoutLength:  character opt_charset
8622                                 {
8623                                         if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
8624                                         {
8625                                                 char *type;
8626
8627                                                 type = palloc(strlen($1) + 1 + strlen($2) + 1);
8628                                                 strcpy(type, $1);
8629                                                 strcat(type, "_");
8630                                                 strcat(type, $2);
8631                                                 $1 = type;
8632                                         }
8633
8634                                         $$ = SystemTypeName($1);
8635
8636                                         /* char defaults to char(1), varchar to no limit */
8637                                         if (strcmp($1, "bpchar") == 0)
8638                                                 $$->typmods = list_make1(makeIntConst(1, -1));
8639
8640                                         $$->location = @1;
8641                                 }
8642                 ;
8643
8644 character:      CHARACTER opt_varying
8645                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
8646                         | CHAR_P opt_varying
8647                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
8648                         | VARCHAR
8649                                                                                 { $$ = "varchar"; }
8650                         | NATIONAL CHARACTER opt_varying
8651                                                                                 { $$ = $3 ? "varchar": "bpchar"; }
8652                         | NATIONAL CHAR_P opt_varying
8653                                                                                 { $$ = $3 ? "varchar": "bpchar"; }
8654                         | NCHAR opt_varying
8655                                                                                 { $$ = $2 ? "varchar": "bpchar"; }
8656                 ;
8657
8658 opt_varying:
8659                         VARYING                                                                 { $$ = TRUE; }
8660                         | /*EMPTY*/                                                             { $$ = FALSE; }
8661                 ;
8662
8663 opt_charset:
8664                         CHARACTER SET ColId                                             { $$ = $3; }
8665                         | /*EMPTY*/                                                             { $$ = NULL; }
8666                 ;
8667
8668 /*
8669  * SQL92 date/time types
8670  */
8671 ConstDatetime:
8672                         TIMESTAMP '(' Iconst ')' opt_timezone
8673                                 {
8674                                         if ($5)
8675                                                 $$ = SystemTypeName("timestamptz");
8676                                         else
8677                                                 $$ = SystemTypeName("timestamp");
8678                                         $$->typmods = list_make1(makeIntConst($3, @3));
8679                                         $$->location = @1;
8680                                 }
8681                         | TIMESTAMP opt_timezone
8682                                 {
8683                                         if ($2)
8684                                                 $$ = SystemTypeName("timestamptz");
8685                                         else
8686                                                 $$ = SystemTypeName("timestamp");
8687                                         $$->location = @1;
8688                                 }
8689                         | TIME '(' Iconst ')' opt_timezone
8690                                 {
8691                                         if ($5)
8692                                                 $$ = SystemTypeName("timetz");
8693                                         else
8694                                                 $$ = SystemTypeName("time");
8695                                         $$->typmods = list_make1(makeIntConst($3, @3));
8696                                         $$->location = @1;
8697                                 }
8698                         | TIME opt_timezone
8699                                 {
8700                                         if ($2)
8701                                                 $$ = SystemTypeName("timetz");
8702                                         else
8703                                                 $$ = SystemTypeName("time");
8704                                         $$->location = @1;
8705                                 }
8706                 ;
8707
8708 ConstInterval:
8709                         INTERVAL
8710                                 {
8711                                         $$ = SystemTypeName("interval");
8712                                         $$->location = @1;
8713                                 }
8714                 ;
8715
8716 opt_timezone:
8717                         WITH_TIME ZONE                                                  { $$ = TRUE; }
8718                         | WITHOUT TIME ZONE                                             { $$ = FALSE; }
8719                         | /*EMPTY*/                                                             { $$ = FALSE; }
8720                 ;
8721
8722 opt_interval:
8723                         YEAR_P
8724                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
8725                         | MONTH_P
8726                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
8727                         | DAY_P
8728                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
8729                         | HOUR_P
8730                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
8731                         | MINUTE_P
8732                                 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
8733                         | interval_second
8734                                 { $$ = $1; }
8735                         | YEAR_P TO MONTH_P
8736                                 {
8737                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
8738                                                                                                  INTERVAL_MASK(MONTH), @1));
8739                                 }
8740                         | DAY_P TO HOUR_P
8741                                 {
8742                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8743                                                                                                  INTERVAL_MASK(HOUR), @1));
8744                                 }
8745                         | DAY_P TO MINUTE_P
8746                                 {
8747                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8748                                                                                                  INTERVAL_MASK(HOUR) |
8749                                                                                                  INTERVAL_MASK(MINUTE), @1));
8750                                 }
8751                         | DAY_P TO interval_second
8752                                 {
8753                                         $$ = $3;
8754                                         linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
8755                                                                                                 INTERVAL_MASK(HOUR) |
8756                                                                                                 INTERVAL_MASK(MINUTE) |
8757                                                                                                 INTERVAL_MASK(SECOND), @1);
8758                                 }
8759                         | HOUR_P TO MINUTE_P
8760                                 {
8761                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
8762                                                                                                  INTERVAL_MASK(MINUTE), @1));
8763                                 }
8764                         | HOUR_P TO interval_second
8765                                 {
8766                                         $$ = $3;
8767                                         linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
8768                                                                                                 INTERVAL_MASK(MINUTE) |
8769                                                                                                 INTERVAL_MASK(SECOND), @1);
8770                                 }
8771                         | MINUTE_P TO interval_second
8772                                 {
8773                                         $$ = $3;
8774                                         linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
8775                                                                                                 INTERVAL_MASK(SECOND), @1);
8776                                 }
8777                         | /*EMPTY*/
8778                                 { $$ = NIL; }
8779                 ;
8780
8781 interval_second:
8782                         SECOND_P
8783                                 {
8784                                         $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
8785                                 }
8786                         | SECOND_P '(' Iconst ')'
8787                                 {
8788                                         $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
8789                                                                         makeIntConst($3, @3));
8790                                 }
8791                 ;
8792
8793
8794 /*****************************************************************************
8795  *
8796  *      expression grammar
8797  *
8798  *****************************************************************************/
8799
8800 /*
8801  * General expressions
8802  * This is the heart of the expression syntax.
8803  *
8804  * We have two expression types: a_expr is the unrestricted kind, and
8805  * b_expr is a subset that must be used in some places to avoid shift/reduce
8806  * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
8807  * because that use of AND conflicts with AND as a boolean operator.  So,
8808  * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
8809  *
8810  * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
8811  * always be used by surrounding it with parens.
8812  *
8813  * c_expr is all the productions that are common to a_expr and b_expr;
8814  * it's factored out just to eliminate redundant coding.
8815  */
8816 a_expr:         c_expr                                                                  { $$ = $1; }
8817                         | a_expr TYPECAST Typename
8818                                         { $$ = makeTypeCast($1, $3, @2); }
8819                         | a_expr AT TIME ZONE a_expr
8820                                 {
8821                                         FuncCall *n = makeNode(FuncCall);
8822                                         n->funcname = SystemFuncName("timezone");
8823                                         n->args = list_make2($5, $1);
8824                                         n->agg_order = NIL;
8825                                         n->agg_star = FALSE;
8826                                         n->agg_distinct = FALSE;
8827                                         n->func_variadic = FALSE;
8828                                         n->over = NULL;
8829                                         n->location = @2;
8830                                         $$ = (Node *) n;
8831                                 }
8832                 /*
8833                  * These operators must be called out explicitly in order to make use
8834                  * of bison's automatic operator-precedence handling.  All other
8835                  * operator names are handled by the generic productions using "Op",
8836                  * below; and all those operators will have the same precedence.
8837                  *
8838                  * If you add more explicitly-known operators, be sure to add them
8839                  * also to b_expr and to the MathOp list above.
8840                  */
8841                         | '+' a_expr                                    %prec UMINUS
8842                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8843                         | '-' a_expr                                    %prec UMINUS
8844                                 { $$ = doNegate($2, @1); }
8845                         | a_expr '+' a_expr
8846                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8847                         | a_expr '-' a_expr
8848                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8849                         | a_expr '*' a_expr
8850                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8851                         | a_expr '/' a_expr
8852                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8853                         | a_expr '%' a_expr
8854                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8855                         | a_expr '^' a_expr
8856                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8857                         | a_expr '<' a_expr
8858                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8859                         | a_expr '>' a_expr
8860                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8861                         | a_expr '=' a_expr
8862                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8863
8864                         | a_expr qual_Op a_expr                         %prec Op
8865                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8866                         | qual_Op a_expr                                        %prec Op
8867                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8868                         | a_expr qual_Op                                        %prec POSTFIXOP
8869                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8870
8871                         | a_expr AND a_expr
8872                                 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
8873                         | a_expr OR a_expr
8874                                 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
8875                         | NOT a_expr
8876                                 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
8877
8878                         | a_expr LIKE a_expr
8879                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
8880                         | a_expr LIKE a_expr ESCAPE a_expr
8881                                 {
8882                                         FuncCall *n = makeNode(FuncCall);
8883                                         n->funcname = SystemFuncName("like_escape");
8884                                         n->args = list_make2($3, $5);
8885                                         n->agg_order = NIL;
8886                                         n->agg_star = FALSE;
8887                                         n->agg_distinct = FALSE;
8888                                         n->func_variadic = FALSE;
8889                                         n->over = NULL;
8890                                         n->location = @2;
8891                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
8892                                 }
8893                         | a_expr NOT LIKE a_expr
8894                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
8895                         | a_expr NOT LIKE a_expr ESCAPE a_expr
8896                                 {
8897                                         FuncCall *n = makeNode(FuncCall);
8898                                         n->funcname = SystemFuncName("like_escape");
8899                                         n->args = list_make2($4, $6);
8900                                         n->agg_order = NIL;
8901                                         n->agg_star = FALSE;
8902                                         n->agg_distinct = FALSE;
8903                                         n->func_variadic = FALSE;
8904                                         n->over = NULL;
8905                                         n->location = @2;
8906                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
8907                                 }
8908                         | a_expr ILIKE a_expr
8909                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
8910                         | a_expr ILIKE a_expr ESCAPE a_expr
8911                                 {
8912                                         FuncCall *n = makeNode(FuncCall);
8913                                         n->funcname = SystemFuncName("like_escape");
8914                                         n->args = list_make2($3, $5);
8915                                         n->agg_order = NIL;
8916                                         n->agg_star = FALSE;
8917                                         n->agg_distinct = FALSE;
8918                                         n->func_variadic = FALSE;
8919                                         n->over = NULL;
8920                                         n->location = @2;
8921                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
8922                                 }
8923                         | a_expr NOT ILIKE a_expr
8924                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
8925                         | a_expr NOT ILIKE a_expr ESCAPE a_expr
8926                                 {
8927                                         FuncCall *n = makeNode(FuncCall);
8928                                         n->funcname = SystemFuncName("like_escape");
8929                                         n->args = list_make2($4, $6);
8930                                         n->agg_order = NIL;
8931                                         n->agg_star = FALSE;
8932                                         n->agg_distinct = FALSE;
8933                                         n->func_variadic = FALSE;
8934                                         n->over = NULL;
8935                                         n->location = @2;
8936                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
8937                                 }
8938
8939                         | a_expr SIMILAR TO a_expr                              %prec SIMILAR
8940                                 {
8941                                         FuncCall *n = makeNode(FuncCall);
8942                                         n->funcname = SystemFuncName("similar_escape");
8943                                         n->args = list_make2($4, makeNullAConst(-1));
8944                                         n->agg_order = NIL;
8945                                         n->agg_star = FALSE;
8946                                         n->agg_distinct = FALSE;
8947                                         n->func_variadic = FALSE;
8948                                         n->over = NULL;
8949                                         n->location = @2;
8950                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8951                                 }
8952                         | a_expr SIMILAR TO a_expr ESCAPE a_expr
8953                                 {
8954                                         FuncCall *n = makeNode(FuncCall);
8955                                         n->funcname = SystemFuncName("similar_escape");
8956                                         n->args = list_make2($4, $6);
8957                                         n->agg_order = NIL;
8958                                         n->agg_star = FALSE;
8959                                         n->agg_distinct = FALSE;
8960                                         n->func_variadic = FALSE;
8961                                         n->over = NULL;
8962                                         n->location = @2;
8963                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8964                                 }
8965                         | a_expr NOT SIMILAR TO a_expr                  %prec SIMILAR
8966                                 {
8967                                         FuncCall *n = makeNode(FuncCall);
8968                                         n->funcname = SystemFuncName("similar_escape");
8969                                         n->args = list_make2($5, makeNullAConst(-1));
8970                                         n->agg_order = NIL;
8971                                         n->agg_star = FALSE;
8972                                         n->agg_distinct = FALSE;
8973                                         n->func_variadic = FALSE;
8974                                         n->over = NULL;
8975                                         n->location = @2;
8976                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8977                                 }
8978                         | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
8979                                 {
8980                                         FuncCall *n = makeNode(FuncCall);
8981                                         n->funcname = SystemFuncName("similar_escape");
8982                                         n->args = list_make2($5, $7);
8983                                         n->agg_order = NIL;
8984                                         n->agg_star = FALSE;
8985                                         n->agg_distinct = FALSE;
8986                                         n->func_variadic = FALSE;
8987                                         n->over = NULL;
8988                                         n->location = @2;
8989                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8990                                 }
8991
8992                         /* NullTest clause
8993                          * Define SQL92-style Null test clause.
8994                          * Allow two forms described in the standard:
8995                          *      a IS NULL
8996                          *      a IS NOT NULL
8997                          * Allow two SQL extensions
8998                          *      a ISNULL
8999                          *      a NOTNULL
9000                          */
9001                         | a_expr IS NULL_P
9002                                 {
9003                                         NullTest *n = makeNode(NullTest);
9004                                         n->arg = (Expr *) $1;
9005                                         n->nulltesttype = IS_NULL;
9006                                         $$ = (Node *)n;
9007                                 }
9008                         | a_expr ISNULL
9009                                 {
9010                                         NullTest *n = makeNode(NullTest);
9011                                         n->arg = (Expr *) $1;
9012                                         n->nulltesttype = IS_NULL;
9013                                         $$ = (Node *)n;
9014                                 }
9015                         | a_expr IS NOT NULL_P
9016                                 {
9017                                         NullTest *n = makeNode(NullTest);
9018                                         n->arg = (Expr *) $1;
9019                                         n->nulltesttype = IS_NOT_NULL;
9020                                         $$ = (Node *)n;
9021                                 }
9022                         | a_expr NOTNULL
9023                                 {
9024                                         NullTest *n = makeNode(NullTest);
9025                                         n->arg = (Expr *) $1;
9026                                         n->nulltesttype = IS_NOT_NULL;
9027                                         $$ = (Node *)n;
9028                                 }
9029                         | row OVERLAPS row
9030                                 {
9031                                         $$ = (Node *)makeOverlaps($1, $3, @2, yyscanner);
9032                                 }
9033                         | a_expr IS TRUE_P
9034                                 {
9035                                         BooleanTest *b = makeNode(BooleanTest);
9036                                         b->arg = (Expr *) $1;
9037                                         b->booltesttype = IS_TRUE;
9038                                         $$ = (Node *)b;
9039                                 }
9040                         | a_expr IS NOT TRUE_P
9041                                 {
9042                                         BooleanTest *b = makeNode(BooleanTest);
9043                                         b->arg = (Expr *) $1;
9044                                         b->booltesttype = IS_NOT_TRUE;
9045                                         $$ = (Node *)b;
9046                                 }
9047                         | a_expr IS FALSE_P
9048                                 {
9049                                         BooleanTest *b = makeNode(BooleanTest);
9050                                         b->arg = (Expr *) $1;
9051                                         b->booltesttype = IS_FALSE;
9052                                         $$ = (Node *)b;
9053                                 }
9054                         | a_expr IS NOT FALSE_P
9055                                 {
9056                                         BooleanTest *b = makeNode(BooleanTest);
9057                                         b->arg = (Expr *) $1;
9058                                         b->booltesttype = IS_NOT_FALSE;
9059                                         $$ = (Node *)b;
9060                                 }
9061                         | a_expr IS UNKNOWN
9062                                 {
9063                                         BooleanTest *b = makeNode(BooleanTest);
9064                                         b->arg = (Expr *) $1;
9065                                         b->booltesttype = IS_UNKNOWN;
9066                                         $$ = (Node *)b;
9067                                 }
9068                         | a_expr IS NOT UNKNOWN
9069                                 {
9070                                         BooleanTest *b = makeNode(BooleanTest);
9071                                         b->arg = (Expr *) $1;
9072                                         b->booltesttype = IS_NOT_UNKNOWN;
9073                                         $$ = (Node *)b;
9074                                 }
9075                         | a_expr IS DISTINCT FROM a_expr                        %prec IS
9076                                 {
9077                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
9078                                 }
9079                         | a_expr IS NOT DISTINCT FROM a_expr            %prec IS
9080                                 {
9081                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
9082                                                                         (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
9083                                                                                                                           "=", $1, $6, @2),
9084                                                                                          @2);
9085
9086                                 }
9087                         | a_expr IS OF '(' type_list ')'                        %prec IS
9088                                 {
9089                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
9090                                 }
9091                         | a_expr IS NOT OF '(' type_list ')'            %prec IS
9092                                 {
9093                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
9094                                 }
9095                         /*
9096                          *      Ideally we would not use hard-wired operators below but
9097                          *      instead use opclasses.  However, mixed data types and other
9098                          *      issues make this difficult:
9099                          *      http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
9100                          */
9101                         | a_expr BETWEEN opt_asymmetric b_expr AND b_expr               %prec BETWEEN
9102                                 {
9103                                         $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
9104                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
9105                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
9106                                                                                          @2);
9107                                 }
9108                         | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr   %prec BETWEEN
9109                                 {
9110                                         $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
9111                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
9112                                                 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
9113                                                                                          @2);
9114                                 }
9115                         | a_expr BETWEEN SYMMETRIC b_expr AND b_expr                    %prec BETWEEN
9116                                 {
9117                                         $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
9118                                                 (Node *) makeA_Expr(AEXPR_AND, NIL,
9119                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
9120                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
9121                                                                                         @2),
9122                                                 (Node *) makeA_Expr(AEXPR_AND, NIL,
9123                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
9124                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
9125                                                                                         @2),
9126                                                                                          @2);
9127                                 }
9128                         | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr                %prec BETWEEN
9129                                 {
9130                                         $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
9131                                                 (Node *) makeA_Expr(AEXPR_OR, NIL,
9132                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
9133                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
9134                                                                                         @2),
9135                                                 (Node *) makeA_Expr(AEXPR_OR, NIL,
9136                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
9137                                                     (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
9138                                                                                         @2),
9139                                                                                          @2);
9140                                 }
9141                         | a_expr IN_P in_expr
9142                                 {
9143                                         /* in_expr returns a SubLink or a list of a_exprs */
9144                                         if (IsA($3, SubLink))
9145                                         {
9146                                                 /* generate foo = ANY (subquery) */
9147                                                 SubLink *n = (SubLink *) $3;
9148                                                 n->subLinkType = ANY_SUBLINK;
9149                                                 n->testexpr = $1;
9150                                                 n->operName = list_make1(makeString("="));
9151                                                 n->location = @2;
9152                                                 $$ = (Node *)n;
9153                                         }
9154                                         else
9155                                         {
9156                                                 /* generate scalar IN expression */
9157                                                 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
9158                                         }
9159                                 }
9160                         | a_expr NOT IN_P in_expr
9161                                 {
9162                                         /* in_expr returns a SubLink or a list of a_exprs */
9163                                         if (IsA($4, SubLink))
9164                                         {
9165                                                 /* generate NOT (foo = ANY (subquery)) */
9166                                                 /* Make an = ANY node */
9167                                                 SubLink *n = (SubLink *) $4;
9168                                                 n->subLinkType = ANY_SUBLINK;
9169                                                 n->testexpr = $1;
9170                                                 n->operName = list_make1(makeString("="));
9171                                                 n->location = @3;
9172                                                 /* Stick a NOT on top */
9173                                                 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
9174                                         }
9175                                         else
9176                                         {
9177                                                 /* generate scalar NOT IN expression */
9178                                                 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
9179                                         }
9180                                 }
9181                         | a_expr subquery_Op sub_type select_with_parens        %prec Op
9182                                 {
9183                                         SubLink *n = makeNode(SubLink);
9184                                         n->subLinkType = $3;
9185                                         n->testexpr = $1;
9186                                         n->operName = $2;
9187                                         n->subselect = $4;
9188                                         n->location = @2;
9189                                         $$ = (Node *)n;
9190                                 }
9191                         | a_expr subquery_Op sub_type '(' a_expr ')'            %prec Op
9192                                 {
9193                                         if ($3 == ANY_SUBLINK)
9194                                                 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
9195                                         else
9196                                                 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
9197                                 }
9198                         | UNIQUE select_with_parens
9199                                 {
9200                                         /* Not sure how to get rid of the parentheses
9201                                          * but there are lots of shift/reduce errors without them.
9202                                          *
9203                                          * Should be able to implement this by plopping the entire
9204                                          * select into a node, then transforming the target expressions
9205                                          * from whatever they are into count(*), and testing the
9206                                          * entire result equal to one.
9207                                          * But, will probably implement a separate node in the executor.
9208                                          */
9209                                         ereport(ERROR,
9210                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9211                                                          errmsg("UNIQUE predicate is not yet implemented"),
9212                                                          parser_errposition(@1)));
9213                                 }
9214                         | a_expr IS DOCUMENT_P                                  %prec IS
9215                                 {
9216                                         $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
9217                                                                          list_make1($1), @2);
9218                                 }
9219                         | a_expr IS NOT DOCUMENT_P                              %prec IS
9220                                 {
9221                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
9222                                                                                          makeXmlExpr(IS_DOCUMENT, NULL, NIL,
9223                                                                                                                  list_make1($1), @2),
9224                                                                                          @2);
9225                                 }
9226                 ;
9227
9228 /*
9229  * Restricted expressions
9230  *
9231  * b_expr is a subset of the complete expression syntax defined by a_expr.
9232  *
9233  * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
9234  * cause trouble in the places where b_expr is used.  For simplicity, we
9235  * just eliminate all the boolean-keyword-operator productions from b_expr.
9236  */
9237 b_expr:         c_expr
9238                                 { $$ = $1; }
9239                         | b_expr TYPECAST Typename
9240                                 { $$ = makeTypeCast($1, $3, @2); }
9241                         | '+' b_expr                                    %prec UMINUS
9242                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
9243                         | '-' b_expr                                    %prec UMINUS
9244                                 { $$ = doNegate($2, @1); }
9245                         | b_expr '+' b_expr
9246                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
9247                         | b_expr '-' b_expr
9248                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
9249                         | b_expr '*' b_expr
9250                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
9251                         | b_expr '/' b_expr
9252                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
9253                         | b_expr '%' b_expr
9254                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
9255                         | b_expr '^' b_expr
9256                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
9257                         | b_expr '<' b_expr
9258                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
9259                         | b_expr '>' b_expr
9260                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
9261                         | b_expr '=' b_expr
9262                                 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
9263                         | b_expr qual_Op b_expr                         %prec Op
9264                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
9265                         | qual_Op b_expr                                        %prec Op
9266                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
9267                         | b_expr qual_Op                                        %prec POSTFIXOP
9268                                 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
9269                         | b_expr IS DISTINCT FROM b_expr                %prec IS
9270                                 {
9271                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
9272                                 }
9273                         | b_expr IS NOT DISTINCT FROM b_expr    %prec IS
9274                                 {
9275                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
9276                                                 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
9277                                 }
9278                         | b_expr IS OF '(' type_list ')'                %prec IS
9279                                 {
9280                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
9281                                 }
9282                         | b_expr IS NOT OF '(' type_list ')'    %prec IS
9283                                 {
9284                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
9285                                 }
9286                         | b_expr IS DOCUMENT_P                                  %prec IS
9287                                 {
9288                                         $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
9289                                                                          list_make1($1), @2);
9290                                 }
9291                         | b_expr IS NOT DOCUMENT_P                              %prec IS
9292                                 {
9293                                         $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
9294                                                                                          makeXmlExpr(IS_DOCUMENT, NULL, NIL,
9295                                                                                                                  list_make1($1), @2),
9296                                                                                          @2);
9297                                 }
9298                 ;
9299
9300 /*
9301  * Productions that can be used in both a_expr and b_expr.
9302  *
9303  * Note: productions that refer recursively to a_expr or b_expr mostly
9304  * cannot appear here.  However, it's OK to refer to a_exprs that occur
9305  * inside parentheses, such as function arguments; that cannot introduce
9306  * ambiguity to the b_expr syntax.
9307  */
9308 c_expr:         columnref                                                               { $$ = $1; }
9309                         | AexprConst                                                    { $$ = $1; }
9310                         | PARAM opt_indirection
9311                                 {
9312                                         ParamRef *p = makeNode(ParamRef);
9313                                         p->number = $1;
9314                                         p->location = @1;
9315                                         if ($2)
9316                                         {
9317                                                 A_Indirection *n = makeNode(A_Indirection);
9318                                                 n->arg = (Node *) p;
9319                                                 n->indirection = check_indirection($2, yyscanner);
9320                                                 $$ = (Node *) n;
9321                                         }
9322                                         else
9323                                                 $$ = (Node *) p;
9324                                 }
9325                         | '(' a_expr ')' opt_indirection
9326                                 {
9327                                         if ($4)
9328                                         {
9329                                                 A_Indirection *n = makeNode(A_Indirection);
9330                                                 n->arg = $2;
9331                                                 n->indirection = check_indirection($4, yyscanner);
9332                                                 $$ = (Node *)n;
9333                                         }
9334                                         else
9335                                                 $$ = $2;
9336                                 }
9337                         | case_expr
9338                                 { $$ = $1; }
9339                         | func_expr
9340                                 { $$ = $1; }
9341                         | select_with_parens                    %prec UMINUS
9342                                 {
9343                                         SubLink *n = makeNode(SubLink);
9344                                         n->subLinkType = EXPR_SUBLINK;
9345                                         n->testexpr = NULL;
9346                                         n->operName = NIL;
9347                                         n->subselect = $1;
9348                                         n->location = @1;
9349                                         $$ = (Node *)n;
9350                                 }
9351                         | EXISTS select_with_parens
9352                                 {
9353                                         SubLink *n = makeNode(SubLink);
9354                                         n->subLinkType = EXISTS_SUBLINK;
9355                                         n->testexpr = NULL;
9356                                         n->operName = NIL;
9357                                         n->subselect = $2;
9358                                         n->location = @1;
9359                                         $$ = (Node *)n;
9360                                 }
9361                         | ARRAY select_with_parens
9362                                 {
9363                                         SubLink *n = makeNode(SubLink);
9364                                         n->subLinkType = ARRAY_SUBLINK;
9365                                         n->testexpr = NULL;
9366                                         n->operName = NIL;
9367                                         n->subselect = $2;
9368                                         n->location = @1;
9369                                         $$ = (Node *)n;
9370                                 }
9371                         | ARRAY array_expr
9372                                 {
9373                                         A_ArrayExpr *n = (A_ArrayExpr *) $2;
9374                                         Assert(IsA(n, A_ArrayExpr));
9375                                         /* point outermost A_ArrayExpr to the ARRAY keyword */
9376                                         n->location = @1;
9377                                         $$ = (Node *)n;
9378                                 }
9379                         | row
9380                                 {
9381                                         RowExpr *r = makeNode(RowExpr);
9382                                         r->args = $1;
9383                                         r->row_typeid = InvalidOid;     /* not analyzed yet */
9384                                         r->location = @1;
9385                                         $$ = (Node *)r;
9386                                 }
9387                 ;
9388
9389 /*
9390  * func_expr is split out from c_expr just so that we have a classification
9391  * for "everything that is a function call or looks like one".  This isn't
9392  * very important, but it saves us having to document which variants are
9393  * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
9394  * (Note that many of the special SQL functions wouldn't actually make any
9395  * sense as functional index entries, but we ignore that consideration here.)
9396  */
9397 func_expr:      func_name '(' ')' over_clause
9398                                 {
9399                                         FuncCall *n = makeNode(FuncCall);
9400                                         n->funcname = $1;
9401                                         n->args = NIL;
9402                                         n->agg_order = NIL;
9403                                         n->agg_star = FALSE;
9404                                         n->agg_distinct = FALSE;
9405                                         n->func_variadic = FALSE;
9406                                         n->over = $4;
9407                                         n->location = @1;
9408                                         $$ = (Node *)n;
9409                                 }
9410                         | func_name '(' func_arg_list ')' over_clause
9411                                 {
9412                                         FuncCall *n = makeNode(FuncCall);
9413                                         n->funcname = $1;
9414                                         n->args = $3;
9415                                         n->agg_order = NIL;
9416                                         n->agg_star = FALSE;
9417                                         n->agg_distinct = FALSE;
9418                                         n->func_variadic = FALSE;
9419                                         n->over = $5;
9420                                         n->location = @1;
9421                                         $$ = (Node *)n;
9422                                 }
9423                         | func_name '(' VARIADIC func_arg_expr ')' over_clause
9424                                 {
9425                                         FuncCall *n = makeNode(FuncCall);
9426                                         n->funcname = $1;
9427                                         n->args = list_make1($4);
9428                                         n->agg_order = NIL;
9429                                         n->agg_star = FALSE;
9430                                         n->agg_distinct = FALSE;
9431                                         n->func_variadic = TRUE;
9432                                         n->over = $6;
9433                                         n->location = @1;
9434                                         $$ = (Node *)n;
9435                                 }
9436                         | func_name '(' func_arg_list ',' VARIADIC func_arg_expr ')' over_clause
9437                                 {
9438                                         FuncCall *n = makeNode(FuncCall);
9439                                         n->funcname = $1;
9440                                         n->args = lappend($3, $6);
9441                                         n->agg_order = NIL;
9442                                         n->agg_star = FALSE;
9443                                         n->agg_distinct = FALSE;
9444                                         n->func_variadic = TRUE;
9445                                         n->over = $8;
9446                                         n->location = @1;
9447                                         $$ = (Node *)n;
9448                                 }
9449                         | func_name '(' func_arg_list sort_clause ')' over_clause
9450                                 {
9451                                         FuncCall *n = makeNode(FuncCall);
9452                                         n->funcname = $1;
9453                                         n->args = $3;
9454                                         n->agg_order = $4;
9455                                         n->agg_star = FALSE;
9456                                         n->agg_distinct = FALSE;
9457                                         n->func_variadic = FALSE;
9458                                         n->over = $6;
9459                                         n->location = @1;
9460                                         $$ = (Node *)n;
9461                                 }
9462                         | func_name '(' ALL func_arg_list opt_sort_clause ')' over_clause
9463                                 {
9464                                         FuncCall *n = makeNode(FuncCall);
9465                                         n->funcname = $1;
9466                                         n->args = $4;
9467                                         n->agg_order = $5;
9468                                         n->agg_star = FALSE;
9469                                         n->agg_distinct = FALSE;
9470                                         /* Ideally we'd mark the FuncCall node to indicate
9471                                          * "must be an aggregate", but there's no provision
9472                                          * for that in FuncCall at the moment.
9473                                          */
9474                                         n->func_variadic = FALSE;
9475                                         n->over = $7;
9476                                         n->location = @1;
9477                                         $$ = (Node *)n;
9478                                 }
9479                         | func_name '(' DISTINCT func_arg_list opt_sort_clause ')' over_clause
9480                                 {
9481                                         FuncCall *n = makeNode(FuncCall);
9482                                         n->funcname = $1;
9483                                         n->args = $4;
9484                                         n->agg_order = $5;
9485                                         n->agg_star = FALSE;
9486                                         n->agg_distinct = TRUE;
9487                                         n->func_variadic = FALSE;
9488                                         n->over = $7;
9489                                         n->location = @1;
9490                                         $$ = (Node *)n;
9491                                 }
9492                         | func_name '(' '*' ')' over_clause
9493                                 {
9494                                         /*
9495                                          * We consider AGGREGATE(*) to invoke a parameterless
9496                                          * aggregate.  This does the right thing for COUNT(*),
9497                                          * and there are no other aggregates in SQL92 that accept
9498                                          * '*' as parameter.
9499                                          *
9500                                          * The FuncCall node is also marked agg_star = true,
9501                                          * so that later processing can detect what the argument
9502                                          * really was.
9503                                          */
9504                                         FuncCall *n = makeNode(FuncCall);
9505                                         n->funcname = $1;
9506                                         n->args = NIL;
9507                                         n->agg_order = NIL;
9508                                         n->agg_star = TRUE;
9509                                         n->agg_distinct = FALSE;
9510                                         n->func_variadic = FALSE;
9511                                         n->over = $5;
9512                                         n->location = @1;
9513                                         $$ = (Node *)n;
9514                                 }
9515                         | CURRENT_DATE
9516                                 {
9517                                         /*
9518                                          * Translate as "'now'::text::date".
9519                                          *
9520                                          * We cannot use "'now'::date" because coerce_type() will
9521                                          * immediately reduce that to a constant representing
9522                                          * today's date.  We need to delay the conversion until
9523                                          * runtime, else the wrong things will happen when
9524                                          * CURRENT_DATE is used in a column default value or rule.
9525                                          *
9526                                          * This could be simplified if we had a way to generate
9527                                          * an expression tree representing runtime application
9528                                          * of type-input conversion functions.  (As of PG 7.3
9529                                          * that is actually possible, but not clear that we want
9530                                          * to rely on it.)
9531                                          */
9532                                         Node *n;
9533                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9534                                         $$ = makeTypeCast(n, SystemTypeName("date"), -1);
9535                                 }
9536                         | CURRENT_TIME
9537                                 {
9538                                         /*
9539                                          * Translate as "'now'::text::timetz".
9540                                          * See comments for CURRENT_DATE.
9541                                          */
9542                                         Node *n;
9543                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9544                                         $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
9545                                 }
9546                         | CURRENT_TIME '(' Iconst ')'
9547                                 {
9548                                         /*
9549                                          * Translate as "'now'::text::timetz(n)".
9550                                          * See comments for CURRENT_DATE.
9551                                          */
9552                                         Node *n;
9553                                         TypeName *d;
9554                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9555                                         d = SystemTypeName("timetz");
9556                                         d->typmods = list_make1(makeIntConst($3, @3));
9557                                         $$ = makeTypeCast(n, d, -1);
9558                                 }
9559                         | CURRENT_TIMESTAMP
9560                                 {
9561                                         /*
9562                                          * Translate as "now()", since we have a function that
9563                                          * does exactly what is needed.
9564                                          */
9565                                         FuncCall *n = makeNode(FuncCall);
9566                                         n->funcname = SystemFuncName("now");
9567                                         n->args = NIL;
9568                                         n->agg_order = NIL;
9569                                         n->agg_star = FALSE;
9570                                         n->agg_distinct = FALSE;
9571                                         n->func_variadic = FALSE;
9572                                         n->over = NULL;
9573                                         n->location = @1;
9574                                         $$ = (Node *)n;
9575                                 }
9576                         | CURRENT_TIMESTAMP '(' Iconst ')'
9577                                 {
9578                                         /*
9579                                          * Translate as "'now'::text::timestamptz(n)".
9580                                          * See comments for CURRENT_DATE.
9581                                          */
9582                                         Node *n;
9583                                         TypeName *d;
9584                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9585                                         d = SystemTypeName("timestamptz");
9586                                         d->typmods = list_make1(makeIntConst($3, @3));
9587                                         $$ = makeTypeCast(n, d, -1);
9588                                 }
9589                         | LOCALTIME
9590                                 {
9591                                         /*
9592                                          * Translate as "'now'::text::time".
9593                                          * See comments for CURRENT_DATE.
9594                                          */
9595                                         Node *n;
9596                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9597                                         $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
9598                                 }
9599                         | LOCALTIME '(' Iconst ')'
9600                                 {
9601                                         /*
9602                                          * Translate as "'now'::text::time(n)".
9603                                          * See comments for CURRENT_DATE.
9604                                          */
9605                                         Node *n;
9606                                         TypeName *d;
9607                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9608                                         d = SystemTypeName("time");
9609                                         d->typmods = list_make1(makeIntConst($3, @3));
9610                                         $$ = makeTypeCast((Node *)n, d, -1);
9611                                 }
9612                         | LOCALTIMESTAMP
9613                                 {
9614                                         /*
9615                                          * Translate as "'now'::text::timestamp".
9616                                          * See comments for CURRENT_DATE.
9617                                          */
9618                                         Node *n;
9619                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9620                                         $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
9621                                 }
9622                         | LOCALTIMESTAMP '(' Iconst ')'
9623                                 {
9624                                         /*
9625                                          * Translate as "'now'::text::timestamp(n)".
9626                                          * See comments for CURRENT_DATE.
9627                                          */
9628                                         Node *n;
9629                                         TypeName *d;
9630                                         n = makeStringConstCast("now", @1, SystemTypeName("text"));
9631                                         d = SystemTypeName("timestamp");
9632                                         d->typmods = list_make1(makeIntConst($3, @3));
9633                                         $$ = makeTypeCast(n, d, -1);
9634                                 }
9635                         | CURRENT_ROLE
9636                                 {
9637                                         FuncCall *n = makeNode(FuncCall);
9638                                         n->funcname = SystemFuncName("current_user");
9639                                         n->args = NIL;
9640                                         n->agg_order = NIL;
9641                                         n->agg_star = FALSE;
9642                                         n->agg_distinct = FALSE;
9643                                         n->func_variadic = FALSE;
9644                                         n->over = NULL;
9645                                         n->location = @1;
9646                                         $$ = (Node *)n;
9647                                 }
9648                         | CURRENT_USER
9649                                 {
9650                                         FuncCall *n = makeNode(FuncCall);
9651                                         n->funcname = SystemFuncName("current_user");
9652                                         n->args = NIL;
9653                                         n->agg_order = NIL;
9654                                         n->agg_star = FALSE;
9655                                         n->agg_distinct = FALSE;
9656                                         n->func_variadic = FALSE;
9657                                         n->over = NULL;
9658                                         n->location = @1;
9659                                         $$ = (Node *)n;
9660                                 }
9661                         | SESSION_USER
9662                                 {
9663                                         FuncCall *n = makeNode(FuncCall);
9664                                         n->funcname = SystemFuncName("session_user");
9665                                         n->args = NIL;
9666                                         n->agg_order = NIL;
9667                                         n->agg_star = FALSE;
9668                                         n->agg_distinct = FALSE;
9669                                         n->func_variadic = FALSE;
9670                                         n->over = NULL;
9671                                         n->location = @1;
9672                                         $$ = (Node *)n;
9673                                 }
9674                         | USER
9675                                 {
9676                                         FuncCall *n = makeNode(FuncCall);
9677                                         n->funcname = SystemFuncName("current_user");
9678                                         n->args = NIL;
9679                                         n->agg_order = NIL;
9680                                         n->agg_star = FALSE;
9681                                         n->agg_distinct = FALSE;
9682                                         n->func_variadic = FALSE;
9683                                         n->over = NULL;
9684                                         n->location = @1;
9685                                         $$ = (Node *)n;
9686                                 }
9687                         | CURRENT_CATALOG
9688                                 {
9689                                         FuncCall *n = makeNode(FuncCall);
9690                                         n->funcname = SystemFuncName("current_database");
9691                                         n->args = NIL;
9692                                         n->agg_order = NIL;
9693                                         n->agg_star = FALSE;
9694                                         n->agg_distinct = FALSE;
9695                                         n->func_variadic = FALSE;
9696                                         n->over = NULL;
9697                                         n->location = @1;
9698                                         $$ = (Node *)n;
9699                                 }
9700                         | CURRENT_SCHEMA
9701                                 {
9702                                         FuncCall *n = makeNode(FuncCall);
9703                                         n->funcname = SystemFuncName("current_schema");
9704                                         n->args = NIL;
9705                                         n->agg_order = NIL;
9706                                         n->agg_star = FALSE;
9707                                         n->agg_distinct = FALSE;
9708                                         n->func_variadic = FALSE;
9709                                         n->over = NULL;
9710                                         n->location = @1;
9711                                         $$ = (Node *)n;
9712                                 }
9713                         | CAST '(' a_expr AS Typename ')'
9714                                 { $$ = makeTypeCast($3, $5, @1); }
9715                         | EXTRACT '(' extract_list ')'
9716                                 {
9717                                         FuncCall *n = makeNode(FuncCall);
9718                                         n->funcname = SystemFuncName("date_part");
9719                                         n->args = $3;
9720                                         n->agg_order = NIL;
9721                                         n->agg_star = FALSE;
9722                                         n->agg_distinct = FALSE;
9723                                         n->func_variadic = FALSE;
9724                                         n->over = NULL;
9725                                         n->location = @1;
9726                                         $$ = (Node *)n;
9727                                 }
9728                         | OVERLAY '(' overlay_list ')'
9729                                 {
9730                                         /* overlay(A PLACING B FROM C FOR D) is converted to
9731                                          * overlay(A, B, C, D)
9732                                          * overlay(A PLACING B FROM C) is converted to
9733                                          * overlay(A, B, C)
9734                                          */
9735                                         FuncCall *n = makeNode(FuncCall);
9736                                         n->funcname = SystemFuncName("overlay");
9737                                         n->args = $3;
9738                                         n->agg_order = NIL;
9739                                         n->agg_star = FALSE;
9740                                         n->agg_distinct = FALSE;
9741                                         n->func_variadic = FALSE;
9742                                         n->over = NULL;
9743                                         n->location = @1;
9744                                         $$ = (Node *)n;
9745                                 }
9746                         | POSITION '(' position_list ')'
9747                                 {
9748                                         /* position(A in B) is converted to position(B, A) */
9749                                         FuncCall *n = makeNode(FuncCall);
9750                                         n->funcname = SystemFuncName("position");
9751                                         n->args = $3;
9752                                         n->agg_order = NIL;
9753                                         n->agg_star = FALSE;
9754                                         n->agg_distinct = FALSE;
9755                                         n->func_variadic = FALSE;
9756                                         n->over = NULL;
9757                                         n->location = @1;
9758                                         $$ = (Node *)n;
9759                                 }
9760                         | SUBSTRING '(' substr_list ')'
9761                                 {
9762                                         /* substring(A from B for C) is converted to
9763                                          * substring(A, B, C) - thomas 2000-11-28
9764                                          */
9765                                         FuncCall *n = makeNode(FuncCall);
9766                                         n->funcname = SystemFuncName("substring");
9767                                         n->args = $3;
9768                                         n->agg_order = NIL;
9769                                         n->agg_star = FALSE;
9770                                         n->agg_distinct = FALSE;
9771                                         n->func_variadic = FALSE;
9772                                         n->over = NULL;
9773                                         n->location = @1;
9774                                         $$ = (Node *)n;
9775                                 }
9776                         | TREAT '(' a_expr AS Typename ')'
9777                                 {
9778                                         /* TREAT(expr AS target) converts expr of a particular type to target,
9779                                          * which is defined to be a subtype of the original expression.
9780                                          * In SQL99, this is intended for use with structured UDTs,
9781                                          * but let's make this a generally useful form allowing stronger
9782                                          * coercions than are handled by implicit casting.
9783                                          */
9784                                         FuncCall *n = makeNode(FuncCall);
9785                                         /* Convert SystemTypeName() to SystemFuncName() even though
9786                                          * at the moment they result in the same thing.
9787                                          */
9788                                         n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
9789                                         n->args = list_make1($3);
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 = @1;
9796                                         $$ = (Node *)n;
9797                                 }
9798                         | TRIM '(' BOTH trim_list ')'
9799                                 {
9800                                         /* various trim expressions are defined in SQL92
9801                                          * - thomas 1997-07-19
9802                                          */
9803                                         FuncCall *n = makeNode(FuncCall);
9804                                         n->funcname = SystemFuncName("btrim");
9805                                         n->args = $4;
9806                                         n->agg_order = NIL;
9807                                         n->agg_star = FALSE;
9808                                         n->agg_distinct = FALSE;
9809                                         n->func_variadic = FALSE;
9810                                         n->over = NULL;
9811                                         n->location = @1;
9812                                         $$ = (Node *)n;
9813                                 }
9814                         | TRIM '(' LEADING trim_list ')'
9815                                 {
9816                                         FuncCall *n = makeNode(FuncCall);
9817                                         n->funcname = SystemFuncName("ltrim");
9818                                         n->args = $4;
9819                                         n->agg_order = NIL;
9820                                         n->agg_star = FALSE;
9821                                         n->agg_distinct = FALSE;
9822                                         n->func_variadic = FALSE;
9823                                         n->over = NULL;
9824                                         n->location = @1;
9825                                         $$ = (Node *)n;
9826                                 }
9827                         | TRIM '(' TRAILING trim_list ')'
9828                                 {
9829                                         FuncCall *n = makeNode(FuncCall);
9830                                         n->funcname = SystemFuncName("rtrim");
9831                                         n->args = $4;
9832                                         n->agg_order = NIL;
9833                                         n->agg_star = FALSE;
9834                                         n->agg_distinct = FALSE;
9835                                         n->func_variadic = FALSE;
9836                                         n->over = NULL;
9837                                         n->location = @1;
9838                                         $$ = (Node *)n;
9839                                 }
9840                         | TRIM '(' trim_list ')'
9841                                 {
9842                                         FuncCall *n = makeNode(FuncCall);
9843                                         n->funcname = SystemFuncName("btrim");
9844                                         n->args = $3;
9845                                         n->agg_order = NIL;
9846                                         n->agg_star = FALSE;
9847                                         n->agg_distinct = FALSE;
9848                                         n->func_variadic = FALSE;
9849                                         n->over = NULL;
9850                                         n->location = @1;
9851                                         $$ = (Node *)n;
9852                                 }
9853                         | NULLIF '(' a_expr ',' a_expr ')'
9854                                 {
9855                                         $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
9856                                 }
9857                         | COALESCE '(' expr_list ')'
9858                                 {
9859                                         CoalesceExpr *c = makeNode(CoalesceExpr);
9860                                         c->args = $3;
9861                                         c->location = @1;
9862                                         $$ = (Node *)c;
9863                                 }
9864                         | GREATEST '(' expr_list ')'
9865                                 {
9866                                         MinMaxExpr *v = makeNode(MinMaxExpr);
9867                                         v->args = $3;
9868                                         v->op = IS_GREATEST;
9869                                         v->location = @1;
9870                                         $$ = (Node *)v;
9871                                 }
9872                         | LEAST '(' expr_list ')'
9873                                 {
9874                                         MinMaxExpr *v = makeNode(MinMaxExpr);
9875                                         v->args = $3;
9876                                         v->op = IS_LEAST;
9877                                         v->location = @1;
9878                                         $$ = (Node *)v;
9879                                 }
9880                         | XMLCONCAT '(' expr_list ')'
9881                                 {
9882                                         $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
9883                                 }
9884                         | XMLELEMENT '(' NAME_P ColLabel ')'
9885                                 {
9886                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
9887                                 }
9888                         | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
9889                                 {
9890                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
9891                                 }
9892                         | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
9893                                 {
9894                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
9895                                 }
9896                         | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
9897                                 {
9898                                         $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
9899                                 }
9900                         | XMLEXISTS '(' c_expr xmlexists_argument ')'
9901                                 {
9902                                         /* xmlexists(A PASSING [BY REF] B [BY REF]) is
9903                                          * converted to xmlexists(A, B)*/
9904                                         FuncCall *n = makeNode(FuncCall);
9905                                         n->funcname = SystemFuncName("xmlexists");
9906                                         n->args = list_make2($3, $4);
9907                                         n->agg_order = NIL;
9908                                         n->agg_star = FALSE;
9909                                         n->agg_distinct = FALSE;
9910                                         n->func_variadic = FALSE;
9911                                         n->over = NULL;
9912                                         n->location = @1;
9913                                         $$ = (Node *)n;
9914                                 }
9915                         | XMLFOREST '(' xml_attribute_list ')'
9916                                 {
9917                                         $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
9918                                 }
9919                         | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
9920                                 {
9921                                         XmlExpr *x = (XmlExpr *)
9922                                                 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
9923                                                                         list_make2($4, makeBoolAConst($5, -1)),
9924                                                                         @1);
9925                                         x->xmloption = $3;
9926                                         $$ = (Node *)x;
9927                                 }
9928                         | XMLPI '(' NAME_P ColLabel ')'
9929                                 {
9930                                         $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
9931                                 }
9932                         | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
9933                                 {
9934                                         $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
9935                                 }
9936                         | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
9937                                 {
9938                                         $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
9939                                                                          list_make3($3, $5, $6), @1);
9940                                 }
9941                         | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
9942                                 {
9943                                         XmlSerialize *n = makeNode(XmlSerialize);
9944                                         n->xmloption = $3;
9945                                         n->expr = $4;
9946                                         n->typeName = $6;
9947                                         n->location = @1;
9948                                         $$ = (Node *)n;
9949                                 }
9950                 ;
9951
9952 /*
9953  * SQL/XML support
9954  */
9955 xml_root_version: VERSION_P a_expr
9956                                 { $$ = $2; }
9957                         | VERSION_P NO VALUE_P
9958                                 { $$ = makeNullAConst(-1); }
9959                 ;
9960
9961 opt_xml_root_standalone: ',' STANDALONE_P YES_P
9962                                 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
9963                         | ',' STANDALONE_P NO
9964                                 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
9965                         | ',' STANDALONE_P NO VALUE_P
9966                                 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
9967                         | /*EMPTY*/
9968                                 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
9969                 ;
9970
9971 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')'        { $$ = $3; }
9972                 ;
9973
9974 xml_attribute_list:     xml_attribute_el                                        { $$ = list_make1($1); }
9975                         | xml_attribute_list ',' xml_attribute_el       { $$ = lappend($1, $3); }
9976                 ;
9977
9978 xml_attribute_el: a_expr AS ColLabel
9979                                 {
9980                                         $$ = makeNode(ResTarget);
9981                                         $$->name = $3;
9982                                         $$->indirection = NIL;
9983                                         $$->val = (Node *) $1;
9984                                         $$->location = @1;
9985                                 }
9986                         | a_expr
9987                                 {
9988                                         $$ = makeNode(ResTarget);
9989                                         $$->name = NULL;
9990                                         $$->indirection = NIL;
9991                                         $$->val = (Node *) $1;
9992                                         $$->location = @1;
9993                                 }
9994                 ;
9995
9996 document_or_content: DOCUMENT_P                                         { $$ = XMLOPTION_DOCUMENT; }
9997                         | CONTENT_P                                                             { $$ = XMLOPTION_CONTENT; }
9998                 ;
9999
10000 xml_whitespace_option: PRESERVE WHITESPACE_P            { $$ = TRUE; }
10001                         | STRIP_P WHITESPACE_P                                  { $$ = FALSE; }
10002                         | /*EMPTY*/                                                             { $$ = FALSE; }
10003                 ;
10004
10005 /* We allow several variants for SQL and other compatibility. */
10006 xmlexists_argument:
10007                         PASSING c_expr
10008                                 {
10009                                         $$ = $2;
10010                                 }
10011                         | PASSING c_expr BY REF
10012                                 {
10013                                         $$ = $2;
10014                                 }
10015                         | PASSING BY REF c_expr
10016                                 {
10017                                         $$ = $4;
10018                                 }
10019                         | PASSING BY REF c_expr BY REF
10020                                 {
10021                                         $$ = $4;
10022                                 }
10023                 ;
10024
10025
10026 /*
10027  * Window Definitions
10028  */
10029 window_clause:
10030                         WINDOW window_definition_list                   { $$ = $2; }
10031                         | /*EMPTY*/                                                             { $$ = NIL; }
10032                 ;
10033
10034 window_definition_list:
10035                         window_definition                                               { $$ = list_make1($1); }
10036                         | window_definition_list ',' window_definition
10037                                                                                                         { $$ = lappend($1, $3); }
10038                 ;
10039
10040 window_definition:
10041                         ColId AS window_specification
10042                                 {
10043                                         WindowDef *n = $3;
10044                                         n->name = $1;
10045                                         $$ = n;
10046                                 }
10047                 ;
10048
10049 over_clause: OVER window_specification
10050                                 { $$ = $2; }
10051                         | OVER ColId
10052                                 {
10053                                         WindowDef *n = makeNode(WindowDef);
10054                                         n->name = $2;
10055                                         n->refname = NULL;
10056                                         n->partitionClause = NIL;
10057                                         n->orderClause = NIL;
10058                                         n->frameOptions = FRAMEOPTION_DEFAULTS;
10059                                         n->startOffset = NULL;
10060                                         n->endOffset = NULL;
10061                                         n->location = @2;
10062                                         $$ = n;
10063                                 }
10064                         | /*EMPTY*/
10065                                 { $$ = NULL; }
10066                 ;
10067
10068 window_specification: '(' opt_existing_window_name opt_partition_clause
10069                                                 opt_sort_clause opt_frame_clause ')'
10070                                 {
10071                                         WindowDef *n = makeNode(WindowDef);
10072                                         n->name = NULL;
10073                                         n->refname = $2;
10074                                         n->partitionClause = $3;
10075                                         n->orderClause = $4;
10076                                         /* copy relevant fields of opt_frame_clause */
10077                                         n->frameOptions = $5->frameOptions;
10078                                         n->startOffset = $5->startOffset;
10079                                         n->endOffset = $5->endOffset;
10080                                         n->location = @1;
10081                                         $$ = n;
10082                                 }
10083                 ;
10084
10085 /*
10086  * If we see PARTITION, RANGE, or ROWS as the first token after the '('
10087  * of a window_specification, we want the assumption to be that there is
10088  * no existing_window_name; but those keywords are unreserved and so could
10089  * be ColIds.  We fix this by making them have the same precedence as IDENT
10090  * and giving the empty production here a slightly higher precedence, so
10091  * that the shift/reduce conflict is resolved in favor of reducing the rule.
10092  * These keywords are thus precluded from being an existing_window_name but
10093  * are not reserved for any other purpose.
10094  */
10095 opt_existing_window_name: ColId                                         { $$ = $1; }
10096                         | /*EMPTY*/                             %prec Op                { $$ = NULL; }
10097                 ;
10098
10099 opt_partition_clause: PARTITION BY expr_list            { $$ = $3; }
10100                         | /*EMPTY*/                                                             { $$ = NIL; }
10101                 ;
10102
10103 /*
10104  * For frame clauses, we return a WindowDef, but only some fields are used:
10105  * frameOptions, startOffset, and endOffset.
10106  *
10107  * This is only a subset of the full SQL:2008 frame_clause grammar.
10108  * We don't support <window frame exclusion> yet.
10109  */
10110 opt_frame_clause:
10111                         RANGE frame_extent
10112                                 {
10113                                         WindowDef *n = $2;
10114                                         n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE;
10115                                         if (n->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING |
10116                                                                                    FRAMEOPTION_END_VALUE_PRECEDING))
10117                                                 ereport(ERROR,
10118                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10119                                                                  errmsg("RANGE PRECEDING is only supported with UNBOUNDED"),
10120                                                                  parser_errposition(@1)));
10121                                         if (n->frameOptions & (FRAMEOPTION_START_VALUE_FOLLOWING |
10122                                                                                    FRAMEOPTION_END_VALUE_FOLLOWING))
10123                                                 ereport(ERROR,
10124                                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
10125                                                                  errmsg("RANGE FOLLOWING is only supported with UNBOUNDED"),
10126                                                                  parser_errposition(@1)));
10127                                         $$ = n;
10128                                 }
10129                         | ROWS frame_extent
10130                                 {
10131                                         WindowDef *n = $2;
10132                                         n->frameOptions |= FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS;
10133                                         $$ = n;
10134                                 }
10135                         | /*EMPTY*/
10136                                 {
10137                                         WindowDef *n = makeNode(WindowDef);
10138                                         n->frameOptions = FRAMEOPTION_DEFAULTS;
10139                                         n->startOffset = NULL;
10140                                         n->endOffset = NULL;
10141                                         $$ = n;
10142                                 }
10143                 ;
10144
10145 frame_extent: frame_bound
10146                                 {
10147                                         WindowDef *n = $1;
10148                                         /* reject invalid cases */
10149                                         if (n->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
10150                                                 ereport(ERROR,
10151                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10152                                                                  errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
10153                                                                  parser_errposition(@1)));
10154                                         if (n->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING)
10155                                                 ereport(ERROR,
10156                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10157                                                                  errmsg("frame starting from following row cannot end with current row"),
10158                                                                  parser_errposition(@1)));
10159                                         n->frameOptions |= FRAMEOPTION_END_CURRENT_ROW;
10160                                         $$ = n;
10161                                 }
10162                         | BETWEEN frame_bound AND frame_bound
10163                                 {
10164                                         WindowDef *n1 = $2;
10165                                         WindowDef *n2 = $4;
10166                                         /* form merged options */
10167                                         int             frameOptions = n1->frameOptions;
10168                                         /* shift converts START_ options to END_ options */
10169                                         frameOptions |= n2->frameOptions << 1;
10170                                         frameOptions |= FRAMEOPTION_BETWEEN;
10171                                         /* reject invalid cases */
10172                                         if (frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
10173                                                 ereport(ERROR,
10174                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10175                                                                  errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
10176                                                                  parser_errposition(@2)));
10177                                         if (frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING)
10178                                                 ereport(ERROR,
10179                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10180                                                                  errmsg("frame end cannot be UNBOUNDED PRECEDING"),
10181                                                                  parser_errposition(@4)));
10182                                         if ((frameOptions & FRAMEOPTION_START_CURRENT_ROW) &&
10183                                                 (frameOptions & FRAMEOPTION_END_VALUE_PRECEDING))
10184                                                 ereport(ERROR,
10185                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10186                                                                  errmsg("frame starting from current row cannot have preceding rows"),
10187                                                                  parser_errposition(@4)));
10188                                         if ((frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) &&
10189                                                 (frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING |
10190                                                                                  FRAMEOPTION_END_CURRENT_ROW)))
10191                                                 ereport(ERROR,
10192                                                                 (errcode(ERRCODE_WINDOWING_ERROR),
10193                                                                  errmsg("frame starting from following row cannot have preceding rows"),
10194                                                                  parser_errposition(@4)));
10195                                         n1->frameOptions = frameOptions;
10196                                         n1->endOffset = n2->startOffset;
10197                                         $$ = n1;
10198                                 }
10199                 ;
10200
10201 /*
10202  * This is used for both frame start and frame end, with output set up on
10203  * the assumption it's frame start; the frame_extent productions must reject
10204  * invalid cases.
10205  */
10206 frame_bound:
10207                         UNBOUNDED PRECEDING
10208                                 {
10209                                         WindowDef *n = makeNode(WindowDef);
10210                                         n->frameOptions = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
10211                                         n->startOffset = NULL;
10212                                         n->endOffset = NULL;
10213                                         $$ = n;
10214                                 }
10215                         | UNBOUNDED FOLLOWING
10216                                 {
10217                                         WindowDef *n = makeNode(WindowDef);
10218                                         n->frameOptions = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
10219                                         n->startOffset = NULL;
10220                                         n->endOffset = NULL;
10221                                         $$ = n;
10222                                 }
10223                         | CURRENT_P ROW
10224                                 {
10225                                         WindowDef *n = makeNode(WindowDef);
10226                                         n->frameOptions = FRAMEOPTION_START_CURRENT_ROW;
10227                                         n->startOffset = NULL;
10228                                         n->endOffset = NULL;
10229                                         $$ = n;
10230                                 }
10231                         | a_expr PRECEDING
10232                                 {
10233                                         WindowDef *n = makeNode(WindowDef);
10234                                         n->frameOptions = FRAMEOPTION_START_VALUE_PRECEDING;
10235                                         n->startOffset = $1;
10236                                         n->endOffset = NULL;
10237                                         $$ = n;
10238                                 }
10239                         | a_expr FOLLOWING
10240                                 {
10241                                         WindowDef *n = makeNode(WindowDef);
10242                                         n->frameOptions = FRAMEOPTION_START_VALUE_FOLLOWING;
10243                                         n->startOffset = $1;
10244                                         n->endOffset = NULL;
10245                                         $$ = n;
10246                                 }
10247                 ;
10248
10249
10250 /*
10251  * Supporting nonterminals for expressions.
10252  */
10253
10254 /* Explicit row production.
10255  *
10256  * SQL99 allows an optional ROW keyword, so we can now do single-element rows
10257  * without conflicting with the parenthesized a_expr production.  Without the
10258  * ROW keyword, there must be more than one a_expr inside the parens.
10259  */
10260 row:            ROW '(' expr_list ')'                                   { $$ = $3; }
10261                         | ROW '(' ')'                                                   { $$ = NIL; }
10262                         | '(' expr_list ',' a_expr ')'                  { $$ = lappend($2, $4); }
10263                 ;
10264
10265 sub_type:       ANY                                                                             { $$ = ANY_SUBLINK; }
10266                         | SOME                                                                  { $$ = ANY_SUBLINK; }
10267                         | ALL                                                                   { $$ = ALL_SUBLINK; }
10268                 ;
10269
10270 all_Op:         Op                                                                              { $$ = $1; }
10271                         | MathOp                                                                { $$ = $1; }
10272                 ;
10273
10274 MathOp:          '+'                                                                    { $$ = "+"; }
10275                         | '-'                                                                   { $$ = "-"; }
10276                         | '*'                                                                   { $$ = "*"; }
10277                         | '/'                                                                   { $$ = "/"; }
10278                         | '%'                                                                   { $$ = "%"; }
10279                         | '^'                                                                   { $$ = "^"; }
10280                         | '<'                                                                   { $$ = "<"; }
10281                         | '>'                                                                   { $$ = ">"; }
10282                         | '='                                                                   { $$ = "="; }
10283                 ;
10284
10285 qual_Op:        Op
10286                                         { $$ = list_make1(makeString($1)); }
10287                         | OPERATOR '(' any_operator ')'
10288                                         { $$ = $3; }
10289                 ;
10290
10291 qual_all_Op:
10292                         all_Op
10293                                         { $$ = list_make1(makeString($1)); }
10294                         | OPERATOR '(' any_operator ')'
10295                                         { $$ = $3; }
10296                 ;
10297
10298 subquery_Op:
10299                         all_Op
10300                                         { $$ = list_make1(makeString($1)); }
10301                         | OPERATOR '(' any_operator ')'
10302                                         { $$ = $3; }
10303                         | LIKE
10304                                         { $$ = list_make1(makeString("~~")); }
10305                         | NOT LIKE
10306                                         { $$ = list_make1(makeString("!~~")); }
10307                         | ILIKE
10308                                         { $$ = list_make1(makeString("~~*")); }
10309                         | NOT ILIKE
10310                                         { $$ = list_make1(makeString("!~~*")); }
10311 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
10312  * the regular expression is preprocessed by a function (similar_escape),
10313  * and the ~ operator for posix regular expressions is used.
10314  *        x SIMILAR TO y     ->    x ~ similar_escape(y)
10315  * this transformation is made on the fly by the parser upwards.
10316  * however the SubLink structure which handles any/some/all stuff
10317  * is not ready for such a thing.
10318  */
10319                         ;
10320
10321 expr_list:      a_expr
10322                                 {
10323                                         $$ = list_make1($1);
10324                                 }
10325                         | expr_list ',' a_expr
10326                                 {
10327                                         $$ = lappend($1, $3);
10328                                 }
10329                 ;
10330
10331 /* function arguments can have names */
10332 func_arg_list:  func_arg_expr
10333                                 {
10334                                         $$ = list_make1($1);
10335                                 }
10336                         | func_arg_list ',' func_arg_expr
10337                                 {
10338                                         $$ = lappend($1, $3);
10339                                 }
10340                 ;
10341
10342 func_arg_expr:  a_expr
10343                                 {
10344                                         $$ = $1;
10345                                 }
10346                         | param_name COLON_EQUALS a_expr
10347                                 {
10348                                         NamedArgExpr *na = makeNode(NamedArgExpr);
10349                                         na->name = $1;
10350                                         na->arg = (Expr *) $3;
10351                                         na->argnumber = -1;             /* until determined */
10352                                         na->location = @1;
10353                                         $$ = (Node *) na;
10354                                 }
10355                 ;
10356
10357 type_list:      Typename                                                                { $$ = list_make1($1); }
10358                         | type_list ',' Typename                                { $$ = lappend($1, $3); }
10359                 ;
10360
10361 array_expr: '[' expr_list ']'
10362                                 {
10363                                         $$ = makeAArrayExpr($2, @1);
10364                                 }
10365                         | '[' array_expr_list ']'
10366                                 {
10367                                         $$ = makeAArrayExpr($2, @1);
10368                                 }
10369                         | '[' ']'
10370                                 {
10371                                         $$ = makeAArrayExpr(NIL, @1);
10372                                 }
10373                 ;
10374
10375 array_expr_list: array_expr                                                     { $$ = list_make1($1); }
10376                         | array_expr_list ',' array_expr                { $$ = lappend($1, $3); }
10377                 ;
10378
10379
10380 extract_list:
10381                         extract_arg FROM a_expr
10382                                 {
10383                                         $$ = list_make2(makeStringConst($1, @1), $3);
10384                                 }
10385                         | /*EMPTY*/                                                             { $$ = NIL; }
10386                 ;
10387
10388 /* Allow delimited string Sconst in extract_arg as an SQL extension.
10389  * - thomas 2001-04-12
10390  */
10391 extract_arg:
10392                         IDENT                                                                   { $$ = $1; }
10393                         | YEAR_P                                                                { $$ = "year"; }
10394                         | MONTH_P                                                               { $$ = "month"; }
10395                         | DAY_P                                                                 { $$ = "day"; }
10396                         | HOUR_P                                                                { $$ = "hour"; }
10397                         | MINUTE_P                                                              { $$ = "minute"; }
10398                         | SECOND_P                                                              { $$ = "second"; }
10399                         | Sconst                                                                { $$ = $1; }
10400                 ;
10401
10402 /* OVERLAY() arguments
10403  * SQL99 defines the OVERLAY() function:
10404  * o overlay(text placing text from int for int)
10405  * o overlay(text placing text from int)
10406  * and similarly for binary strings
10407  */
10408 overlay_list:
10409                         a_expr overlay_placing substr_from substr_for
10410                                 {
10411                                         $$ = list_make4($1, $2, $3, $4);
10412                                 }
10413                         | a_expr overlay_placing substr_from
10414                                 {
10415                                         $$ = list_make3($1, $2, $3);
10416                                 }
10417                 ;
10418
10419 overlay_placing:
10420                         PLACING a_expr
10421                                 { $$ = $2; }
10422                 ;
10423
10424 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
10425
10426 position_list:
10427                         b_expr IN_P b_expr                                              { $$ = list_make2($3, $1); }
10428                         | /*EMPTY*/                                                             { $$ = NIL; }
10429                 ;
10430
10431 /* SUBSTRING() arguments
10432  * SQL9x defines a specific syntax for arguments to SUBSTRING():
10433  * o substring(text from int for int)
10434  * o substring(text from int) get entire string from starting point "int"
10435  * o substring(text for int) get first "int" characters of string
10436  * o substring(text from pattern) get entire string matching pattern
10437  * o substring(text from pattern for escape) same with specified escape char
10438  * We also want to support generic substring functions which accept
10439  * the usual generic list of arguments. So we will accept both styles
10440  * here, and convert the SQL9x style to the generic list for further
10441  * processing. - thomas 2000-11-28
10442  */
10443 substr_list:
10444                         a_expr substr_from substr_for
10445                                 {
10446                                         $$ = list_make3($1, $2, $3);
10447                                 }
10448                         | a_expr substr_for substr_from
10449                                 {
10450                                         /* not legal per SQL99, but might as well allow it */
10451                                         $$ = list_make3($1, $3, $2);
10452                                 }
10453                         | a_expr substr_from
10454                                 {
10455                                         $$ = list_make2($1, $2);
10456                                 }
10457                         | a_expr substr_for
10458                                 {
10459                                         /*
10460                                          * Since there are no cases where this syntax allows
10461                                          * a textual FOR value, we forcibly cast the argument
10462                                          * to int4.  The possible matches in pg_proc are
10463                                          * substring(text,int4) and substring(text,text),
10464                                          * and we don't want the parser to choose the latter,
10465                                          * which it is likely to do if the second argument
10466                                          * is unknown or doesn't have an implicit cast to int4.
10467                                          */
10468                                         $$ = list_make3($1, makeIntConst(1, -1),
10469                                                                         makeTypeCast($2,
10470                                                                                                  SystemTypeName("int4"), -1));
10471                                 }
10472                         | expr_list
10473                                 {
10474                                         $$ = $1;
10475                                 }
10476                         | /*EMPTY*/
10477                                 { $$ = NIL; }
10478                 ;
10479
10480 substr_from:
10481                         FROM a_expr                                                             { $$ = $2; }
10482                 ;
10483
10484 substr_for: FOR a_expr                                                          { $$ = $2; }
10485                 ;
10486
10487 trim_list:      a_expr FROM expr_list                                   { $$ = lappend($3, $1); }
10488                         | FROM expr_list                                                { $$ = $2; }
10489                         | expr_list                                                             { $$ = $1; }
10490                 ;
10491
10492 in_expr:        select_with_parens
10493                                 {
10494                                         SubLink *n = makeNode(SubLink);
10495                                         n->subselect = $1;
10496                                         /* other fields will be filled later */
10497                                         $$ = (Node *)n;
10498                                 }
10499                         | '(' expr_list ')'                                             { $$ = (Node *)$2; }
10500                 ;
10501
10502 /*
10503  * Define SQL92-style case clause.
10504  * - Full specification
10505  *      CASE WHEN a = b THEN c ... ELSE d END
10506  * - Implicit argument
10507  *      CASE a WHEN b THEN c ... ELSE d END
10508  */
10509 case_expr:      CASE case_arg when_clause_list case_default END_P
10510                                 {
10511                                         CaseExpr *c = makeNode(CaseExpr);
10512                                         c->casetype = InvalidOid; /* not analyzed yet */
10513                                         c->arg = (Expr *) $2;
10514                                         c->args = $3;
10515                                         c->defresult = (Expr *) $4;
10516                                         c->location = @1;
10517                                         $$ = (Node *)c;
10518                                 }
10519                 ;
10520
10521 when_clause_list:
10522                         /* There must be at least one */
10523                         when_clause                                                             { $$ = list_make1($1); }
10524                         | when_clause_list when_clause                  { $$ = lappend($1, $2); }
10525                 ;
10526
10527 when_clause:
10528                         WHEN a_expr THEN a_expr
10529                                 {
10530                                         CaseWhen *w = makeNode(CaseWhen);
10531                                         w->expr = (Expr *) $2;
10532                                         w->result = (Expr *) $4;
10533                                         w->location = @1;
10534                                         $$ = (Node *)w;
10535                                 }
10536                 ;
10537
10538 case_default:
10539                         ELSE a_expr                                                             { $$ = $2; }
10540                         | /*EMPTY*/                                                             { $$ = NULL; }
10541                 ;
10542
10543 case_arg:       a_expr                                                                  { $$ = $1; }
10544                         | /*EMPTY*/                                                             { $$ = NULL; }
10545                 ;
10546
10547 columnref:      ColId
10548                                 {
10549                                         $$ = makeColumnRef($1, NIL, @1, yyscanner);
10550                                 }
10551                         | ColId indirection
10552                                 {
10553                                         $$ = makeColumnRef($1, $2, @1, yyscanner);
10554                                 }
10555                 ;
10556
10557 indirection_el:
10558                         '.' attr_name
10559                                 {
10560                                         $$ = (Node *) makeString($2);
10561                                 }
10562                         | '.' '*'
10563                                 {
10564                                         $$ = (Node *) makeNode(A_Star);
10565                                 }
10566                         | '[' a_expr ']'
10567                                 {
10568                                         A_Indices *ai = makeNode(A_Indices);
10569                                         ai->lidx = NULL;
10570                                         ai->uidx = $2;
10571                                         $$ = (Node *) ai;
10572                                 }
10573                         | '[' a_expr ':' a_expr ']'
10574                                 {
10575                                         A_Indices *ai = makeNode(A_Indices);
10576                                         ai->lidx = $2;
10577                                         ai->uidx = $4;
10578                                         $$ = (Node *) ai;
10579                                 }
10580                 ;
10581
10582 indirection:
10583                         indirection_el                                                  { $$ = list_make1($1); }
10584                         | indirection indirection_el                    { $$ = lappend($1, $2); }
10585                 ;
10586
10587 opt_indirection:
10588                         /*EMPTY*/                                                               { $$ = NIL; }
10589                         | opt_indirection indirection_el                { $$ = lappend($1, $2); }
10590                 ;
10591
10592 opt_asymmetric: ASYMMETRIC
10593                         | /*EMPTY*/
10594                 ;
10595
10596 /*
10597  * The SQL spec defines "contextually typed value expressions" and
10598  * "contextually typed row value constructors", which for our purposes
10599  * are the same as "a_expr" and "row" except that DEFAULT can appear at
10600  * the top level.
10601  */
10602
10603 ctext_expr:
10604                         a_expr                                  { $$ = (Node *) $1; }
10605                         | DEFAULT
10606                                 {
10607                                         SetToDefault *n = makeNode(SetToDefault);
10608                                         n->location = @1;
10609                                         $$ = (Node *) n;
10610                                 }
10611                 ;
10612
10613 ctext_expr_list:
10614                         ctext_expr                                                              { $$ = list_make1($1); }
10615                         | ctext_expr_list ',' ctext_expr                { $$ = lappend($1, $3); }
10616                 ;
10617
10618 /*
10619  * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
10620  * making VALUES a fully reserved word, which will probably break more apps
10621  * than allowing the noise-word is worth.
10622  */
10623 ctext_row: '(' ctext_expr_list ')'                                      { $$ = $2; }
10624                 ;
10625
10626
10627 /*****************************************************************************
10628  *
10629  *      target list for SELECT
10630  *
10631  *****************************************************************************/
10632
10633 target_list:
10634                         target_el                                                               { $$ = list_make1($1); }
10635                         | target_list ',' target_el                             { $$ = lappend($1, $3); }
10636                 ;
10637
10638 target_el:      a_expr AS ColLabel
10639                                 {
10640                                         $$ = makeNode(ResTarget);
10641                                         $$->name = $3;
10642                                         $$->indirection = NIL;
10643                                         $$->val = (Node *)$1;
10644                                         $$->location = @1;
10645                                 }
10646                         /*
10647                          * We support omitting AS only for column labels that aren't
10648                          * any known keyword.  There is an ambiguity against postfix
10649                          * operators: is "a ! b" an infix expression, or a postfix
10650                          * expression and a column label?  We prefer to resolve this
10651                          * as an infix expression, which we accomplish by assigning
10652                          * IDENT a precedence higher than POSTFIXOP.
10653                          */
10654                         | a_expr IDENT
10655                                 {
10656                                         $$ = makeNode(ResTarget);
10657                                         $$->name = $2;
10658                                         $$->indirection = NIL;
10659                                         $$->val = (Node *)$1;
10660                                         $$->location = @1;
10661                                 }
10662                         | a_expr
10663                                 {
10664                                         $$ = makeNode(ResTarget);
10665                                         $$->name = NULL;
10666                                         $$->indirection = NIL;
10667                                         $$->val = (Node *)$1;
10668                                         $$->location = @1;
10669                                 }
10670                         | '*'
10671                                 {
10672                                         ColumnRef *n = makeNode(ColumnRef);
10673                                         n->fields = list_make1(makeNode(A_Star));
10674                                         n->location = @1;
10675
10676                                         $$ = makeNode(ResTarget);
10677                                         $$->name = NULL;
10678                                         $$->indirection = NIL;
10679                                         $$->val = (Node *)n;
10680                                         $$->location = @1;
10681                                 }
10682                 ;
10683
10684
10685 /*****************************************************************************
10686  *
10687  *      Names and constants
10688  *
10689  *****************************************************************************/
10690
10691 qualified_name_list:
10692                         qualified_name                                                  { $$ = list_make1($1); }
10693                         | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
10694                 ;
10695
10696 /*
10697  * The production for a qualified relation name has to exactly match the
10698  * production for a qualified func_name, because in a FROM clause we cannot
10699  * tell which we are parsing until we see what comes after it ('(' for a
10700  * func_name, something else for a relation). Therefore we allow 'indirection'
10701  * which may contain subscripts, and reject that case in the C code.
10702  */
10703 qualified_name:
10704                         ColId
10705                                 {
10706                                         $$ = makeNode(RangeVar);
10707                                         $$->catalogname = NULL;
10708                                         $$->schemaname = NULL;
10709                                         $$->relname = $1;
10710                                         $$->location = @1;
10711                                 }
10712                         | ColId indirection
10713                                 {
10714                                         check_qualified_name($2, yyscanner);
10715                                         $$ = makeNode(RangeVar);
10716                                         switch (list_length($2))
10717                                         {
10718                                                 case 1:
10719                                                         $$->catalogname = NULL;
10720                                                         $$->schemaname = $1;
10721                                                         $$->relname = strVal(linitial($2));
10722                                                         break;
10723                                                 case 2:
10724                                                         $$->catalogname = $1;
10725                                                         $$->schemaname = strVal(linitial($2));
10726                                                         $$->relname = strVal(lsecond($2));
10727                                                         break;
10728                                                 default:
10729                                                         ereport(ERROR,
10730                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
10731                                                                          errmsg("improper qualified name (too many dotted names): %s",
10732                                                                                         NameListToString(lcons(makeString($1), $2))),
10733                                                                          parser_errposition(@1)));
10734                                                         break;
10735                                         }
10736                                         $$->location = @1;
10737                                 }
10738                 ;
10739
10740 name_list:      name
10741                                         { $$ = list_make1(makeString($1)); }
10742                         | name_list ',' name
10743                                         { $$ = lappend($1, makeString($3)); }
10744                 ;
10745
10746
10747 name:           ColId                                                                   { $$ = $1; };
10748
10749 database_name:
10750                         ColId                                                                   { $$ = $1; };
10751
10752 access_method:
10753                         ColId                                                                   { $$ = $1; };
10754
10755 attr_name:      ColLabel                                                                { $$ = $1; };
10756
10757 index_name: ColId                                                                       { $$ = $1; };
10758
10759 file_name:      Sconst                                                                  { $$ = $1; };
10760
10761 /*
10762  * The production for a qualified func_name has to exactly match the
10763  * production for a qualified columnref, because we cannot tell which we
10764  * are parsing until we see what comes after it ('(' or Sconst for a func_name,
10765  * anything else for a columnref).  Therefore we allow 'indirection' which
10766  * may contain subscripts, and reject that case in the C code.  (If we
10767  * ever implement SQL99-like methods, such syntax may actually become legal!)
10768  */
10769 func_name:      type_function_name
10770                                         { $$ = list_make1(makeString($1)); }
10771                         | ColId indirection
10772                                         {
10773                                                 $$ = check_func_name(lcons(makeString($1), $2),
10774                                                                                          yyscanner);
10775                                         }
10776                 ;
10777
10778
10779 /*
10780  * Constants
10781  */
10782 AexprConst: Iconst
10783                                 {
10784                                         $$ = makeIntConst($1, @1);
10785                                 }
10786                         | FCONST
10787                                 {
10788                                         $$ = makeFloatConst($1, @1);
10789                                 }
10790                         | Sconst
10791                                 {
10792                                         $$ = makeStringConst($1, @1);
10793                                 }
10794                         | BCONST
10795                                 {
10796                                         $$ = makeBitStringConst($1, @1);
10797                                 }
10798                         | XCONST
10799                                 {
10800                                         /* This is a bit constant per SQL99:
10801                                          * Without Feature F511, "BIT data type",
10802                                          * a <general literal> shall not be a
10803                                          * <bit string literal> or a <hex string literal>.
10804                                          */
10805                                         $$ = makeBitStringConst($1, @1);
10806                                 }
10807                         | func_name Sconst
10808                                 {
10809                                         /* generic type 'literal' syntax */
10810                                         TypeName *t = makeTypeNameFromNameList($1);
10811                                         t->location = @1;
10812                                         $$ = makeStringConstCast($2, @2, t);
10813                                 }
10814                         | func_name '(' func_arg_list ')' Sconst
10815                                 {
10816                                         /* generic syntax with a type modifier */
10817                                         TypeName *t = makeTypeNameFromNameList($1);
10818                                         ListCell *lc;
10819
10820                                         /*
10821                                          * We must use func_arg_list in the production to avoid
10822                                          * reduce/reduce conflicts, but we don't actually wish
10823                                          * to allow NamedArgExpr in this context.
10824                                          */
10825                                         foreach(lc, $3)
10826                                         {
10827                                                 NamedArgExpr *arg = (NamedArgExpr *) lfirst(lc);
10828
10829                                                 if (IsA(arg, NamedArgExpr))
10830                                                         ereport(ERROR,
10831                                                                     (errcode(ERRCODE_SYNTAX_ERROR),
10832                                                                      errmsg("type modifier cannot have parameter name"),
10833                                                                      parser_errposition(arg->location)));
10834                                         }
10835                                         t->typmods = $3;
10836                                         t->location = @1;
10837                                         $$ = makeStringConstCast($5, @5, t);
10838                                 }
10839                         | ConstTypename Sconst
10840                                 {
10841                                         $$ = makeStringConstCast($2, @2, $1);
10842                                 }
10843                         | ConstInterval Sconst opt_interval
10844                                 {
10845                                         TypeName *t = $1;
10846                                         t->typmods = $3;
10847                                         $$ = makeStringConstCast($2, @2, t);
10848                                 }
10849                         | ConstInterval '(' Iconst ')' Sconst opt_interval
10850                                 {
10851                                         TypeName *t = $1;
10852                                         if ($6 != NIL)
10853                                         {
10854                                                 if (list_length($6) != 1)
10855                                                         ereport(ERROR,
10856                                                                         (errcode(ERRCODE_SYNTAX_ERROR),
10857                                                                          errmsg("interval precision specified twice"),
10858                                                                          parser_errposition(@1)));
10859                                                 t->typmods = lappend($6, makeIntConst($3, @3));
10860                                         }
10861                                         else
10862                                                 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
10863                                                                                                 makeIntConst($3, @3));
10864                                         $$ = makeStringConstCast($5, @5, t);
10865                                 }
10866                         | TRUE_P
10867                                 {
10868                                         $$ = makeBoolAConst(TRUE, @1);
10869                                 }
10870                         | FALSE_P
10871                                 {
10872                                         $$ = makeBoolAConst(FALSE, @1);
10873                                 }
10874                         | NULL_P
10875                                 {
10876                                         $$ = makeNullAConst(@1);
10877                                 }
10878                 ;
10879
10880 Iconst:         ICONST                                                                  { $$ = $1; };
10881 Sconst:         SCONST                                                                  { $$ = $1; };
10882 RoleId:         ColId                                                                   { $$ = $1; };
10883
10884 SignedIconst: Iconst                                                            { $$ = $1; }
10885                         | '+' Iconst                                                    { $$ = + $2; }
10886                         | '-' Iconst                                                    { $$ = - $2; }
10887                 ;
10888
10889 /*
10890  * Name classification hierarchy.
10891  *
10892  * IDENT is the lexeme returned by the lexer for identifiers that match
10893  * no known keyword.  In most cases, we can accept certain keywords as
10894  * names, not only IDENTs.      We prefer to accept as many such keywords
10895  * as possible to minimize the impact of "reserved words" on programmers.
10896  * So, we divide names into several possible classes.  The classification
10897  * is chosen in part to make keywords acceptable as names wherever possible.
10898  */
10899
10900 /* Column identifier --- names that can be column, table, etc names.
10901  */
10902 ColId:          IDENT                                                                   { $$ = $1; }
10903                         | unreserved_keyword                                    { $$ = pstrdup($1); }
10904                         | col_name_keyword                                              { $$ = pstrdup($1); }
10905                 ;
10906
10907 /* Type/function identifier --- names that can be type or function names.
10908  */
10909 type_function_name:     IDENT                                                   { $$ = $1; }
10910                         | unreserved_keyword                                    { $$ = pstrdup($1); }
10911                         | type_func_name_keyword                                { $$ = pstrdup($1); }
10912                 ;
10913
10914 /* Column label --- allowed labels in "AS" clauses.
10915  * This presently includes *all* Postgres keywords.
10916  */
10917 ColLabel:       IDENT                                                                   { $$ = $1; }
10918                         | unreserved_keyword                                    { $$ = pstrdup($1); }
10919                         | col_name_keyword                                              { $$ = pstrdup($1); }
10920                         | type_func_name_keyword                                { $$ = pstrdup($1); }
10921                         | reserved_keyword                                              { $$ = pstrdup($1); }
10922                 ;
10923
10924
10925 /*
10926  * Keyword category lists.  Generally, every keyword present in
10927  * the Postgres grammar should appear in exactly one of these lists.
10928  *
10929  * Put a new keyword into the first list that it can go into without causing
10930  * shift or reduce conflicts.  The earlier lists define "less reserved"
10931  * categories of keywords.
10932  *
10933  * Make sure that each keyword's category in keywords.c matches where
10934  * it is listed here.  (Someday we may be able to generate these lists and
10935  * keywords.c's table from a common master list.)
10936  */
10937
10938 /* "Unreserved" keywords --- available for use as any kind of name.
10939  */
10940 unreserved_keyword:
10941                           ABORT_P
10942                         | ABSOLUTE_P
10943                         | ACCESS
10944                         | ACTION
10945                         | ADD_P
10946                         | ADMIN
10947                         | AFTER
10948                         | AGGREGATE
10949                         | ALSO
10950                         | ALTER
10951                         | ALWAYS
10952                         | ASSERTION
10953                         | ASSIGNMENT
10954                         | AT
10955                         | ATTRIBUTE
10956                         | BACKWARD
10957                         | BEFORE
10958                         | BEGIN_P
10959                         | BY
10960                         | CACHE
10961                         | CALLED
10962                         | CASCADE
10963                         | CASCADED
10964                         | CATALOG_P
10965                         | CHAIN
10966                         | CHARACTERISTICS
10967                         | CHECKPOINT
10968                         | CLASS
10969                         | CLOSE
10970                         | CLUSTER
10971                         | COMMENT
10972                         | COMMENTS
10973                         | COMMIT
10974                         | COMMITTED
10975                         | CONFIGURATION
10976                         | CONNECTION
10977                         | CONSTRAINTS
10978                         | CONTENT_P
10979                         | CONTINUE_P
10980                         | CONVERSION_P
10981                         | COPY
10982                         | COST
10983                         | CREATEDB
10984                         | CREATEROLE
10985                         | CREATEUSER
10986                         | CSV
10987                         | CURRENT_P
10988                         | CURSOR
10989                         | CYCLE
10990                         | DATA_P
10991                         | DATABASE
10992                         | DAY_P
10993                         | DEALLOCATE
10994                         | DECLARE
10995                         | DEFAULTS
10996                         | DEFERRED
10997                         | DEFINER
10998                         | DELETE_P
10999                         | DELIMITER
11000                         | DELIMITERS
11001                         | DICTIONARY
11002                         | DISABLE_P
11003                         | DISCARD
11004                         | DOCUMENT_P
11005                         | DOMAIN_P
11006                         | DOUBLE_P
11007                         | DROP
11008                         | EACH
11009                         | ENABLE_P
11010                         | ENCODING
11011                         | ENCRYPTED
11012                         | ENUM_P
11013                         | ESCAPE
11014                         | EXCLUDE
11015                         | EXCLUDING
11016                         | EXCLUSIVE
11017                         | EXECUTE
11018                         | EXPLAIN
11019                         | EXTERNAL
11020                         | FAMILY
11021                         | FIRST_P
11022                         | FOLLOWING
11023                         | FORCE
11024                         | FORWARD
11025                         | FUNCTION
11026                         | FUNCTIONS
11027                         | GLOBAL
11028                         | GRANTED
11029                         | HANDLER
11030                         | HEADER_P
11031                         | HOLD
11032                         | HOUR_P
11033                         | IDENTITY_P
11034                         | IF_P
11035                         | IMMEDIATE
11036                         | IMMUTABLE
11037                         | IMPLICIT_P
11038                         | INCLUDING
11039                         | INCREMENT
11040                         | INDEX
11041                         | INDEXES
11042                         | INHERIT
11043                         | INHERITS
11044                         | INLINE_P
11045                         | INPUT_P
11046                         | INSENSITIVE
11047                         | INSERT
11048                         | INSTEAD
11049                         | INVOKER
11050                         | ISOLATION
11051                         | KEY
11052                         | LANGUAGE
11053                         | LARGE_P
11054                         | LAST_P
11055                         | LC_COLLATE_P
11056                         | LC_CTYPE_P
11057                         | LEVEL
11058                         | LISTEN
11059                         | LOAD
11060                         | LOCAL
11061                         | LOCATION
11062                         | LOCK_P
11063                         | LOGIN_P
11064                         | MAPPING
11065                         | MATCH
11066                         | MAXVALUE
11067                         | MINUTE_P
11068                         | MINVALUE
11069                         | MODE
11070                         | MONTH_P
11071                         | MOVE
11072                         | NAME_P
11073                         | NAMES
11074                         | NEXT
11075                         | NO
11076                         | NOCREATEDB
11077                         | NOCREATEROLE
11078                         | NOCREATEUSER
11079                         | NOINHERIT
11080                         | NOLOGIN_P
11081                         | NOSUPERUSER
11082                         | NOTHING
11083                         | NOTIFY
11084                         | NOWAIT
11085                         | NULLS_P
11086                         | OBJECT_P
11087                         | OF
11088                         | OIDS
11089                         | OPERATOR
11090                         | OPTION
11091                         | OPTIONS
11092                         | OWNED
11093                         | OWNER
11094                         | PARSER
11095                         | PARTIAL
11096                         | PARTITION
11097                         | PASSING
11098                         | PASSWORD
11099                         | PLANS
11100                         | PRECEDING
11101                         | PREPARE
11102                         | PREPARED
11103                         | PRESERVE
11104                         | PRIOR
11105                         | PRIVILEGES
11106                         | PROCEDURAL
11107                         | PROCEDURE
11108                         | QUOTE
11109                         | RANGE
11110                         | READ
11111                         | REASSIGN
11112                         | RECHECK
11113                         | RECURSIVE
11114                         | REF
11115                         | REINDEX
11116                         | RELATIVE_P
11117                         | RELEASE
11118                         | RENAME
11119                         | REPEATABLE
11120                         | REPLACE
11121                         | REPLICA
11122                         | RESET
11123                         | RESTART
11124                         | RESTRICT
11125                         | RETURNS
11126                         | REVOKE
11127                         | ROLE
11128                         | ROLLBACK
11129                         | ROWS
11130                         | RULE
11131                         | SAVEPOINT
11132                         | SCHEMA
11133                         | SCROLL
11134                         | SEARCH
11135                         | SECOND_P
11136                         | SECURITY
11137                         | SEQUENCE
11138                         | SEQUENCES
11139                         | SERIALIZABLE
11140                         | SERVER
11141                         | SESSION
11142                         | SET
11143                         | SHARE
11144                         | SHOW
11145                         | SIMPLE
11146                         | STABLE
11147                         | STANDALONE_P
11148                         | START
11149                         | STATEMENT
11150                         | STATISTICS
11151                         | STDIN
11152                         | STDOUT
11153                         | STORAGE
11154                         | STRICT_P
11155                         | STRIP_P
11156                         | SUPERUSER_P
11157                         | SYSID
11158                         | SYSTEM_P
11159                         | TABLES
11160                         | TABLESPACE
11161                         | TEMP
11162                         | TEMPLATE
11163                         | TEMPORARY
11164                         | TEXT_P
11165                         | TRANSACTION
11166                         | TRIGGER
11167                         | TRUNCATE
11168                         | TRUSTED
11169                         | TYPE_P
11170                         | UNBOUNDED
11171                         | UNCOMMITTED
11172                         | UNENCRYPTED
11173                         | UNKNOWN
11174                         | UNLISTEN
11175                         | UNTIL
11176                         | UPDATE
11177                         | VACUUM
11178                         | VALID
11179                         | VALIDATOR
11180                         | VALUE_P
11181                         | VARYING
11182                         | VERSION_P
11183                         | VIEW
11184                         | VOLATILE
11185                         | WHITESPACE_P
11186                         | WITHOUT
11187                         | WORK
11188                         | WRAPPER
11189                         | WRITE
11190                         | XML_P
11191                         | YEAR_P
11192                         | YES_P
11193                         | ZONE
11194                 ;
11195
11196 /* Column identifier --- keywords that can be column, table, etc names.
11197  *
11198  * Many of these keywords will in fact be recognized as type or function
11199  * names too; but they have special productions for the purpose, and so
11200  * can't be treated as "generic" type or function names.
11201  *
11202  * The type names appearing here are not usable as function names
11203  * because they can be followed by '(' in typename productions, which
11204  * looks too much like a function call for an LR(1) parser.
11205  */
11206 col_name_keyword:
11207                           BETWEEN
11208                         | BIGINT
11209                         | BIT
11210                         | BOOLEAN_P
11211                         | CHAR_P
11212                         | CHARACTER
11213                         | COALESCE
11214                         | DEC
11215                         | DECIMAL_P
11216                         | EXISTS
11217                         | EXTRACT
11218                         | FLOAT_P
11219                         | GREATEST
11220                         | INOUT
11221                         | INT_P
11222                         | INTEGER
11223                         | INTERVAL
11224                         | LEAST
11225                         | NATIONAL
11226                         | NCHAR
11227                         | NONE
11228                         | NULLIF
11229                         | NUMERIC
11230                         | OUT_P
11231                         | OVERLAY
11232                         | POSITION
11233                         | PRECISION
11234                         | REAL
11235                         | ROW
11236                         | SETOF
11237                         | SMALLINT
11238                         | SUBSTRING
11239                         | TIME
11240                         | TIMESTAMP
11241                         | TREAT
11242                         | TRIM
11243                         | VALUES
11244                         | VARCHAR
11245                         | XMLATTRIBUTES
11246                         | XMLCONCAT
11247                         | XMLELEMENT
11248                         | XMLEXISTS
11249                         | XMLFOREST
11250                         | XMLPARSE
11251                         | XMLPI
11252                         | XMLROOT
11253                         | XMLSERIALIZE
11254                 ;
11255
11256 /* Type/function identifier --- keywords that can be type or function names.
11257  *
11258  * Most of these are keywords that are used as operators in expressions;
11259  * in general such keywords can't be column names because they would be
11260  * ambiguous with variables, but they are unambiguous as function identifiers.
11261  *
11262  * Do not include POSITION, SUBSTRING, etc here since they have explicit
11263  * productions in a_expr to support the goofy SQL9x argument syntax.
11264  * - thomas 2000-11-28
11265  */
11266 type_func_name_keyword:
11267                           AUTHORIZATION
11268                         | BINARY
11269                         | CONCURRENTLY
11270                         | CROSS
11271                         | CURRENT_SCHEMA
11272                         | FREEZE
11273                         | FULL
11274                         | ILIKE
11275                         | INNER_P
11276                         | IS
11277                         | ISNULL
11278                         | JOIN
11279                         | LEFT
11280                         | LIKE
11281                         | NATURAL
11282                         | NOTNULL
11283                         | OUTER_P
11284                         | OVER
11285                         | OVERLAPS
11286                         | RIGHT
11287                         | SIMILAR
11288                         | VERBOSE
11289                 ;
11290
11291 /* Reserved keyword --- these keywords are usable only as a ColLabel.
11292  *
11293  * Keywords appear here if they could not be distinguished from variable,
11294  * type, or function names in some contexts.  Don't put things here unless
11295  * forced to.
11296  */
11297 reserved_keyword:
11298                           ALL
11299                         | ANALYSE
11300                         | ANALYZE
11301                         | AND
11302                         | ANY
11303                         | ARRAY
11304                         | AS
11305                         | ASC
11306                         | ASYMMETRIC
11307                         | BOTH
11308                         | CASE
11309                         | CAST
11310                         | CHECK
11311                         | COLLATE
11312                         | COLUMN
11313                         | CONSTRAINT
11314                         | CREATE
11315                         | CURRENT_CATALOG
11316                         | CURRENT_DATE
11317                         | CURRENT_ROLE
11318                         | CURRENT_TIME
11319                         | CURRENT_TIMESTAMP
11320                         | CURRENT_USER
11321                         | DEFAULT
11322                         | DEFERRABLE
11323                         | DESC
11324                         | DISTINCT
11325                         | DO
11326                         | ELSE
11327                         | END_P
11328                         | EXCEPT
11329                         | FALSE_P
11330                         | FETCH
11331                         | FOR
11332                         | FOREIGN
11333                         | FROM
11334                         | GRANT
11335                         | GROUP_P
11336                         | HAVING
11337                         | IN_P
11338                         | INITIALLY
11339                         | INTERSECT
11340                         | INTO
11341                         | LEADING
11342                         | LIMIT
11343                         | LOCALTIME
11344                         | LOCALTIMESTAMP
11345                         | NOT
11346                         | NULL_P
11347                         | OFF
11348                         | OFFSET
11349                         | ON
11350                         | ONLY
11351                         | OR
11352                         | ORDER
11353                         | PLACING
11354                         | PRIMARY
11355                         | REFERENCES
11356                         | RETURNING
11357                         | SELECT
11358                         | SESSION_USER
11359                         | SOME
11360                         | SYMMETRIC
11361                         | TABLE
11362                         | THEN
11363                         | TO
11364                         | TRAILING
11365                         | TRUE_P
11366                         | UNION
11367                         | UNIQUE
11368                         | USER
11369                         | USING
11370                         | VARIADIC
11371                         | WHEN
11372                         | WHERE
11373                         | WINDOW
11374                         | WITH
11375                 ;
11376
11377 %%
11378
11379 /*
11380  * The signature of this function is required by bison.  However, we
11381  * ignore the passed yylloc and instead use the last token position
11382  * available from the scanner.
11383  */
11384 static void
11385 base_yyerror(YYLTYPE *yylloc, core_yyscan_t yyscanner, const char *msg)
11386 {
11387         parser_yyerror(msg);
11388 }
11389
11390 static Node *
11391 makeColumnRef(char *colname, List *indirection,
11392                           int location, core_yyscan_t yyscanner)
11393 {
11394         /*
11395          * Generate a ColumnRef node, with an A_Indirection node added if there
11396          * is any subscripting in the specified indirection list.  However,
11397          * any field selection at the start of the indirection list must be
11398          * transposed into the "fields" part of the ColumnRef node.
11399          */
11400         ColumnRef  *c = makeNode(ColumnRef);
11401         int             nfields = 0;
11402         ListCell *l;
11403
11404         c->location = location;
11405         foreach(l, indirection)
11406         {
11407                 if (IsA(lfirst(l), A_Indices))
11408                 {
11409                         A_Indirection *i = makeNode(A_Indirection);
11410
11411                         if (nfields == 0)
11412                         {
11413                                 /* easy case - all indirection goes to A_Indirection */
11414                                 c->fields = list_make1(makeString(colname));
11415                                 i->indirection = check_indirection(indirection, yyscanner);
11416                         }
11417                         else
11418                         {
11419                                 /* got to split the list in two */
11420                                 i->indirection = check_indirection(list_copy_tail(indirection,
11421                                                                                                                                   nfields),
11422                                                                                                    yyscanner);
11423                                 indirection = list_truncate(indirection, nfields);
11424                                 c->fields = lcons(makeString(colname), indirection);
11425                         }
11426                         i->arg = (Node *) c;
11427                         return (Node *) i;
11428                 }
11429                 else if (IsA(lfirst(l), A_Star))
11430                 {
11431                         /* We only allow '*' at the end of a ColumnRef */
11432                         if (lnext(l) != NULL)
11433                                 parser_yyerror("improper use of \"*\"");
11434                 }
11435                 nfields++;
11436         }
11437         /* No subscripting, so all indirection gets added to field list */
11438         c->fields = lcons(makeString(colname), indirection);
11439         return (Node *) c;
11440 }
11441
11442 static Node *
11443 makeTypeCast(Node *arg, TypeName *typename, int location)
11444 {
11445         TypeCast *n = makeNode(TypeCast);
11446         n->arg = arg;
11447         n->typeName = typename;
11448         n->location = location;
11449         return (Node *) n;
11450 }
11451
11452 static Node *
11453 makeStringConst(char *str, int location)
11454 {
11455         A_Const *n = makeNode(A_Const);
11456
11457         n->val.type = T_String;
11458         n->val.val.str = str;
11459         n->location = location;
11460
11461         return (Node *)n;
11462 }
11463
11464 static Node *
11465 makeStringConstCast(char *str, int location, TypeName *typename)
11466 {
11467         Node *s = makeStringConst(str, location);
11468
11469         return makeTypeCast(s, typename, -1);
11470 }
11471
11472 static Node *
11473 makeIntConst(int val, int location)
11474 {
11475         A_Const *n = makeNode(A_Const);
11476
11477         n->val.type = T_Integer;
11478         n->val.val.ival = val;
11479         n->location = location;
11480
11481         return (Node *)n;
11482 }
11483
11484 static Node *
11485 makeFloatConst(char *str, int location)
11486 {
11487         A_Const *n = makeNode(A_Const);
11488
11489         n->val.type = T_Float;
11490         n->val.val.str = str;
11491         n->location = location;
11492
11493         return (Node *)n;
11494 }
11495
11496 static Node *
11497 makeBitStringConst(char *str, int location)
11498 {
11499         A_Const *n = makeNode(A_Const);
11500
11501         n->val.type = T_BitString;
11502         n->val.val.str = str;
11503         n->location = location;
11504
11505         return (Node *)n;
11506 }
11507
11508 static Node *
11509 makeNullAConst(int location)
11510 {
11511         A_Const *n = makeNode(A_Const);
11512
11513         n->val.type = T_Null;
11514         n->location = location;
11515
11516         return (Node *)n;
11517 }
11518
11519 static Node *
11520 makeAConst(Value *v, int location)
11521 {
11522         Node *n;
11523
11524         switch (v->type)
11525         {
11526                 case T_Float:
11527                         n = makeFloatConst(v->val.str, location);
11528                         break;
11529
11530                 case T_Integer:
11531                         n = makeIntConst(v->val.ival, location);
11532                         break;
11533
11534                 case T_String:
11535                 default:
11536                         n = makeStringConst(v->val.str, location);
11537                         break;
11538         }
11539
11540         return n;
11541 }
11542
11543 /* makeBoolAConst()
11544  * Create an A_Const string node and put it inside a boolean cast.
11545  */
11546 static Node *
11547 makeBoolAConst(bool state, int location)
11548 {
11549         A_Const *n = makeNode(A_Const);
11550
11551         n->val.type = T_String;
11552         n->val.val.str = (state ? "t" : "f");
11553         n->location = location;
11554
11555         return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
11556 }
11557
11558 /* makeOverlaps()
11559  * Create and populate a FuncCall node to support the OVERLAPS operator.
11560  */
11561 static FuncCall *
11562 makeOverlaps(List *largs, List *rargs, int location, core_yyscan_t yyscanner)
11563 {
11564         FuncCall *n = makeNode(FuncCall);
11565
11566         n->funcname = SystemFuncName("overlaps");
11567         if (list_length(largs) == 1)
11568                 largs = lappend(largs, largs);
11569         else if (list_length(largs) != 2)
11570                 ereport(ERROR,
11571                                 (errcode(ERRCODE_SYNTAX_ERROR),
11572                                  errmsg("wrong number of parameters on left side of OVERLAPS expression"),
11573                                  parser_errposition(location)));
11574         if (list_length(rargs) == 1)
11575                 rargs = lappend(rargs, rargs);
11576         else if (list_length(rargs) != 2)
11577                 ereport(ERROR,
11578                                 (errcode(ERRCODE_SYNTAX_ERROR),
11579                                  errmsg("wrong number of parameters on right side of OVERLAPS expression"),
11580                                  parser_errposition(location)));
11581         n->args = list_concat(largs, rargs);
11582         n->agg_order = NIL;
11583         n->agg_star = FALSE;
11584         n->agg_distinct = FALSE;
11585         n->func_variadic = FALSE;
11586         n->over = NULL;
11587         n->location = location;
11588         return n;
11589 }
11590
11591 /* check_qualified_name --- check the result of qualified_name production
11592  *
11593  * It's easiest to let the grammar production for qualified_name allow
11594  * subscripts and '*', which we then must reject here.
11595  */
11596 static void
11597 check_qualified_name(List *names, core_yyscan_t yyscanner)
11598 {
11599         ListCell   *i;
11600
11601         foreach(i, names)
11602         {
11603                 if (!IsA(lfirst(i), String))
11604                         parser_yyerror("syntax error");
11605         }
11606 }
11607
11608 /* check_func_name --- check the result of func_name production
11609  *
11610  * It's easiest to let the grammar production for func_name allow subscripts
11611  * and '*', which we then must reject here.
11612  */
11613 static List *
11614 check_func_name(List *names, core_yyscan_t yyscanner)
11615 {
11616         ListCell   *i;
11617
11618         foreach(i, names)
11619         {
11620                 if (!IsA(lfirst(i), String))
11621                         parser_yyerror("syntax error");
11622         }
11623         return names;
11624 }
11625
11626 /* check_indirection --- check the result of indirection production
11627  *
11628  * We only allow '*' at the end of the list, but it's hard to enforce that
11629  * in the grammar, so do it here.
11630  */
11631 static List *
11632 check_indirection(List *indirection, core_yyscan_t yyscanner)
11633 {
11634         ListCell *l;
11635
11636         foreach(l, indirection)
11637         {
11638                 if (IsA(lfirst(l), A_Star))
11639                 {
11640                         if (lnext(l) != NULL)
11641                                 parser_yyerror("improper use of \"*\"");
11642                 }
11643         }
11644         return indirection;
11645 }
11646
11647 /* extractArgTypes()
11648  * Given a list of FunctionParameter nodes, extract a list of just the
11649  * argument types (TypeNames) for input parameters only.  This is what
11650  * is needed to look up an existing function, which is what is wanted by
11651  * the productions that use this call.
11652  */
11653 static List *
11654 extractArgTypes(List *parameters)
11655 {
11656         List       *result = NIL;
11657         ListCell   *i;
11658
11659         foreach(i, parameters)
11660         {
11661                 FunctionParameter *p = (FunctionParameter *) lfirst(i);
11662
11663                 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
11664                         result = lappend(result, p->argType);
11665         }
11666         return result;
11667 }
11668
11669 /* findLeftmostSelect()
11670  * Find the leftmost component SelectStmt in a set-operation parsetree.
11671  */
11672 static SelectStmt *
11673 findLeftmostSelect(SelectStmt *node)
11674 {
11675         while (node && node->op != SETOP_NONE)
11676                 node = node->larg;
11677         Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
11678         return node;
11679 }
11680
11681 /* insertSelectOptions()
11682  * Insert ORDER BY, etc into an already-constructed SelectStmt.
11683  *
11684  * This routine is just to avoid duplicating code in SelectStmt productions.
11685  */
11686 static void
11687 insertSelectOptions(SelectStmt *stmt,
11688                                         List *sortClause, List *lockingClause,
11689                                         Node *limitOffset, Node *limitCount,
11690                                         WithClause *withClause,
11691                                         core_yyscan_t yyscanner)
11692 {
11693         Assert(IsA(stmt, SelectStmt));
11694
11695         /*
11696          * Tests here are to reject constructs like
11697          *      (SELECT foo ORDER BY bar) ORDER BY baz
11698          */
11699         if (sortClause)
11700         {
11701                 if (stmt->sortClause)
11702                         ereport(ERROR,
11703                                         (errcode(ERRCODE_SYNTAX_ERROR),
11704                                          errmsg("multiple ORDER BY clauses not allowed"),
11705                                          parser_errposition(exprLocation((Node *) sortClause))));
11706                 stmt->sortClause = sortClause;
11707         }
11708         /* We can handle multiple locking clauses, though */
11709         stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
11710         if (limitOffset)
11711         {
11712                 if (stmt->limitOffset)
11713                         ereport(ERROR,
11714                                         (errcode(ERRCODE_SYNTAX_ERROR),
11715                                          errmsg("multiple OFFSET clauses not allowed"),
11716                                          parser_errposition(exprLocation(limitOffset))));
11717                 stmt->limitOffset = limitOffset;
11718         }
11719         if (limitCount)
11720         {
11721                 if (stmt->limitCount)
11722                         ereport(ERROR,
11723                                         (errcode(ERRCODE_SYNTAX_ERROR),
11724                                          errmsg("multiple LIMIT clauses not allowed"),
11725                                          parser_errposition(exprLocation(limitCount))));
11726                 stmt->limitCount = limitCount;
11727         }
11728         if (withClause)
11729         {
11730                 if (stmt->withClause)
11731                         ereport(ERROR,
11732                                         (errcode(ERRCODE_SYNTAX_ERROR),
11733                                          errmsg("multiple WITH clauses not allowed"),
11734                                          parser_errposition(exprLocation((Node *) withClause))));
11735                 stmt->withClause = withClause;
11736         }
11737 }
11738
11739 static Node *
11740 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
11741 {
11742         SelectStmt *n = makeNode(SelectStmt);
11743
11744         n->op = op;
11745         n->all = all;
11746         n->larg = (SelectStmt *) larg;
11747         n->rarg = (SelectStmt *) rarg;
11748         return (Node *) n;
11749 }
11750
11751 /* SystemFuncName()
11752  * Build a properly-qualified reference to a built-in function.
11753  */
11754 List *
11755 SystemFuncName(char *name)
11756 {
11757         return list_make2(makeString("pg_catalog"), makeString(name));
11758 }
11759
11760 /* SystemTypeName()
11761  * Build a properly-qualified reference to a built-in type.
11762  *
11763  * typmod is defaulted, but may be changed afterwards by caller.
11764  * Likewise for the location.
11765  */
11766 TypeName *
11767 SystemTypeName(char *name)
11768 {
11769         return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
11770                                                                                            makeString(name)));
11771 }
11772
11773 /* doNegate()
11774  * Handle negation of a numeric constant.
11775  *
11776  * Formerly, we did this here because the optimizer couldn't cope with
11777  * indexquals that looked like "var = -4" --- it wants "var = const"
11778  * and a unary minus operator applied to a constant didn't qualify.
11779  * As of Postgres 7.0, that problem doesn't exist anymore because there
11780  * is a constant-subexpression simplifier in the optimizer.  However,
11781  * there's still a good reason for doing this here, which is that we can
11782  * postpone committing to a particular internal representation for simple
11783  * negative constants.  It's better to leave "-123.456" in string form
11784  * until we know what the desired type is.
11785  */
11786 static Node *
11787 doNegate(Node *n, int location)
11788 {
11789         if (IsA(n, A_Const))
11790         {
11791                 A_Const *con = (A_Const *)n;
11792
11793                 /* report the constant's location as that of the '-' sign */
11794                 con->location = location;
11795
11796                 if (con->val.type == T_Integer)
11797                 {
11798                         con->val.val.ival = -con->val.val.ival;
11799                         return n;
11800                 }
11801                 if (con->val.type == T_Float)
11802                 {
11803                         doNegateFloat(&con->val);
11804                         return n;
11805                 }
11806         }
11807
11808         return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
11809 }
11810
11811 static void
11812 doNegateFloat(Value *v)
11813 {
11814         char   *oldval = v->val.str;
11815
11816         Assert(IsA(v, Float));
11817         if (*oldval == '+')
11818                 oldval++;
11819         if (*oldval == '-')
11820                 v->val.str = oldval+1;  /* just strip the '-' */
11821         else
11822         {
11823                 char   *newval = (char *) palloc(strlen(oldval) + 2);
11824
11825                 *newval = '-';
11826                 strcpy(newval+1, oldval);
11827                 v->val.str = newval;
11828         }
11829 }
11830
11831 static Node *
11832 makeAArrayExpr(List *elements, int location)
11833 {
11834         A_ArrayExpr *n = makeNode(A_ArrayExpr);
11835
11836         n->elements = elements;
11837         n->location = location;
11838         return (Node *) n;
11839 }
11840
11841 static Node *
11842 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
11843                         int location)
11844 {
11845         XmlExpr    *x = makeNode(XmlExpr);
11846
11847         x->op = op;
11848         x->name = name;
11849         /*
11850          * named_args is a list of ResTarget; it'll be split apart into separate
11851          * expression and name lists in transformXmlExpr().
11852          */
11853         x->named_args = named_args;
11854         x->arg_names = NIL;
11855         x->args = args;
11856         /* xmloption, if relevant, must be filled in by caller */
11857         /* type and typmod will be filled in during parse analysis */
11858         x->location = location;
11859         return (Node *) x;
11860 }
11861
11862 /* parser_init()
11863  * Initialize to parse one query string
11864  */
11865 void
11866 parser_init(base_yy_extra_type *yyext)
11867 {
11868         yyext->parsetree = NIL;         /* in case grammar forgets to set it */
11869 }
11870
11871 /*
11872  * Merge the input and output parameters of a table function.
11873  */
11874 static List *
11875 mergeTableFuncParameters(List *func_args, List *columns)
11876 {
11877         ListCell   *lc;
11878
11879         /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
11880         foreach(lc, func_args)
11881         {
11882                 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
11883
11884                 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
11885                         ereport(ERROR,
11886                                         (errcode(ERRCODE_SYNTAX_ERROR),
11887                                          errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
11888         }
11889
11890         return list_concat(func_args, columns);
11891 }
11892
11893 /*
11894  * Determine return type of a TABLE function.  A single result column
11895  * returns setof that column's type; otherwise return setof record.
11896  */
11897 static TypeName *
11898 TableFuncTypeName(List *columns)
11899 {
11900         TypeName *result;
11901
11902         if (list_length(columns) == 1)
11903         {
11904                 FunctionParameter *p = (FunctionParameter *) linitial(columns);
11905
11906                 result = (TypeName *) copyObject(p->argType);
11907         }
11908         else
11909                 result = SystemTypeName("record");
11910
11911         result->setof = true;
11912
11913         return result;
11914 }
11915
11916 /*
11917  * Convert a list of (dotted) names to a RangeVar (like
11918  * makeRangeVarFromNameList, but with position support).  The
11919  * "AnyName" refers to the any_name production in the grammar.
11920  */
11921 static RangeVar *
11922 makeRangeVarFromAnyName(List *names, int position, core_yyscan_t yyscanner)
11923 {
11924         RangeVar *r = makeNode(RangeVar);
11925
11926         switch (list_length(names))
11927         {
11928                 case 1:
11929                         r->catalogname = NULL;
11930                         r->schemaname = NULL;
11931                         r->relname = strVal(linitial(names));
11932                         break;
11933                 case 2:
11934                         r->catalogname = NULL;
11935                         r->schemaname = strVal(linitial(names));
11936                         r->relname = strVal(lsecond(names));
11937                         break;
11938                 case 3:
11939                         r->catalogname = strVal(linitial(names));;
11940                         r->schemaname = strVal(lsecond(names));
11941                         r->relname = strVal(lthird(names));
11942                         break;
11943                 default:
11944                         ereport(ERROR,
11945                                         (errcode(ERRCODE_SYNTAX_ERROR),
11946                                          errmsg("improper qualified name (too many dotted names): %s",
11947                                                         NameListToString(names)),
11948                                          parser_errposition(position)));
11949                         break;
11950         }
11951
11952         r->location = position;
11953
11954         return r;
11955 }
11956
11957 /*
11958  * Must undefine this stuff before including scan.c, since it has different
11959  * definitions for these macros.
11960  */
11961 #undef yyerror
11962 #undef yylval
11963 #undef yylloc
11964
11965 #include "scan.c"