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