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