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