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