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