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