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