4 /*-------------------------------------------------------------------------
7 * POSTGRESQL BISON rules/actions
9 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.667 2009/07/12 17:12:33 tgl Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
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.
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
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "catalog/pg_trigger.h"
57 #include "commands/defrem.h"
58 #include "nodes/makefuncs.h"
59 #include "nodes/nodeFuncs.h"
60 #include "parser/gramparse.h"
61 #include "parser/parser.h"
62 #include "storage/lmgr.h"
63 #include "utils/date.h"
64 #include "utils/datetime.h"
65 #include "utils/numeric.h"
66 #include "utils/xml.h"
69 /* Location tracking support --- simpler than bison's default */
70 #define YYLLOC_DEFAULT(Current, Rhs, N) \
73 (Current) = (Rhs)[1]; \
75 (Current) = (Rhs)[0]; \
79 * The %name-prefix option below will make bison call base_yylex, but we
80 * really want it to call filtered_base_yylex (see parser.c).
82 #define base_yylex filtered_base_yylex
85 * Bison doesn't allocate anything that needs to live across parser calls,
86 * so we can easily have it use palloc instead of malloc. This prevents
87 * memory leaks if we error out during parsing. Note this only works with
88 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
89 * if possible, so there's not really much problem anyhow, at least if
90 * you're building with gcc.
92 #define YYMALLOC palloc
95 extern List *parsetree; /* final parse result is delivered here */
97 static bool QueryIsRule = FALSE;
99 /* Private struct for the result of privilege_target production */
100 typedef struct PrivTarget
102 GrantObjectType objtype;
107 * If you need access to certain yacc-generated variables and find that
108 * they're static by default, uncomment the next line. (this is not a
111 /*#define __YYSCLASS*/
113 static Node *makeColumnRef(char *colname, List *indirection, int location);
114 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
115 static Node *makeStringConst(char *str, int location);
116 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
117 static Node *makeIntConst(int val, int location);
118 static Node *makeFloatConst(char *str, int location);
119 static Node *makeBitStringConst(char *str, int location);
120 static Node *makeNullAConst(int location);
121 static Node *makeAConst(Value *v, int location);
122 static Node *makeBoolAConst(bool state, int location);
123 static FuncCall *makeOverlaps(List *largs, List *rargs, int location);
124 static void check_qualified_name(List *names);
125 static List *check_func_name(List *names);
126 static List *check_indirection(List *indirection);
127 static List *extractArgTypes(List *parameters);
128 static SelectStmt *findLeftmostSelect(SelectStmt *node);
129 static void insertSelectOptions(SelectStmt *stmt,
130 List *sortClause, List *lockingClause,
131 Node *limitOffset, Node *limitCount,
132 WithClause *withClause);
133 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
134 static Node *doNegate(Node *n, int location);
135 static void doNegateFloat(Value *v);
136 static Node *makeAArrayExpr(List *elements, int location);
137 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
138 List *args, int location);
139 static List *mergeTableFuncParameters(List *func_args, List *columns);
140 static TypeName *TableFuncTypeName(List *columns);
145 %name-prefix="base_yy"
156 DropBehavior dbehavior;
157 OnCommitAction oncommit;
164 FunctionParameter *fun_param;
165 FunctionParameterMode fun_param_mode;
166 FuncWithArgs *funwithargs;
178 struct PrivTarget *privtarget;
179 AccessPriv *accesspriv;
182 VariableSetStmt *vsetstmt;
185 %type <node> stmt schema_stmt
186 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterFdwStmt
187 AlterForeignServerStmt AlterGroupStmt
188 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
189 AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
190 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
191 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
192 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
193 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
194 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
195 CreateFdwStmt CreateForeignServerStmt CreateAssertStmt CreateTrigStmt
196 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
197 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
198 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
199 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
200 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
201 DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
202 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
203 LockStmt NotifyStmt ExplainableStmt PreparableStmt
204 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
205 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
206 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
207 SelectStmt TransactionStmt TruncateStmt
208 UnlistenStmt UpdateStmt VacuumStmt
209 VariableResetStmt VariableSetStmt VariableShowStmt
210 ViewStmt CheckPointStmt CreateConversionStmt
211 DeallocateStmt PrepareStmt ExecuteStmt
212 DropOwnedStmt ReassignOwnedStmt
213 AlterTSConfigurationStmt AlterTSDictionaryStmt
215 %type <node> select_no_parens select_with_parens select_clause
216 simple_select values_clause
218 %type <node> alter_column_default opclass_item opclass_drop alter_using
219 %type <ival> add_drop opt_asc_desc opt_nulls_order
221 %type <node> alter_table_cmd
222 %type <list> alter_table_cmds
224 %type <dbehavior> opt_drop_behavior
226 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
227 transaction_mode_list
228 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
229 transaction_mode_item
231 %type <ival> opt_lock lock_type cast_context
232 %type <boolean> opt_force opt_or_replace
233 opt_grant_grant_option opt_grant_admin_option
234 opt_nowait opt_if_exists opt_with_data
236 %type <list> OptRoleList
237 %type <defelt> OptRoleElem
240 %type <str> foreign_server_version opt_foreign_server_version
241 %type <str> auth_ident
243 %type <str> OptSchemaName
244 %type <list> OptSchemaEltList
246 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
247 %type <str> opt_lancompiler
249 %type <ival> TriggerEvents TriggerOneEvent
250 %type <value> TriggerFuncArg
252 %type <str> relation_name copy_file_name
253 database_name access_method_clause access_method attr_name
254 index_name name file_name cluster_index_specification
256 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
257 opt_class opt_validator validator_clause
259 %type <range> qualified_name OptConstrFromTable
261 %type <str> all_Op MathOp SpecialRuleRelation
263 %type <str> iso_level opt_encoding
265 %type <list> grantee_list
266 %type <accesspriv> privilege
267 %type <list> privileges privilege_list
268 %type <privtarget> privilege_target
269 %type <funwithargs> function_with_argtypes
270 %type <list> function_with_argtypes_list
272 %type <list> stmtblock stmtmulti
273 OptTableElementList TableElementList OptInherit definition
274 reloptions opt_reloptions
275 OptWith opt_distinct opt_definition func_args func_args_list
276 func_args_with_defaults func_args_with_defaults_list
277 func_as createfunc_opt_list alterfunc_opt_list
278 aggr_args old_aggr_definition old_aggr_list
279 oper_argtypes RuleActionList RuleActionMulti
280 opt_column_list columnList opt_name_list
281 sort_clause opt_sort_clause sortby_list index_params
282 name_list from_clause from_list opt_array_bounds
283 qualified_name_list any_name any_name_list
284 any_operator expr_list attrs
285 target_list insert_column_list set_target_list
286 set_clause_list set_clause multiple_set_clause
287 ctext_expr_list ctext_row def_list indirection opt_indirection
288 reloption_list group_clause TriggerFuncArgs select_limit
289 opt_select_limit opclass_item_list opclass_drop_list
290 opt_opfamily transaction_mode_list_or_empty
291 TableFuncElementList opt_type_modifiers
293 execute_param_clause using_clause returning_clause
294 enum_val_list table_func_column_list
295 create_generic_options alter_generic_options
298 %type <range> OptTempTableName
299 %type <into> into_clause create_as_target
301 %type <defelt> createfunc_opt_item common_func_opt_item
302 %type <fun_param> func_arg func_arg_with_default table_func_column
303 %type <fun_param_mode> arg_class
304 %type <typnam> func_return func_type
306 %type <boolean> TriggerForType OptTemp
307 %type <oncommit> OnCommitOption
309 %type <node> for_locking_item
310 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
311 %type <list> locked_rels_list
312 %type <boolean> opt_all
314 %type <node> join_outer join_qual
315 %type <jtype> join_type
317 %type <list> extract_list overlay_list position_list
318 %type <list> substr_list trim_list
319 %type <list> opt_interval interval_second
320 %type <node> overlay_placing substr_from substr_for
322 %type <boolean> opt_instead opt_analyze
323 %type <boolean> index_opt_unique opt_verbose opt_full
324 %type <boolean> opt_freeze opt_default opt_recheck
325 %type <defelt> opt_binary opt_oids copy_delimiter
327 %type <boolean> copy_from
329 %type <ival> opt_column event cursor_options opt_hold opt_set_data
330 %type <objtype> reindex_type drop_type comment_type
332 %type <node> fetch_direction select_limit_value select_offset_value
333 select_offset_value2 opt_select_fetch_first_value
334 %type <ival> row_or_rows first_or_next
336 %type <list> OptSeqOptList SeqOptList
337 %type <defelt> SeqOptElem
339 %type <istmt> insert_rest
341 %type <vsetstmt> set_rest SetResetClause
343 %type <node> TableElement ConstraintElem TableFuncElement
344 %type <node> columnDef
345 %type <defelt> def_elem reloption_elem old_aggr_elem
346 %type <node> def_arg columnElem where_clause where_or_current_clause
347 a_expr b_expr c_expr func_expr AexprConst indirection_el
348 columnref in_expr having_clause func_table array_expr
349 %type <list> row type_list array_expr_list
350 %type <node> case_expr case_arg when_clause case_default
351 %type <list> when_clause_list
352 %type <ival> sub_type
353 %type <list> OptCreateAs CreateAsList
354 %type <node> CreateAsElement ctext_expr
355 %type <value> NumericOnly
356 %type <alias> alias_clause
357 %type <sortby> sortby
358 %type <ielem> index_elem
359 %type <node> table_ref
360 %type <jexpr> joined_table
361 %type <range> relation_expr
362 %type <range> relation_expr_opt_alias
363 %type <target> target_el single_set_clause set_target insert_column_item
365 %type <str> generic_option_name
366 %type <node> generic_option_arg
367 %type <defelt> generic_option_elem alter_generic_option_elem
368 %type <list> generic_option_list alter_generic_option_list
370 %type <typnam> Typename SimpleTypename ConstTypename
371 GenericType Numeric opt_float
372 Character ConstCharacter
373 CharacterWithLength CharacterWithoutLength
374 ConstDatetime ConstInterval
375 Bit ConstBit BitWithLength BitWithoutLength
376 %type <str> character
377 %type <str> extract_arg
378 %type <str> opt_charset
379 %type <boolean> opt_varying opt_timezone
381 %type <ival> Iconst SignedIconst
382 %type <str> Sconst comment_text
383 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
384 %type <list> var_list
385 %type <str> ColId ColLabel var_name type_function_name param_name
386 %type <node> var_value zone_value
388 %type <keyword> unreserved_keyword type_func_name_keyword
389 %type <keyword> col_name_keyword reserved_keyword
391 %type <node> TableConstraint TableLikeClause
392 %type <list> TableLikeOptionList
393 %type <ival> TableLikeOption
394 %type <list> ColQualList
395 %type <node> ColConstraint ColConstraintElem ConstraintAttr
396 %type <ival> key_actions key_delete key_match key_update key_action
397 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
400 %type <list> constraints_set_list
401 %type <boolean> constraints_set_mode
402 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
403 %type <list> opt_check_option
405 %type <target> xml_attribute_el
406 %type <list> xml_attribute_list xml_attributes
407 %type <node> xml_root_version opt_xml_root_standalone
408 %type <ival> document_or_content
409 %type <boolean> xml_whitespace_option
411 %type <node> common_table_expr
412 %type <with> with_clause
413 %type <list> cte_list
415 %type <list> window_clause window_definition_list opt_partition_clause
416 %type <windef> window_definition over_clause window_specification
417 %type <str> opt_existing_window_name
418 %type <ival> opt_frame_clause frame_extent frame_bound
422 * If you make any token changes, update the keyword table in
423 * src/include/parser/kwlist.h and add new keywords to the appropriate one of
424 * the reserved-or-not-so-reserved keyword lists, below; search
425 * this file for "Name classification hierarchy".
428 /* ordinary key words in alphabetical order */
429 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
430 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
431 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
433 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
436 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
437 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
438 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
439 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
440 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
441 CREATEROLE CREATEUSER CROSS CSV CURRENT_P
442 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
443 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
445 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
446 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
447 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
449 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
450 EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
452 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
453 FREEZE FROM FULL FUNCTION
455 GLOBAL GRANT GRANTED GREATEST GROUP_P
457 HANDLER HAVING HEADER_P HOLD HOUR_P
459 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
460 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
461 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
462 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
468 LANCOMPILER LANGUAGE LARGE_P LAST_P LC_COLLATE_P LC_CTYPE_P LEADING
469 LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP
470 LOCATION LOCK_P LOGIN_P
472 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
474 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
475 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
476 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
478 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
479 ORDER OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
481 PARSER PARTIAL PARTITION PASSWORD PLACING PLANS POSITION
482 PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
483 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
487 RANGE READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX
488 RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA RESET RESTART
489 RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
491 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
492 SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
493 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
494 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
495 SYMMETRIC SYSID SYSTEM_P
497 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
498 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
499 TRUNCATE TRUSTED TYPE_P
501 UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
504 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
505 VERBOSE VERSION_P VIEW VOLATILE
507 WHEN WHERE WHITESPACE_P WINDOW WITH WITHOUT WORK WRAPPER WRITE
509 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
510 XMLPI XMLROOT XMLSERIALIZE
516 /* The grammar thinks these are keywords, but they are not in the kwlist.h
517 * list and so can never be entered directly. The filter in parser.c
518 * creates these tokens when required.
520 %token NULLS_FIRST NULLS_LAST WITH_TIME
522 /* Special token types, not actually keywords - see the "lex" file */
523 %token <str> IDENT FCONST SCONST BCONST XCONST Op
524 %token <ival> ICONST PARAM
526 /* precedence: lowest to highest */
527 %nonassoc SET /* see relation_expr_opt_alias */
535 %nonassoc LIKE ILIKE SIMILAR
540 %left POSTFIXOP /* dummy for postfix Op rules */
542 * To support target_el without AS, we must give IDENT an explicit priority
543 * between POSTFIXOP and Op. We can safely assign the same priority to
544 * various unreserved keywords as needed to resolve ambiguities (this can't
545 * have any bad effects since obviously the keywords will still behave the
546 * same as if they weren't keywords). We need to do this for PARTITION,
547 * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
548 * so that they can follow a_expr without creating
549 * postfix-operator problems.
551 %nonassoc IDENT PARTITION RANGE ROWS
552 %left Op OPERATOR /* multi-character ops and user-defined operators */
555 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
559 /* Unary Operators */
560 %left AT ZONE /* sets precedence for AT TIME ZONE */
567 * These might seem to be low-precedence, but actually they are not part
568 * of the arithmetic hierarchy at all in their use as JOIN operators.
569 * We make them high-precedence to support their use as function names.
570 * They wouldn't be given a precedence at all, were it not that we need
571 * left-associativity among the JOIN rules themselves.
573 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
574 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
575 %right PRESERVE STRIP_P
579 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
580 * psql already handles such cases, but other interfaces don't.
583 stmtblock: stmtmulti { parsetree = $1; }
586 /* the thrashing around here is to discard "empty" statements... */
587 stmtmulti: stmtmulti ';' stmt
589 $$ = lappend($1, $3);
603 | AlterDatabaseSetStmt
606 | AlterForeignServerStmt
609 | AlterObjectSchemaStmt
615 | AlterTSConfigurationStmt
616 | AlterTSDictionaryStmt
617 | AlterUserMappingStmt
630 | CreateConversionStmt
633 | CreateForeignServerStmt
643 | CreateTableSpaceStmt
647 | CreateUserMappingStmt
657 | DropForeignServerStmt
669 | DropUserMappingStmt
706 /*****************************************************************************
708 * Create a new Postgres DBMS role
710 *****************************************************************************/
713 CREATE ROLE RoleId opt_with OptRoleList
715 CreateRoleStmt *n = makeNode(CreateRoleStmt);
716 n->stmt_type = ROLESTMT_ROLE;
729 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
730 * for backwards compatibility). Note: the only option required by SQL99
731 * is "WITH ADMIN name".
734 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
735 | /* EMPTY */ { $$ = NIL; }
741 $$ = makeDefElem("password",
742 (Node *)makeString($2));
746 $$ = makeDefElem("password", NULL);
748 | ENCRYPTED PASSWORD Sconst
750 $$ = makeDefElem("encryptedPassword",
751 (Node *)makeString($3));
753 | UNENCRYPTED PASSWORD Sconst
755 $$ = makeDefElem("unencryptedPassword",
756 (Node *)makeString($3));
760 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
764 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
768 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
772 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
776 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
780 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
784 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
788 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
792 /* For backwards compatibility, synonym for SUPERUSER */
793 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
797 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
801 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
805 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
807 | CONNECTION LIMIT SignedIconst
809 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
813 $$ = makeDefElem("validUntil", (Node *)makeString($3));
815 /* Supported but not documented for roles, for use by ALTER GROUP. */
818 $$ = makeDefElem("rolemembers", (Node *)$2);
820 /* The following are not supported by ALTER ROLE/USER/GROUP */
823 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
827 $$ = makeDefElem("adminmembers", (Node *)$2);
831 $$ = makeDefElem("rolemembers", (Node *)$2);
833 | IN_P ROLE name_list
835 $$ = makeDefElem("addroleto", (Node *)$3);
837 | IN_P GROUP_P name_list
839 $$ = makeDefElem("addroleto", (Node *)$3);
844 /*****************************************************************************
846 * Create a new Postgres DBMS user (role with implied login ability)
848 *****************************************************************************/
851 CREATE USER RoleId opt_with OptRoleList
853 CreateRoleStmt *n = makeNode(CreateRoleStmt);
854 n->stmt_type = ROLESTMT_USER;
862 /*****************************************************************************
864 * Alter a postgresql DBMS role
866 *****************************************************************************/
869 ALTER ROLE RoleId opt_with OptRoleList
871 AlterRoleStmt *n = makeNode(AlterRoleStmt);
873 n->action = +1; /* add, if there are members */
880 ALTER ROLE RoleId SetResetClause
882 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
890 /*****************************************************************************
892 * Alter a postgresql DBMS user
894 *****************************************************************************/
897 ALTER USER RoleId opt_with OptRoleList
899 AlterRoleStmt *n = makeNode(AlterRoleStmt);
901 n->action = +1; /* add, if there are members */
909 ALTER USER RoleId SetResetClause
911 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
919 /*****************************************************************************
921 * Drop a postgresql DBMS role
923 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
924 * might own objects in multiple databases, there is presently no way to
925 * implement either cascading or restricting. Caveat DBA.
926 *****************************************************************************/
931 DropRoleStmt *n = makeNode(DropRoleStmt);
932 n->missing_ok = FALSE;
936 | DROP ROLE IF_P EXISTS name_list
938 DropRoleStmt *n = makeNode(DropRoleStmt);
939 n->missing_ok = TRUE;
945 /*****************************************************************************
947 * Drop a postgresql DBMS user
949 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
950 * might own objects in multiple databases, there is presently no way to
951 * implement either cascading or restricting. Caveat DBA.
952 *****************************************************************************/
957 DropRoleStmt *n = makeNode(DropRoleStmt);
958 n->missing_ok = FALSE;
962 | DROP USER IF_P EXISTS name_list
964 DropRoleStmt *n = makeNode(DropRoleStmt);
966 n->missing_ok = TRUE;
972 /*****************************************************************************
974 * Create a postgresql group (role without login ability)
976 *****************************************************************************/
979 CREATE GROUP_P RoleId opt_with OptRoleList
981 CreateRoleStmt *n = makeNode(CreateRoleStmt);
982 n->stmt_type = ROLESTMT_GROUP;
990 /*****************************************************************************
992 * Alter a postgresql group
994 *****************************************************************************/
997 ALTER GROUP_P RoleId add_drop USER name_list
999 AlterRoleStmt *n = makeNode(AlterRoleStmt);
1002 n->options = list_make1(makeDefElem("rolemembers",
1008 add_drop: ADD_P { $$ = +1; }
1013 /*****************************************************************************
1015 * Drop a postgresql group
1017 * XXX see above notes about cascading DROP USER; groups have same problem.
1018 *****************************************************************************/
1021 DROP GROUP_P name_list
1023 DropRoleStmt *n = makeNode(DropRoleStmt);
1024 n->missing_ok = FALSE;
1028 | DROP GROUP_P IF_P EXISTS name_list
1030 DropRoleStmt *n = makeNode(DropRoleStmt);
1031 n->missing_ok = TRUE;
1038 /*****************************************************************************
1040 * Manipulate a schema
1042 *****************************************************************************/
1045 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1047 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1048 /* One can omit the schema name or the authorization id. */
1057 | CREATE SCHEMA ColId OptSchemaEltList
1059 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1060 /* ...but not both */
1070 | /* EMPTY */ { $$ = NULL; }
1074 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1075 | /* EMPTY */ { $$ = NIL; }
1079 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1080 * statement (in addition to by themselves).
1092 /*****************************************************************************
1094 * Set PG internal variable
1095 * SET name TO 'var_value'
1096 * Include SQL92 syntax (thomas 1997-10-22):
1097 * SET TIME ZONE 'var_value'
1099 *****************************************************************************/
1104 VariableSetStmt *n = $2;
1105 n->is_local = false;
1108 | SET LOCAL set_rest
1110 VariableSetStmt *n = $3;
1114 | SET SESSION set_rest
1116 VariableSetStmt *n = $3;
1117 n->is_local = false;
1122 set_rest: /* Generic SET syntaxes: */
1123 var_name TO var_list
1125 VariableSetStmt *n = makeNode(VariableSetStmt);
1126 n->kind = VAR_SET_VALUE;
1131 | var_name '=' var_list
1133 VariableSetStmt *n = makeNode(VariableSetStmt);
1134 n->kind = VAR_SET_VALUE;
1139 | var_name TO DEFAULT
1141 VariableSetStmt *n = makeNode(VariableSetStmt);
1142 n->kind = VAR_SET_DEFAULT;
1146 | var_name '=' DEFAULT
1148 VariableSetStmt *n = makeNode(VariableSetStmt);
1149 n->kind = VAR_SET_DEFAULT;
1153 | var_name FROM CURRENT_P
1155 VariableSetStmt *n = makeNode(VariableSetStmt);
1156 n->kind = VAR_SET_CURRENT;
1160 /* Special syntaxes mandated by SQL standard: */
1161 | TIME ZONE zone_value
1163 VariableSetStmt *n = makeNode(VariableSetStmt);
1164 n->kind = VAR_SET_VALUE;
1165 n->name = "timezone";
1167 n->args = list_make1($3);
1169 n->kind = VAR_SET_DEFAULT;
1172 | TRANSACTION transaction_mode_list
1174 VariableSetStmt *n = makeNode(VariableSetStmt);
1175 n->kind = VAR_SET_MULTI;
1176 n->name = "TRANSACTION";
1180 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1182 VariableSetStmt *n = makeNode(VariableSetStmt);
1183 n->kind = VAR_SET_MULTI;
1184 n->name = "SESSION CHARACTERISTICS";
1191 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1192 errmsg("current database cannot be changed"),
1193 scanner_errposition(@2)));
1194 $$ = NULL; /*not reached*/
1198 VariableSetStmt *n = makeNode(VariableSetStmt);
1199 n->kind = VAR_SET_VALUE;
1200 n->name = "search_path";
1201 n->args = list_make1(makeStringConst($2, @2));
1204 | NAMES opt_encoding
1206 VariableSetStmt *n = makeNode(VariableSetStmt);
1207 n->kind = VAR_SET_VALUE;
1208 n->name = "client_encoding";
1210 n->args = list_make1(makeStringConst($2, @2));
1212 n->kind = VAR_SET_DEFAULT;
1215 | ROLE ColId_or_Sconst
1217 VariableSetStmt *n = makeNode(VariableSetStmt);
1218 n->kind = VAR_SET_VALUE;
1220 n->args = list_make1(makeStringConst($2, @2));
1223 | SESSION AUTHORIZATION ColId_or_Sconst
1225 VariableSetStmt *n = makeNode(VariableSetStmt);
1226 n->kind = VAR_SET_VALUE;
1227 n->name = "session_authorization";
1228 n->args = list_make1(makeStringConst($3, @3));
1231 | SESSION AUTHORIZATION DEFAULT
1233 VariableSetStmt *n = makeNode(VariableSetStmt);
1234 n->kind = VAR_SET_DEFAULT;
1235 n->name = "session_authorization";
1238 | XML_P OPTION document_or_content
1240 VariableSetStmt *n = makeNode(VariableSetStmt);
1241 n->kind = VAR_SET_VALUE;
1242 n->name = "xmloption";
1243 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1248 var_name: ColId { $$ = $1; }
1249 | var_name '.' ColId
1251 $$ = palloc(strlen($1) + strlen($3) + 2);
1252 sprintf($$, "%s.%s", $1, $3);
1256 var_list: var_value { $$ = list_make1($1); }
1257 | var_list ',' var_value { $$ = lappend($1, $3); }
1260 var_value: opt_boolean
1261 { $$ = makeStringConst($1, @1); }
1263 { $$ = makeStringConst($1, @1); }
1265 { $$ = makeAConst($1, @1); }
1268 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1269 | READ COMMITTED { $$ = "read committed"; }
1270 | REPEATABLE READ { $$ = "repeatable read"; }
1271 | SERIALIZABLE { $$ = "serializable"; }
1275 TRUE_P { $$ = "true"; }
1276 | FALSE_P { $$ = "false"; }
1278 | OFF { $$ = "off"; }
1281 /* Timezone values can be:
1282 * - a string such as 'pst8pdt'
1283 * - an identifier such as "pst8pdt"
1284 * - an integer or floating point number
1285 * - a time interval per SQL99
1286 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1287 * so use IDENT (meaning we reject anything that is a key word).
1292 $$ = makeStringConst($1, @1);
1296 $$ = makeStringConst($1, @1);
1298 | ConstInterval Sconst opt_interval
1303 A_Const *n = (A_Const *) linitial($3);
1304 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1306 (errcode(ERRCODE_SYNTAX_ERROR),
1307 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1308 scanner_errposition(@3)));
1311 $$ = makeStringConstCast($2, @2, t);
1313 | ConstInterval '(' Iconst ')' Sconst opt_interval
1318 A_Const *n = (A_Const *) linitial($6);
1319 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1321 (errcode(ERRCODE_SYNTAX_ERROR),
1322 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1323 scanner_errposition(@6)));
1324 if (list_length($6) != 1)
1326 (errcode(ERRCODE_SYNTAX_ERROR),
1327 errmsg("interval precision specified twice"),
1328 scanner_errposition(@1)));
1329 t->typmods = lappend($6, makeIntConst($3, @3));
1332 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1333 makeIntConst($3, @3));
1334 $$ = makeStringConstCast($5, @5, t);
1336 | NumericOnly { $$ = makeAConst($1, @1); }
1337 | DEFAULT { $$ = NULL; }
1338 | LOCAL { $$ = NULL; }
1343 | DEFAULT { $$ = NULL; }
1344 | /*EMPTY*/ { $$ = NULL; }
1349 | Sconst { $$ = $1; }
1355 VariableSetStmt *n = makeNode(VariableSetStmt);
1356 n->kind = VAR_RESET;
1362 VariableSetStmt *n = makeNode(VariableSetStmt);
1363 n->kind = VAR_RESET;
1364 n->name = "timezone";
1367 | RESET TRANSACTION ISOLATION LEVEL
1369 VariableSetStmt *n = makeNode(VariableSetStmt);
1370 n->kind = VAR_RESET;
1371 n->name = "transaction_isolation";
1374 | RESET SESSION AUTHORIZATION
1376 VariableSetStmt *n = makeNode(VariableSetStmt);
1377 n->kind = VAR_RESET;
1378 n->name = "session_authorization";
1383 VariableSetStmt *n = makeNode(VariableSetStmt);
1384 n->kind = VAR_RESET_ALL;
1389 /* SetResetClause allows SET or RESET without LOCAL */
1391 SET set_rest { $$ = $2; }
1392 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1399 VariableShowStmt *n = makeNode(VariableShowStmt);
1405 VariableShowStmt *n = makeNode(VariableShowStmt);
1406 n->name = "timezone";
1409 | SHOW TRANSACTION ISOLATION LEVEL
1411 VariableShowStmt *n = makeNode(VariableShowStmt);
1412 n->name = "transaction_isolation";
1415 | SHOW SESSION AUTHORIZATION
1417 VariableShowStmt *n = makeNode(VariableShowStmt);
1418 n->name = "session_authorization";
1423 VariableShowStmt *n = makeNode(VariableShowStmt);
1431 SET CONSTRAINTS constraints_set_list constraints_set_mode
1433 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1434 n->constraints = $3;
1440 constraints_set_list:
1442 | qualified_name_list { $$ = $1; }
1445 constraints_set_mode:
1446 DEFERRED { $$ = TRUE; }
1447 | IMMEDIATE { $$ = FALSE; }
1452 * Checkpoint statement
1457 CheckPointStmt *n = makeNode(CheckPointStmt);
1463 /*****************************************************************************
1465 * DISCARD { ALL | TEMP | PLANS }
1467 *****************************************************************************/
1472 DiscardStmt *n = makeNode(DiscardStmt);
1473 n->target = DISCARD_ALL;
1478 DiscardStmt *n = makeNode(DiscardStmt);
1479 n->target = DISCARD_TEMP;
1484 DiscardStmt *n = makeNode(DiscardStmt);
1485 n->target = DISCARD_TEMP;
1490 DiscardStmt *n = makeNode(DiscardStmt);
1491 n->target = DISCARD_PLANS;
1497 /*****************************************************************************
1499 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1501 * Note: we accept all subcommands for each of the four variants, and sort
1502 * out what's really legal at execution time.
1503 *****************************************************************************/
1506 ALTER TABLE relation_expr alter_table_cmds
1508 AlterTableStmt *n = makeNode(AlterTableStmt);
1511 n->relkind = OBJECT_TABLE;
1514 | ALTER INDEX qualified_name alter_table_cmds
1516 AlterTableStmt *n = makeNode(AlterTableStmt);
1519 n->relkind = OBJECT_INDEX;
1522 | ALTER SEQUENCE qualified_name alter_table_cmds
1524 AlterTableStmt *n = makeNode(AlterTableStmt);
1527 n->relkind = OBJECT_SEQUENCE;
1530 | ALTER VIEW qualified_name alter_table_cmds
1532 AlterTableStmt *n = makeNode(AlterTableStmt);
1535 n->relkind = OBJECT_VIEW;
1541 alter_table_cmd { $$ = list_make1($1); }
1542 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1546 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1547 ADD_P opt_column columnDef
1549 AlterTableCmd *n = makeNode(AlterTableCmd);
1550 n->subtype = AT_AddColumn;
1554 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1555 | ALTER opt_column ColId alter_column_default
1557 AlterTableCmd *n = makeNode(AlterTableCmd);
1558 n->subtype = AT_ColumnDefault;
1563 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1564 | ALTER opt_column ColId DROP NOT NULL_P
1566 AlterTableCmd *n = makeNode(AlterTableCmd);
1567 n->subtype = AT_DropNotNull;
1571 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1572 | ALTER opt_column ColId SET NOT NULL_P
1574 AlterTableCmd *n = makeNode(AlterTableCmd);
1575 n->subtype = AT_SetNotNull;
1579 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1580 | ALTER opt_column ColId SET STATISTICS SignedIconst
1582 AlterTableCmd *n = makeNode(AlterTableCmd);
1583 n->subtype = AT_SetStatistics;
1585 n->def = (Node *) makeInteger($6);
1588 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1589 | ALTER opt_column ColId SET STORAGE ColId
1591 AlterTableCmd *n = makeNode(AlterTableCmd);
1592 n->subtype = AT_SetStorage;
1594 n->def = (Node *) makeString($6);
1597 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1598 | DROP opt_column ColId opt_drop_behavior
1600 AlterTableCmd *n = makeNode(AlterTableCmd);
1601 n->subtype = AT_DropColumn;
1607 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1608 * [ USING <expression> ]
1610 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1612 AlterTableCmd *n = makeNode(AlterTableCmd);
1613 n->subtype = AT_AlterColumnType;
1615 n->def = (Node *) $6;
1619 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1620 | ADD_P TableConstraint
1622 AlterTableCmd *n = makeNode(AlterTableCmd);
1623 n->subtype = AT_AddConstraint;
1627 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1628 | DROP CONSTRAINT name opt_drop_behavior
1630 AlterTableCmd *n = makeNode(AlterTableCmd);
1631 n->subtype = AT_DropConstraint;
1636 /* ALTER TABLE <name> SET WITH OIDS */
1639 AlterTableCmd *n = makeNode(AlterTableCmd);
1640 n->subtype = AT_AddOids;
1643 /* ALTER TABLE <name> SET WITHOUT OIDS */
1646 AlterTableCmd *n = makeNode(AlterTableCmd);
1647 n->subtype = AT_DropOids;
1650 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1653 AlterTableCmd *n = makeNode(AlterTableCmd);
1654 n->subtype = AT_ClusterOn;
1658 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1659 | SET WITHOUT CLUSTER
1661 AlterTableCmd *n = makeNode(AlterTableCmd);
1662 n->subtype = AT_DropCluster;
1666 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1667 | ENABLE_P TRIGGER name
1669 AlterTableCmd *n = makeNode(AlterTableCmd);
1670 n->subtype = AT_EnableTrig;
1674 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1675 | ENABLE_P ALWAYS TRIGGER name
1677 AlterTableCmd *n = makeNode(AlterTableCmd);
1678 n->subtype = AT_EnableAlwaysTrig;
1682 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1683 | ENABLE_P REPLICA TRIGGER name
1685 AlterTableCmd *n = makeNode(AlterTableCmd);
1686 n->subtype = AT_EnableReplicaTrig;
1690 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1691 | ENABLE_P TRIGGER ALL
1693 AlterTableCmd *n = makeNode(AlterTableCmd);
1694 n->subtype = AT_EnableTrigAll;
1697 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1698 | ENABLE_P TRIGGER USER
1700 AlterTableCmd *n = makeNode(AlterTableCmd);
1701 n->subtype = AT_EnableTrigUser;
1704 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1705 | DISABLE_P TRIGGER name
1707 AlterTableCmd *n = makeNode(AlterTableCmd);
1708 n->subtype = AT_DisableTrig;
1712 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1713 | DISABLE_P TRIGGER ALL
1715 AlterTableCmd *n = makeNode(AlterTableCmd);
1716 n->subtype = AT_DisableTrigAll;
1719 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1720 | DISABLE_P TRIGGER USER
1722 AlterTableCmd *n = makeNode(AlterTableCmd);
1723 n->subtype = AT_DisableTrigUser;
1726 /* ALTER TABLE <name> ENABLE RULE <rule> */
1727 | ENABLE_P RULE name
1729 AlterTableCmd *n = makeNode(AlterTableCmd);
1730 n->subtype = AT_EnableRule;
1734 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1735 | ENABLE_P ALWAYS RULE name
1737 AlterTableCmd *n = makeNode(AlterTableCmd);
1738 n->subtype = AT_EnableAlwaysRule;
1742 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1743 | ENABLE_P REPLICA RULE name
1745 AlterTableCmd *n = makeNode(AlterTableCmd);
1746 n->subtype = AT_EnableReplicaRule;
1750 /* ALTER TABLE <name> DISABLE RULE <rule> */
1751 | DISABLE_P RULE name
1753 AlterTableCmd *n = makeNode(AlterTableCmd);
1754 n->subtype = AT_DisableRule;
1758 /* ALTER TABLE <name> INHERIT <parent> */
1759 | INHERIT qualified_name
1761 AlterTableCmd *n = makeNode(AlterTableCmd);
1762 n->subtype = AT_AddInherit;
1763 n->def = (Node *) $2;
1766 /* ALTER TABLE <name> NO INHERIT <parent> */
1767 | NO INHERIT qualified_name
1769 AlterTableCmd *n = makeNode(AlterTableCmd);
1770 n->subtype = AT_DropInherit;
1771 n->def = (Node *) $3;
1774 /* ALTER TABLE <name> OWNER TO RoleId */
1777 AlterTableCmd *n = makeNode(AlterTableCmd);
1778 n->subtype = AT_ChangeOwner;
1782 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1783 | SET TABLESPACE name
1785 AlterTableCmd *n = makeNode(AlterTableCmd);
1786 n->subtype = AT_SetTableSpace;
1790 /* ALTER TABLE <name> SET (...) */
1793 AlterTableCmd *n = makeNode(AlterTableCmd);
1794 n->subtype = AT_SetRelOptions;
1795 n->def = (Node *)$2;
1798 /* ALTER TABLE <name> RESET (...) */
1801 AlterTableCmd *n = makeNode(AlterTableCmd);
1802 n->subtype = AT_ResetRelOptions;
1803 n->def = (Node *)$2;
1808 alter_column_default:
1809 SET DEFAULT a_expr { $$ = $3; }
1810 | DROP DEFAULT { $$ = NULL; }
1814 CASCADE { $$ = DROP_CASCADE; }
1815 | RESTRICT { $$ = DROP_RESTRICT; }
1816 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1820 USING a_expr { $$ = $2; }
1821 | /* EMPTY */ { $$ = NULL; }
1825 '(' reloption_list ')' { $$ = $2; }
1828 opt_reloptions: WITH reloptions { $$ = $2; }
1829 | /* EMPTY */ { $$ = NIL; }
1833 reloption_elem { $$ = list_make1($1); }
1834 | reloption_list ',' reloption_elem { $$ = lappend($1, $3); }
1837 /* This should match def_elem and also allow qualified names */
1839 ColLabel '=' def_arg
1841 $$ = makeDefElem($1, (Node *) $3);
1845 $$ = makeDefElem($1, NULL);
1847 | ColLabel '.' ColLabel '=' def_arg
1849 $$ = makeDefElemExtended($1, $3, (Node *) $5,
1852 | ColLabel '.' ColLabel
1854 $$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
1859 /*****************************************************************************
1862 * close <portalname>
1864 *****************************************************************************/
1869 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1875 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1876 n->portalname = NULL;
1882 /*****************************************************************************
1885 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1887 * BINARY, OIDS, and DELIMITERS kept in old locations
1888 * for backward compatibility. 2002-06-18
1890 * COPY ( SELECT ... ) TO file [WITH options]
1891 * This form doesn't have the backwards-compatible option
1894 *****************************************************************************/
1896 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1897 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1899 CopyStmt *n = makeNode(CopyStmt);
1907 /* Concatenate user-supplied flags */
1909 n->options = lappend(n->options, $2);
1911 n->options = lappend(n->options, $5);
1913 n->options = lappend(n->options, $8);
1915 n->options = list_concat(n->options, $10);
1918 | COPY select_with_parens TO copy_file_name opt_with
1921 CopyStmt *n = makeNode(CopyStmt);
1934 | TO { $$ = FALSE; }
1938 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1939 * used depends on the direction. (It really doesn't make sense to copy from
1940 * stdout. We silently correct the "typo".) - AY 9/94
1944 | STDIN { $$ = NULL; }
1945 | STDOUT { $$ = NULL; }
1951 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1952 | /* EMPTY */ { $$ = NIL; }
1959 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1963 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1965 | DELIMITER opt_as Sconst
1967 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1969 | NULL_P opt_as Sconst
1971 $$ = makeDefElem("null", (Node *)makeString($3));
1975 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1979 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1981 | QUOTE opt_as Sconst
1983 $$ = makeDefElem("quote", (Node *)makeString($3));
1985 | ESCAPE opt_as Sconst
1987 $$ = makeDefElem("escape", (Node *)makeString($3));
1989 | FORCE QUOTE columnList
1991 $$ = makeDefElem("force_quote", (Node *)$3);
1993 | FORCE NOT NULL_P columnList
1995 $$ = makeDefElem("force_notnull", (Node *)$4);
1999 /* The following exist for backward compatibility */
2004 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
2006 | /*EMPTY*/ { $$ = NULL; }
2012 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2014 | /*EMPTY*/ { $$ = NULL; }
2018 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
2019 opt_using DELIMITERS Sconst
2021 $$ = makeDefElem("delimiter", (Node *)makeString($3));
2023 | /*EMPTY*/ { $$ = NULL; }
2032 /*****************************************************************************
2035 * CREATE TABLE relname
2037 *****************************************************************************/
2039 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2040 OptInherit OptWith OnCommitOption OptTableSpace
2042 CreateStmt *n = makeNode(CreateStmt);
2046 n->inhRelations = $8;
2047 n->constraints = NIL;
2050 n->tablespacename = $11;
2053 | CREATE OptTemp TABLE qualified_name OF qualified_name
2054 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
2056 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
2057 * by our inheritance capabilities. Let's try it...
2059 CreateStmt *n = makeNode(CreateStmt);
2063 n->inhRelations = list_make1($6);
2064 n->constraints = NIL;
2067 n->tablespacename = $12;
2073 * Redundancy here is needed to avoid shift/reduce conflicts,
2074 * since TEMP is not a reserved word. See also OptTempTableName.
2076 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2077 * the LOCAL keyword is really meaningless.
2079 OptTemp: TEMPORARY { $$ = TRUE; }
2080 | TEMP { $$ = TRUE; }
2081 | LOCAL TEMPORARY { $$ = TRUE; }
2082 | LOCAL TEMP { $$ = TRUE; }
2083 | GLOBAL TEMPORARY { $$ = TRUE; }
2084 | GLOBAL TEMP { $$ = TRUE; }
2085 | /*EMPTY*/ { $$ = FALSE; }
2088 OptTableElementList:
2089 TableElementList { $$ = $1; }
2090 | /*EMPTY*/ { $$ = NIL; }
2096 $$ = list_make1($1);
2098 | TableElementList ',' TableElement
2100 $$ = lappend($1, $3);
2105 columnDef { $$ = $1; }
2106 | TableLikeClause { $$ = $1; }
2107 | TableConstraint { $$ = $1; }
2110 columnDef: ColId Typename ColQualList
2112 ColumnDef *n = makeNode(ColumnDef);
2115 n->constraints = $3;
2122 ColQualList ColConstraint { $$ = lappend($1, $2); }
2123 | /*EMPTY*/ { $$ = NIL; }
2127 CONSTRAINT name ColConstraintElem
2129 switch (nodeTag($3))
2133 Constraint *n = (Constraint *)$3;
2137 case T_FkConstraint:
2139 FkConstraint *n = (FkConstraint *)$3;
2140 n->constr_name = $2;
2148 | ColConstraintElem { $$ = $1; }
2149 | ConstraintAttr { $$ = $1; }
2152 /* DEFAULT NULL is already the default for Postgres.
2153 * But define it here and carry it forward into the system
2154 * to make it explicit.
2155 * - thomas 1998-09-13
2157 * WITH NULL and NULL are not SQL92-standard syntax elements,
2158 * so leave them out. Use DEFAULT NULL to explicitly indicate
2159 * that a column may have that value. WITH NULL leads to
2160 * shift/reduce conflicts with WITH TIME ZONE anyway.
2161 * - thomas 1999-01-08
2163 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2164 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2165 * or be part of a_expr NOT LIKE or similar constructs).
2170 Constraint *n = makeNode(Constraint);
2171 n->contype = CONSTR_NOTNULL;
2174 n->cooked_expr = NULL;
2176 n->indexspace = NULL;
2181 Constraint *n = makeNode(Constraint);
2182 n->contype = CONSTR_NULL;
2185 n->cooked_expr = NULL;
2187 n->indexspace = NULL;
2190 | UNIQUE opt_definition OptConsTableSpace
2192 Constraint *n = makeNode(Constraint);
2193 n->contype = CONSTR_UNIQUE;
2196 n->cooked_expr = NULL;
2202 | PRIMARY KEY opt_definition OptConsTableSpace
2204 Constraint *n = makeNode(Constraint);
2205 n->contype = CONSTR_PRIMARY;
2208 n->cooked_expr = NULL;
2214 | CHECK '(' a_expr ')'
2216 Constraint *n = makeNode(Constraint);
2217 n->contype = CONSTR_CHECK;
2220 n->cooked_expr = NULL;
2222 n->indexspace = NULL;
2227 Constraint *n = makeNode(Constraint);
2228 n->contype = CONSTR_DEFAULT;
2231 n->cooked_expr = NULL;
2233 n->indexspace = NULL;
2236 | REFERENCES qualified_name opt_column_list key_match key_actions
2238 FkConstraint *n = makeNode(FkConstraint);
2239 n->constr_name = NULL;
2243 n->fk_matchtype = $4;
2244 n->fk_upd_action = (char) ($5 >> 8);
2245 n->fk_del_action = (char) ($5 & 0xFF);
2246 n->deferrable = FALSE;
2247 n->initdeferred = FALSE;
2253 * ConstraintAttr represents constraint attributes, which we parse as if
2254 * they were independent constraint clauses, in order to avoid shift/reduce
2255 * conflicts (since NOT might start either an independent NOT NULL clause
2256 * or an attribute). parse_utilcmd.c is responsible for attaching the
2257 * attribute information to the preceding "real" constraint node, and for
2258 * complaining if attribute clauses appear in the wrong place or wrong
2261 * See also ConstraintAttributeSpec, which can be used in places where
2262 * there is no parsing conflict.
2267 Constraint *n = makeNode(Constraint);
2268 n->contype = CONSTR_ATTR_DEFERRABLE;
2273 Constraint *n = makeNode(Constraint);
2274 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2277 | INITIALLY DEFERRED
2279 Constraint *n = makeNode(Constraint);
2280 n->contype = CONSTR_ATTR_DEFERRED;
2283 | INITIALLY IMMEDIATE
2285 Constraint *n = makeNode(Constraint);
2286 n->contype = CONSTR_ATTR_IMMEDIATE;
2293 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2294 * This seems to be a poor man's inheritance capability, with the resulting
2295 * tables completely decoupled except for the original commonality in definitions.
2297 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2298 * which is a part of SQL:2003.
2301 LIKE qualified_name TableLikeOptionList
2303 InhRelation *n = makeNode(InhRelation);
2310 TableLikeOptionList:
2311 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2312 | /* EMPTY */ { $$ = NIL; }
2316 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2317 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2318 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2319 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2320 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2321 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2325 /* ConstraintElem specifies constraint syntax which is not embedded into
2326 * a column definition. ColConstraintElem specifies the embedded form.
2327 * - thomas 1997-12-03
2330 CONSTRAINT name ConstraintElem
2332 switch (nodeTag($3))
2336 Constraint *n = (Constraint *)$3;
2340 case T_FkConstraint:
2342 FkConstraint *n = (FkConstraint *)$3;
2343 n->constr_name = $2;
2351 | ConstraintElem { $$ = $1; }
2355 CHECK '(' a_expr ')'
2357 Constraint *n = makeNode(Constraint);
2358 n->contype = CONSTR_CHECK;
2361 n->cooked_expr = NULL;
2362 n->indexspace = NULL;
2365 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2367 Constraint *n = makeNode(Constraint);
2368 n->contype = CONSTR_UNIQUE;
2371 n->cooked_expr = NULL;
2377 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2379 Constraint *n = makeNode(Constraint);
2380 n->contype = CONSTR_PRIMARY;
2383 n->cooked_expr = NULL;
2389 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2390 opt_column_list key_match key_actions ConstraintAttributeSpec
2392 FkConstraint *n = makeNode(FkConstraint);
2393 n->constr_name = NULL;
2397 n->fk_matchtype = $9;
2398 n->fk_upd_action = (char) ($10 >> 8);
2399 n->fk_del_action = (char) ($10 & 0xFF);
2400 n->deferrable = ($11 & 1) != 0;
2401 n->initdeferred = ($11 & 2) != 0;
2407 '(' columnList ')' { $$ = $2; }
2408 | /*EMPTY*/ { $$ = NIL; }
2412 columnElem { $$ = list_make1($1); }
2413 | columnList ',' columnElem { $$ = lappend($1, $3); }
2418 $$ = (Node *) makeString($1);
2422 key_match: MATCH FULL
2424 $$ = FKCONSTR_MATCH_FULL;
2429 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2430 errmsg("MATCH PARTIAL not yet implemented"),
2431 scanner_errposition(@1)));
2432 $$ = FKCONSTR_MATCH_PARTIAL;
2436 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2440 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2445 * We combine the update and delete actions into one value temporarily
2446 * for simplicity of parsing, and then break them down again in the
2447 * calling production. update is in the left 8 bits, delete in the right.
2448 * Note that NOACTION is the default.
2452 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2454 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2455 | key_update key_delete
2456 { $$ = ($1 << 8) | ($2 & 0xFF); }
2457 | key_delete key_update
2458 { $$ = ($2 << 8) | ($1 & 0xFF); }
2460 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2463 key_update: ON UPDATE key_action { $$ = $3; }
2466 key_delete: ON DELETE_P key_action { $$ = $3; }
2470 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2471 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2472 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2473 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2474 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2477 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2478 | /*EMPTY*/ { $$ = NIL; }
2481 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2483 WITH reloptions { $$ = $2; }
2484 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2485 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2486 | /*EMPTY*/ { $$ = NIL; }
2489 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2490 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2491 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2492 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2495 OptTableSpace: TABLESPACE name { $$ = $2; }
2496 | /*EMPTY*/ { $$ = NULL; }
2499 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2500 | /*EMPTY*/ { $$ = NULL; }
2505 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2510 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2513 * When the SelectStmt is a set-operation tree, we must
2514 * stuff the INTO information into the leftmost component
2515 * Select, because that's where analyze.c will expect
2516 * to find it. Similarly, the output column names must
2517 * be attached to that Select's target list.
2519 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2520 if (n->intoClause != NULL)
2522 (errcode(ERRCODE_SYNTAX_ERROR),
2523 errmsg("CREATE TABLE AS cannot specify INTO"),
2524 scanner_errposition(exprLocation((Node *) n->intoClause))));
2525 $4->rel->istemp = $2;
2527 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2529 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2535 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2537 $$ = makeNode(IntoClause);
2542 $$->tableSpaceName = $5;
2547 '(' CreateAsList ')' { $$ = $2; }
2548 | /*EMPTY*/ { $$ = NIL; }
2552 CreateAsElement { $$ = list_make1($1); }
2553 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2559 ColumnDef *n = makeNode(ColumnDef);
2564 n->is_not_null = false;
2565 n->raw_default = NULL;
2566 n->cooked_default = NULL;
2567 n->constraints = NIL;
2573 WITH DATA_P { $$ = TRUE; }
2574 | WITH NO DATA_P { $$ = FALSE; }
2575 | /*EMPTY*/ { $$ = TRUE; }
2579 /*****************************************************************************
2582 * CREATE SEQUENCE seqname
2583 * ALTER SEQUENCE seqname
2585 *****************************************************************************/
2588 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2590 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2599 ALTER SEQUENCE qualified_name SeqOptList
2601 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2608 OptSeqOptList: SeqOptList { $$ = $1; }
2609 | /*EMPTY*/ { $$ = NIL; }
2612 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2613 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2616 SeqOptElem: CACHE NumericOnly
2618 $$ = makeDefElem("cache", (Node *)$2);
2622 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2626 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2628 | INCREMENT opt_by NumericOnly
2630 $$ = makeDefElem("increment", (Node *)$3);
2632 | MAXVALUE NumericOnly
2634 $$ = makeDefElem("maxvalue", (Node *)$2);
2636 | MINVALUE NumericOnly
2638 $$ = makeDefElem("minvalue", (Node *)$2);
2642 $$ = makeDefElem("maxvalue", NULL);
2646 $$ = makeDefElem("minvalue", NULL);
2650 $$ = makeDefElem("owned_by", (Node *)$3);
2652 | START opt_with NumericOnly
2654 $$ = makeDefElem("start", (Node *)$3);
2658 $$ = makeDefElem("restart", NULL);
2660 | RESTART opt_with NumericOnly
2662 $$ = makeDefElem("restart", (Node *)$3);
2671 FCONST { $$ = makeFloat($1); }
2677 | SignedIconst { $$ = makeInteger($1); };
2680 /*****************************************************************************
2683 * CREATE PROCEDURAL LANGUAGE ...
2684 * DROP PROCEDURAL LANGUAGE ...
2686 *****************************************************************************/
2689 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2691 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2693 /* parameters are all to be supplied by system */
2695 n->plvalidator = NIL;
2696 n->pltrusted = false;
2699 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2700 HANDLER handler_name opt_validator opt_lancompiler
2702 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2705 n->plvalidator = $8;
2707 /* LANCOMPILER is now ignored entirely */
2713 TRUSTED { $$ = TRUE; }
2714 | /*EMPTY*/ { $$ = FALSE; }
2717 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2718 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2719 * Work around by using simple names, instead.
2722 name { $$ = list_make1(makeString($1)); }
2723 | name attrs { $$ = lcons(makeString($1), $2); }
2727 VALIDATOR handler_name { $$ = $2; }
2728 | NO VALIDATOR { $$ = NIL; }
2732 validator_clause { $$ = $1; }
2733 | /*EMPTY*/ { $$ = NIL; }
2737 LANCOMPILER Sconst { $$ = $2; }
2738 | /*EMPTY*/ { $$ = NULL; }
2742 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2744 DropPLangStmt *n = makeNode(DropPLangStmt);
2747 n->missing_ok = false;
2750 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2752 DropPLangStmt *n = makeNode(DropPLangStmt);
2755 n->missing_ok = true;
2765 /*****************************************************************************
2768 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2770 *****************************************************************************/
2772 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2774 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2775 n->tablespacename = $3;
2782 OptTableSpaceOwner: OWNER name { $$ = $2; }
2783 | /*EMPTY */ { $$ = NULL; }
2786 /*****************************************************************************
2789 * DROP TABLESPACE <tablespace>
2791 * No need for drop behaviour as we cannot implement dependencies for
2792 * objects in other databases; we can only support RESTRICT.
2794 ****************************************************************************/
2796 DropTableSpaceStmt: DROP TABLESPACE name
2798 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2799 n->tablespacename = $3;
2800 n->missing_ok = false;
2803 | DROP TABLESPACE IF_P EXISTS name
2805 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2806 n->tablespacename = $5;
2807 n->missing_ok = true;
2812 /*****************************************************************************
2815 * CREATE FOREIGN DATA WRAPPER name [ VALIDATOR name ]
2817 *****************************************************************************/
2819 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_validator create_generic_options
2821 CreateFdwStmt *n = makeNode(CreateFdwStmt);
2829 /*****************************************************************************
2832 * DROP FOREIGN DATA WRAPPER name
2834 ****************************************************************************/
2836 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
2838 DropFdwStmt *n = makeNode(DropFdwStmt);
2840 n->missing_ok = false;
2844 | DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
2846 DropFdwStmt *n = makeNode(DropFdwStmt);
2848 n->missing_ok = true;
2854 /*****************************************************************************
2857 * ALTER FOREIGN DATA WRAPPER name
2859 ****************************************************************************/
2861 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name validator_clause alter_generic_options
2863 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2866 n->change_validator = true;
2870 | ALTER FOREIGN DATA_P WRAPPER name validator_clause
2872 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2875 n->change_validator = true;
2878 | ALTER FOREIGN DATA_P WRAPPER name alter_generic_options
2880 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2887 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
2888 create_generic_options:
2889 OPTIONS '(' generic_option_list ')' { $$ = $3; }
2890 | /*EMPTY*/ { $$ = NIL; }
2893 generic_option_list:
2896 $$ = list_make1($1);
2898 | generic_option_list ',' generic_option_elem
2900 $$ = lappend($1, $3);
2904 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
2905 alter_generic_options:
2906 OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
2909 alter_generic_option_list:
2910 alter_generic_option_elem
2912 $$ = list_make1($1);
2914 | alter_generic_option_list ',' alter_generic_option_elem
2916 $$ = lappend($1, $3);
2920 alter_generic_option_elem:
2925 | SET generic_option_elem
2928 $$->defaction = DEFELEM_SET;
2930 | ADD_P generic_option_elem
2933 $$->defaction = DEFELEM_ADD;
2935 | DROP generic_option_name
2937 $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
2941 generic_option_elem:
2942 generic_option_name generic_option_arg
2944 $$ = makeDefElem($1, $2);
2948 generic_option_name:
2949 ColLabel { $$ = $1; }
2952 /* We could use def_arg here, but the spec only requires string literals */
2954 Sconst { $$ = (Node *) makeString($1); }
2957 /*****************************************************************************
2960 * CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
2962 *****************************************************************************/
2964 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
2965 FOREIGN DATA_P WRAPPER name create_generic_options
2967 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
2978 TYPE_P Sconst { $$ = $2; }
2979 | /*EMPTY*/ { $$ = NULL; }
2983 foreign_server_version:
2984 VERSION_P Sconst { $$ = $2; }
2985 | VERSION_P NULL_P { $$ = NULL; }
2988 opt_foreign_server_version:
2989 foreign_server_version { $$ = $1; }
2990 | /*EMPTY*/ { $$ = NULL; }
2993 /*****************************************************************************
2998 ****************************************************************************/
3000 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
3002 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3004 n->missing_ok = false;
3008 | DROP SERVER IF_P EXISTS name opt_drop_behavior
3010 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3012 n->missing_ok = true;
3018 /*****************************************************************************
3021 * ALTER SERVER name [VERSION] [OPTIONS]
3023 ****************************************************************************/
3025 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
3027 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3031 n->has_version = true;
3034 | ALTER SERVER name foreign_server_version
3036 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3039 n->has_version = true;
3042 | ALTER SERVER name alter_generic_options
3044 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3051 /*****************************************************************************
3054 * CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
3056 *****************************************************************************/
3058 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
3060 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3068 /* User mapping authorization identifier */
3070 CURRENT_USER { $$ = "current_user"; }
3071 | USER { $$ = "current_user"; }
3072 | RoleId { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3075 /*****************************************************************************
3078 * DROP USER MAPPING FOR auth_ident SERVER name
3080 ****************************************************************************/
3082 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3084 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3087 n->missing_ok = false;
3090 | DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3092 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3095 n->missing_ok = true;
3100 /*****************************************************************************
3103 * ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3105 ****************************************************************************/
3107 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3109 AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3117 /*****************************************************************************
3120 * CREATE TRIGGER ...
3123 *****************************************************************************/
3126 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3127 qualified_name TriggerForSpec EXECUTE PROCEDURE
3128 func_name '(' TriggerFuncArgs ')'
3130 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3138 n->isconstraint = FALSE;
3139 n->deferrable = FALSE;
3140 n->initdeferred = FALSE;
3141 n->constrrel = NULL;
3144 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
3145 qualified_name OptConstrFromTable
3146 ConstraintAttributeSpec
3147 FOR EACH ROW EXECUTE PROCEDURE
3148 func_name '(' TriggerFuncArgs ')'
3150 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3158 n->isconstraint = TRUE;
3159 n->deferrable = ($10 & 1) != 0;
3160 n->initdeferred = ($10 & 2) != 0;
3167 BEFORE { $$ = TRUE; }
3168 | AFTER { $$ = FALSE; }
3174 | TriggerEvents OR TriggerOneEvent
3177 yyerror("duplicate trigger events specified");
3183 INSERT { $$ = TRIGGER_TYPE_INSERT; }
3184 | DELETE_P { $$ = TRIGGER_TYPE_DELETE; }
3185 | UPDATE { $$ = TRIGGER_TYPE_UPDATE; }
3186 | TRUNCATE { $$ = TRIGGER_TYPE_TRUNCATE; }
3190 FOR TriggerForOpt TriggerForType
3197 * If ROW/STATEMENT not specified, default to
3198 * STATEMENT, per SQL
3211 | STATEMENT { $$ = FALSE; }
3215 TriggerFuncArg { $$ = list_make1($1); }
3216 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
3217 | /*EMPTY*/ { $$ = NIL; }
3224 snprintf(buf, sizeof(buf), "%d", $1);
3225 $$ = makeString(pstrdup(buf));
3227 | FCONST { $$ = makeString($1); }
3228 | Sconst { $$ = makeString($1); }
3229 | BCONST { $$ = makeString($1); }
3230 | XCONST { $$ = makeString($1); }
3231 | ColId { $$ = makeString($1); }
3235 FROM qualified_name { $$ = $2; }
3236 | /*EMPTY*/ { $$ = NULL; }
3239 ConstraintAttributeSpec:
3240 ConstraintDeferrabilitySpec
3242 | ConstraintDeferrabilitySpec ConstraintTimeSpec
3244 if ($1 == 0 && $2 != 0)
3246 (errcode(ERRCODE_SYNTAX_ERROR),
3247 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3248 scanner_errposition(@1)));
3251 | ConstraintTimeSpec
3258 | ConstraintTimeSpec ConstraintDeferrabilitySpec
3260 if ($2 == 0 && $1 != 0)
3262 (errcode(ERRCODE_SYNTAX_ERROR),
3263 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3264 scanner_errposition(@1)));
3271 ConstraintDeferrabilitySpec:
3272 NOT DEFERRABLE { $$ = 0; }
3273 | DEFERRABLE { $$ = 1; }
3277 INITIALLY IMMEDIATE { $$ = 0; }
3278 | INITIALLY DEFERRED { $$ = 2; }
3283 DROP TRIGGER name ON qualified_name opt_drop_behavior
3285 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3289 n->removeType = OBJECT_TRIGGER;
3290 n->missing_ok = false;
3293 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
3295 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3299 n->removeType = OBJECT_TRIGGER;
3300 n->missing_ok = true;
3306 /*****************************************************************************
3309 * CREATE ASSERTION ...
3310 * DROP ASSERTION ...
3312 *****************************************************************************/
3315 CREATE ASSERTION name CHECK '(' a_expr ')'
3316 ConstraintAttributeSpec
3318 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3320 n->args = list_make1($6);
3321 n->isconstraint = TRUE;
3322 n->deferrable = ($8 & 1) != 0;
3323 n->initdeferred = ($8 & 2) != 0;
3326 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3327 errmsg("CREATE ASSERTION is not yet implemented")));
3334 DROP ASSERTION name opt_drop_behavior
3336 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3340 n->removeType = OBJECT_TRIGGER; /* XXX */
3342 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3343 errmsg("DROP ASSERTION is not yet implemented")));
3349 /*****************************************************************************
3352 * define (aggregate,operator,type)
3354 *****************************************************************************/
3357 CREATE AGGREGATE func_name aggr_args definition
3359 DefineStmt *n = makeNode(DefineStmt);
3360 n->kind = OBJECT_AGGREGATE;
3361 n->oldstyle = false;
3367 | CREATE AGGREGATE func_name old_aggr_definition
3369 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
3370 DefineStmt *n = makeNode(DefineStmt);
3371 n->kind = OBJECT_AGGREGATE;
3378 | CREATE OPERATOR any_operator definition
3380 DefineStmt *n = makeNode(DefineStmt);
3381 n->kind = OBJECT_OPERATOR;
3382 n->oldstyle = false;
3388 | CREATE TYPE_P any_name definition
3390 DefineStmt *n = makeNode(DefineStmt);
3391 n->kind = OBJECT_TYPE;
3392 n->oldstyle = false;
3398 | CREATE TYPE_P any_name
3400 /* Shell type (identified by lack of definition) */
3401 DefineStmt *n = makeNode(DefineStmt);
3402 n->kind = OBJECT_TYPE;
3403 n->oldstyle = false;
3406 n->definition = NIL;
3409 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3411 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3412 RangeVar *r = makeNode(RangeVar);
3414 /* can't use qualified_name, sigh */
3415 switch (list_length($3))
3418 r->catalogname = NULL;
3419 r->schemaname = NULL;
3420 r->relname = strVal(linitial($3));
3423 r->catalogname = NULL;
3424 r->schemaname = strVal(linitial($3));
3425 r->relname = strVal(lsecond($3));
3428 r->catalogname = strVal(linitial($3));
3429 r->schemaname = strVal(lsecond($3));
3430 r->relname = strVal(lthird($3));
3434 (errcode(ERRCODE_SYNTAX_ERROR),
3435 errmsg("improper qualified name (too many dotted names): %s",
3436 NameListToString($3)),
3437 scanner_errposition(@3)));
3445 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3447 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3452 | CREATE TEXT_P SEARCH PARSER any_name definition
3454 DefineStmt *n = makeNode(DefineStmt);
3455 n->kind = OBJECT_TSPARSER;
3461 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3463 DefineStmt *n = makeNode(DefineStmt);
3464 n->kind = OBJECT_TSDICTIONARY;
3470 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3472 DefineStmt *n = makeNode(DefineStmt);
3473 n->kind = OBJECT_TSTEMPLATE;
3479 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3481 DefineStmt *n = makeNode(DefineStmt);
3482 n->kind = OBJECT_TSCONFIGURATION;
3490 definition: '(' def_list ')' { $$ = $2; }
3493 def_list: def_elem { $$ = list_make1($1); }
3494 | def_list ',' def_elem { $$ = lappend($1, $3); }
3497 def_elem: ColLabel '=' def_arg
3499 $$ = makeDefElem($1, (Node *) $3);
3503 $$ = makeDefElem($1, NULL);
3507 /* Note: any simple identifier will be returned as a type name! */
3508 def_arg: func_type { $$ = (Node *)$1; }
3509 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3510 | qual_all_Op { $$ = (Node *)$1; }
3511 | NumericOnly { $$ = (Node *)$1; }
3512 | Sconst { $$ = (Node *)makeString($1); }
3515 aggr_args: '(' type_list ')' { $$ = $2; }
3516 | '(' '*' ')' { $$ = NIL; }
3519 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3522 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3523 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3527 * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
3528 * the item names needed in old aggregate definitions are likely to become
3531 old_aggr_elem: IDENT '=' def_arg
3533 $$ = makeDefElem($1, (Node *)$3);
3537 enum_val_list: Sconst
3538 { $$ = list_make1(makeString($1)); }
3539 | enum_val_list ',' Sconst
3540 { $$ = lappend($1, makeString($3)); }
3544 /*****************************************************************************
3547 * CREATE OPERATOR CLASS ...
3548 * CREATE OPERATOR FAMILY ...
3549 * ALTER OPERATOR FAMILY ...
3550 * DROP OPERATOR CLASS ...
3551 * DROP OPERATOR FAMILY ...
3553 *****************************************************************************/
3556 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3557 USING access_method opt_opfamily AS opclass_item_list
3559 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3560 n->opclassname = $4;
3564 n->opfamilyname = $11;
3571 opclass_item { $$ = list_make1($1); }
3572 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3576 OPERATOR Iconst any_operator opt_recheck
3578 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3579 n->itemtype = OPCLASS_ITEM_OPERATOR;
3585 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3587 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3588 n->itemtype = OPCLASS_ITEM_OPERATOR;
3594 | FUNCTION Iconst func_name func_args
3596 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3597 n->itemtype = OPCLASS_ITEM_FUNCTION;
3599 n->args = extractArgTypes($4);
3603 | FUNCTION Iconst '(' type_list ')' func_name func_args
3605 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3606 n->itemtype = OPCLASS_ITEM_FUNCTION;
3608 n->args = extractArgTypes($7);
3615 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3616 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3622 opt_default: DEFAULT { $$ = TRUE; }
3623 | /*EMPTY*/ { $$ = FALSE; }
3626 opt_opfamily: FAMILY any_name { $$ = $2; }
3627 | /*EMPTY*/ { $$ = NIL; }
3630 opt_recheck: RECHECK
3633 * RECHECK no longer does anything in opclass definitions,
3634 * but we still accept it to ease porting of old database
3638 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3639 errmsg("RECHECK is no longer required"),
3640 errhint("Update your data type."),
3641 scanner_errposition(@1)));
3644 | /*EMPTY*/ { $$ = FALSE; }
3649 CREATE OPERATOR FAMILY any_name USING access_method
3651 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3652 n->opfamilyname = $4;
3659 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3661 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3662 n->opfamilyname = $4;
3668 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3670 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3671 n->opfamilyname = $4;
3680 opclass_drop { $$ = list_make1($1); }
3681 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3685 OPERATOR Iconst '(' type_list ')'
3687 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3688 n->itemtype = OPCLASS_ITEM_OPERATOR;
3693 | FUNCTION Iconst '(' type_list ')'
3695 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3696 n->itemtype = OPCLASS_ITEM_FUNCTION;
3705 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3707 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3708 n->opclassname = $4;
3711 n->missing_ok = false;
3714 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3716 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3717 n->opclassname = $6;
3720 n->missing_ok = true;
3726 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3728 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3729 n->opfamilyname = $4;
3732 n->missing_ok = false;
3735 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3737 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3738 n->opfamilyname = $6;
3741 n->missing_ok = true;
3747 /*****************************************************************************
3751 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3752 * REASSIGN OWNED BY username [, username ...] TO username
3754 *****************************************************************************/
3756 DROP OWNED BY name_list opt_drop_behavior
3758 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3766 REASSIGN OWNED BY name_list TO name
3768 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3775 /*****************************************************************************
3779 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3780 * [ RESTRICT | CASCADE ]
3782 *****************************************************************************/
3784 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3786 DropStmt *n = makeNode(DropStmt);
3788 n->missing_ok = TRUE;
3793 | DROP drop_type any_name_list opt_drop_behavior
3795 DropStmt *n = makeNode(DropStmt);
3797 n->missing_ok = FALSE;
3805 drop_type: TABLE { $$ = OBJECT_TABLE; }
3806 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3807 | VIEW { $$ = OBJECT_VIEW; }
3808 | INDEX { $$ = OBJECT_INDEX; }
3809 | TYPE_P { $$ = OBJECT_TYPE; }
3810 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3811 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3812 | SCHEMA { $$ = OBJECT_SCHEMA; }
3813 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3814 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3815 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3816 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3820 any_name { $$ = list_make1($1); }
3821 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3824 any_name: ColId { $$ = list_make1(makeString($1)); }
3825 | ColId attrs { $$ = lcons(makeString($1), $2); }
3828 attrs: '.' attr_name
3829 { $$ = list_make1(makeString($2)); }
3830 | attrs '.' attr_name
3831 { $$ = lappend($1, makeString($3)); }
3835 /*****************************************************************************
3838 * truncate table relname1, relname2, ...
3840 *****************************************************************************/
3843 TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
3845 TruncateStmt *n = makeNode(TruncateStmt);
3847 n->restart_seqs = $4;
3854 CONTINUE_P IDENTITY_P { $$ = false; }
3855 | RESTART IDENTITY_P { $$ = true; }
3856 | /* EMPTY */ { $$ = false; }
3859 /*****************************************************************************
3861 * The COMMENT ON statement can take different forms based upon the type of
3862 * the object associated with the comment. The form of the statement is:
3864 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3865 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3866 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3867 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3868 * TEXT SEARCH TEMPLATE |
3869 * TEXT SEARCH CONFIGURATION ] <objname> |
3870 * AGGREGATE <aggname> (arg1, ...) |
3871 * FUNCTION <funcname> (arg1, arg2, ...) |
3872 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3873 * TRIGGER <triggername> ON <relname> |
3874 * CONSTRAINT <constraintname> ON <relname> |
3875 * RULE <rulename> ON <relname> ]
3878 *****************************************************************************/
3881 COMMENT ON comment_type any_name IS comment_text
3883 CommentStmt *n = makeNode(CommentStmt);
3890 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3892 CommentStmt *n = makeNode(CommentStmt);
3893 n->objtype = OBJECT_AGGREGATE;
3899 | COMMENT ON FUNCTION func_name func_args IS comment_text
3901 CommentStmt *n = makeNode(CommentStmt);
3902 n->objtype = OBJECT_FUNCTION;
3904 n->objargs = extractArgTypes($5);
3908 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3910 CommentStmt *n = makeNode(CommentStmt);
3911 n->objtype = OBJECT_OPERATOR;
3917 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3919 CommentStmt *n = makeNode(CommentStmt);
3920 n->objtype = OBJECT_CONSTRAINT;
3921 n->objname = lappend($6, makeString($4));
3926 | COMMENT ON RULE name ON any_name IS comment_text
3928 CommentStmt *n = makeNode(CommentStmt);
3929 n->objtype = OBJECT_RULE;
3930 n->objname = lappend($6, makeString($4));
3935 | COMMENT ON RULE name IS comment_text
3937 /* Obsolete syntax supported for awhile for compatibility */
3938 CommentStmt *n = makeNode(CommentStmt);
3939 n->objtype = OBJECT_RULE;
3940 n->objname = list_make1(makeString($4));
3945 | COMMENT ON TRIGGER name ON any_name IS comment_text
3947 CommentStmt *n = makeNode(CommentStmt);
3948 n->objtype = OBJECT_TRIGGER;
3949 n->objname = lappend($6, makeString($4));
3954 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3956 CommentStmt *n = makeNode(CommentStmt);
3957 n->objtype = OBJECT_OPCLASS;
3959 n->objargs = list_make1(makeString($7));
3963 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3965 CommentStmt *n = makeNode(CommentStmt);
3966 n->objtype = OBJECT_OPFAMILY;
3968 n->objargs = list_make1(makeString($7));
3972 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3974 CommentStmt *n = makeNode(CommentStmt);
3975 n->objtype = OBJECT_LARGEOBJECT;
3976 n->objname = list_make1($5);
3981 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3983 CommentStmt *n = makeNode(CommentStmt);
3984 n->objtype = OBJECT_CAST;
3985 n->objname = list_make1($5);
3986 n->objargs = list_make1($7);
3990 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3992 CommentStmt *n = makeNode(CommentStmt);
3993 n->objtype = OBJECT_LANGUAGE;
3999 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
4001 CommentStmt *n = makeNode(CommentStmt);
4002 n->objtype = OBJECT_TSPARSER;
4007 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
4009 CommentStmt *n = makeNode(CommentStmt);
4010 n->objtype = OBJECT_TSDICTIONARY;
4015 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
4017 CommentStmt *n = makeNode(CommentStmt);
4018 n->objtype = OBJECT_TSTEMPLATE;
4023 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
4025 CommentStmt *n = makeNode(CommentStmt);
4026 n->objtype = OBJECT_TSCONFIGURATION;
4034 COLUMN { $$ = OBJECT_COLUMN; }
4035 | DATABASE { $$ = OBJECT_DATABASE; }
4036 | SCHEMA { $$ = OBJECT_SCHEMA; }
4037 | INDEX { $$ = OBJECT_INDEX; }
4038 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
4039 | TABLE { $$ = OBJECT_TABLE; }
4040 | DOMAIN_P { $$ = OBJECT_TYPE; }
4041 | TYPE_P { $$ = OBJECT_TYPE; }
4042 | VIEW { $$ = OBJECT_VIEW; }
4043 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
4044 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
4045 | ROLE { $$ = OBJECT_ROLE; }
4050 | NULL_P { $$ = NULL; }
4053 /*****************************************************************************
4058 *****************************************************************************/
4060 FetchStmt: FETCH fetch_direction from_in name
4062 FetchStmt *n = (FetchStmt *) $2;
4069 FetchStmt *n = makeNode(FetchStmt);
4070 n->direction = FETCH_FORWARD;
4076 | MOVE fetch_direction from_in name
4078 FetchStmt *n = (FetchStmt *) $2;
4085 FetchStmt *n = makeNode(FetchStmt);
4086 n->direction = FETCH_FORWARD;
4097 FetchStmt *n = makeNode(FetchStmt);
4098 n->direction = FETCH_FORWARD;
4104 FetchStmt *n = makeNode(FetchStmt);
4105 n->direction = FETCH_FORWARD;
4111 FetchStmt *n = makeNode(FetchStmt);
4112 n->direction = FETCH_BACKWARD;
4118 FetchStmt *n = makeNode(FetchStmt);
4119 n->direction = FETCH_ABSOLUTE;
4125 FetchStmt *n = makeNode(FetchStmt);
4126 n->direction = FETCH_ABSOLUTE;
4130 | ABSOLUTE_P SignedIconst
4132 FetchStmt *n = makeNode(FetchStmt);
4133 n->direction = FETCH_ABSOLUTE;
4137 | RELATIVE_P SignedIconst
4139 FetchStmt *n = makeNode(FetchStmt);
4140 n->direction = FETCH_RELATIVE;
4146 FetchStmt *n = makeNode(FetchStmt);
4147 n->direction = FETCH_FORWARD;
4153 FetchStmt *n = makeNode(FetchStmt);
4154 n->direction = FETCH_FORWARD;
4155 n->howMany = FETCH_ALL;
4160 FetchStmt *n = makeNode(FetchStmt);
4161 n->direction = FETCH_FORWARD;
4165 | FORWARD SignedIconst
4167 FetchStmt *n = makeNode(FetchStmt);
4168 n->direction = FETCH_FORWARD;
4174 FetchStmt *n = makeNode(FetchStmt);
4175 n->direction = FETCH_FORWARD;
4176 n->howMany = FETCH_ALL;
4181 FetchStmt *n = makeNode(FetchStmt);
4182 n->direction = FETCH_BACKWARD;
4186 | BACKWARD SignedIconst
4188 FetchStmt *n = makeNode(FetchStmt);
4189 n->direction = FETCH_BACKWARD;
4195 FetchStmt *n = makeNode(FetchStmt);
4196 n->direction = FETCH_BACKWARD;
4197 n->howMany = FETCH_ALL;
4207 /*****************************************************************************
4209 * GRANT and REVOKE statements
4211 *****************************************************************************/
4213 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
4214 opt_grant_grant_option
4216 GrantStmt *n = makeNode(GrantStmt);
4219 n->objtype = ($4)->objtype;
4220 n->objects = ($4)->objs;
4222 n->grant_option = $7;
4228 REVOKE privileges ON privilege_target
4229 FROM grantee_list opt_drop_behavior
4231 GrantStmt *n = makeNode(GrantStmt);
4232 n->is_grant = false;
4233 n->grant_option = false;
4235 n->objtype = ($4)->objtype;
4236 n->objects = ($4)->objs;
4241 | REVOKE GRANT OPTION FOR privileges ON privilege_target
4242 FROM grantee_list opt_drop_behavior
4244 GrantStmt *n = makeNode(GrantStmt);
4245 n->is_grant = false;
4246 n->grant_option = true;
4248 n->objtype = ($7)->objtype;
4249 n->objects = ($7)->objs;
4258 * Privilege names are represented as strings; the validity of the privilege
4259 * names gets checked at execution. This is a bit annoying but we have little
4260 * choice because of the syntactic conflict with lists of role names in
4261 * GRANT/REVOKE. What's more, we have to call out in the "privilege"
4262 * production any reserved keywords that need to be usable as privilege names.
4265 /* either ALL [PRIVILEGES] or a list of individual privileges */
4266 privileges: privilege_list
4272 | ALL '(' columnList ')'
4274 AccessPriv *n = makeNode(AccessPriv);
4275 n->priv_name = NULL;
4279 | ALL PRIVILEGES '(' columnList ')'
4281 AccessPriv *n = makeNode(AccessPriv);
4282 n->priv_name = NULL;
4288 privilege_list: privilege { $$ = list_make1($1); }
4289 | privilege_list ',' privilege { $$ = lappend($1, $3); }
4292 privilege: SELECT opt_column_list
4294 AccessPriv *n = makeNode(AccessPriv);
4295 n->priv_name = pstrdup($1);
4299 | REFERENCES opt_column_list
4301 AccessPriv *n = makeNode(AccessPriv);
4302 n->priv_name = pstrdup($1);
4306 | CREATE opt_column_list
4308 AccessPriv *n = makeNode(AccessPriv);
4309 n->priv_name = pstrdup($1);
4313 | ColId opt_column_list
4315 AccessPriv *n = makeNode(AccessPriv);
4323 /* Don't bother trying to fold the first two rules into one using
4324 * opt_table. You're going to get conflicts.
4329 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4330 n->objtype = ACL_OBJECT_RELATION;
4334 | TABLE qualified_name_list
4336 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4337 n->objtype = ACL_OBJECT_RELATION;
4341 | SEQUENCE qualified_name_list
4343 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4344 n->objtype = ACL_OBJECT_SEQUENCE;
4348 | FOREIGN DATA_P WRAPPER name_list
4350 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4351 n->objtype = ACL_OBJECT_FDW;
4355 | FOREIGN SERVER name_list
4357 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4358 n->objtype = ACL_OBJECT_FOREIGN_SERVER;
4362 | FUNCTION function_with_argtypes_list
4364 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4365 n->objtype = ACL_OBJECT_FUNCTION;
4369 | DATABASE name_list
4371 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4372 n->objtype = ACL_OBJECT_DATABASE;
4376 | LANGUAGE name_list
4378 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4379 n->objtype = ACL_OBJECT_LANGUAGE;
4385 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4386 n->objtype = ACL_OBJECT_NAMESPACE;
4390 | TABLESPACE name_list
4392 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4393 n->objtype = ACL_OBJECT_TABLESPACE;
4401 grantee { $$ = list_make1($1); }
4402 | grantee_list ',' grantee { $$ = lappend($1, $3); }
4407 PrivGrantee *n = makeNode(PrivGrantee);
4408 /* This hack lets us avoid reserving PUBLIC as a keyword*/
4409 if (strcmp($1, "public") == 0)
4417 PrivGrantee *n = makeNode(PrivGrantee);
4418 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
4419 if (strcmp($2, "public") == 0)
4428 opt_grant_grant_option:
4429 WITH GRANT OPTION { $$ = TRUE; }
4430 | /*EMPTY*/ { $$ = FALSE; }
4433 function_with_argtypes_list:
4434 function_with_argtypes { $$ = list_make1($1); }
4435 | function_with_argtypes_list ',' function_with_argtypes
4436 { $$ = lappend($1, $3); }
4439 function_with_argtypes:
4442 FuncWithArgs *n = makeNode(FuncWithArgs);
4444 n->funcargs = extractArgTypes($2);
4449 /*****************************************************************************
4451 * GRANT and REVOKE ROLE statements
4453 *****************************************************************************/
4456 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4458 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4460 n->granted_roles = $2;
4461 n->grantee_roles = $4;
4469 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4471 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4472 n->is_grant = false;
4473 n->admin_opt = false;
4474 n->granted_roles = $2;
4475 n->grantee_roles = $4;
4479 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4481 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4482 n->is_grant = false;
4483 n->admin_opt = true;
4484 n->granted_roles = $5;
4485 n->grantee_roles = $7;
4491 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4492 | /*EMPTY*/ { $$ = FALSE; }
4495 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4496 | /*EMPTY*/ { $$ = NULL; }
4500 /*****************************************************************************
4502 * QUERY: CREATE INDEX
4504 * Note: we can't factor CONCURRENTLY into a separate production without
4505 * making it a reserved word.
4507 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4508 * willing to make TABLESPACE a fully reserved word.
4509 *****************************************************************************/
4511 IndexStmt: CREATE index_opt_unique INDEX index_name
4512 ON qualified_name access_method_clause '(' index_params ')'
4513 opt_reloptions OptTableSpace where_clause
4515 IndexStmt *n = makeNode(IndexStmt);
4517 n->concurrent = false;
4520 n->accessMethod = $7;
4521 n->indexParams = $9;
4523 n->tableSpace = $12;
4524 n->whereClause = $13;
4527 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4528 ON qualified_name access_method_clause '(' index_params ')'
4529 opt_reloptions OptTableSpace where_clause
4531 IndexStmt *n = makeNode(IndexStmt);
4533 n->concurrent = true;
4536 n->accessMethod = $8;
4537 n->indexParams = $10;
4539 n->tableSpace = $13;
4540 n->whereClause = $14;
4546 UNIQUE { $$ = TRUE; }
4547 | /*EMPTY*/ { $$ = FALSE; }
4550 access_method_clause:
4551 USING access_method { $$ = $2; }
4552 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4555 index_params: index_elem { $$ = list_make1($1); }
4556 | index_params ',' index_elem { $$ = lappend($1, $3); }
4560 * Index attributes can be either simple column references, or arbitrary
4561 * expressions in parens. For backwards-compatibility reasons, we allow
4562 * an expression that's just a function call to be written without parens.
4564 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4566 $$ = makeNode(IndexElem);
4571 $$->nulls_ordering = $4;
4573 | func_expr opt_class opt_asc_desc opt_nulls_order
4575 $$ = makeNode(IndexElem);
4580 $$->nulls_ordering = $4;
4582 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4584 $$ = makeNode(IndexElem);
4589 $$->nulls_ordering = $6;
4593 opt_class: any_name { $$ = $1; }
4594 | USING any_name { $$ = $2; }
4595 | /*EMPTY*/ { $$ = NIL; }
4598 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4599 | DESC { $$ = SORTBY_DESC; }
4600 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4603 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4604 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4605 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4609 /*****************************************************************************
4612 * create [or replace] function <fname>
4613 * [(<type-1> { , <type-n>})]
4615 * as <filename or code in language as appropriate>
4616 * language <lang> [with parameters]
4618 *****************************************************************************/
4621 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4622 RETURNS func_return createfunc_opt_list opt_definition
4624 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4633 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4634 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4636 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4639 n->parameters = mergeTableFuncParameters($5, $9);
4640 n->returnType = TableFuncTypeName($9);
4641 n->returnType->location = @7;
4643 n->withClause = $12;
4646 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4647 createfunc_opt_list opt_definition
4649 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4653 n->returnType = NULL;
4661 OR REPLACE { $$ = TRUE; }
4662 | /*EMPTY*/ { $$ = FALSE; }
4665 func_args: '(' func_args_list ')' { $$ = $2; }
4666 | '(' ')' { $$ = NIL; }
4670 func_arg { $$ = list_make1($1); }
4671 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4675 * func_args_with_defaults is separate because we only want to accept
4676 * defaults in CREATE FUNCTION, not in ALTER etc.
4678 func_args_with_defaults:
4679 '(' func_args_with_defaults_list ')' { $$ = $2; }
4680 | '(' ')' { $$ = NIL; }
4683 func_args_with_defaults_list:
4684 func_arg_with_default { $$ = list_make1($1); }
4685 | func_args_with_defaults_list ',' func_arg_with_default
4686 { $$ = lappend($1, $3); }
4690 * The style with arg_class first is SQL99 standard, but Oracle puts
4691 * param_name first; accept both since it's likely people will try both
4692 * anyway. Don't bother trying to save productions by letting arg_class
4693 * have an empty alternative ... you'll get shift/reduce conflicts.
4695 * We can catch over-specified arguments here if we want to,
4696 * but for now better to silently swallow typmod, etc.
4697 * - thomas 2000-03-22
4700 arg_class param_name func_type
4702 FunctionParameter *n = makeNode(FunctionParameter);
4709 | param_name arg_class func_type
4711 FunctionParameter *n = makeNode(FunctionParameter);
4718 | param_name func_type
4720 FunctionParameter *n = makeNode(FunctionParameter);
4723 n->mode = FUNC_PARAM_IN;
4727 | arg_class func_type
4729 FunctionParameter *n = makeNode(FunctionParameter);
4738 FunctionParameter *n = makeNode(FunctionParameter);
4741 n->mode = FUNC_PARAM_IN;
4747 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4748 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4749 | OUT_P { $$ = FUNC_PARAM_OUT; }
4750 | INOUT { $$ = FUNC_PARAM_INOUT; }
4751 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4752 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4756 * Ideally param_name should be ColId, but that causes too many conflicts.
4758 param_name: type_function_name
4764 /* We can catch over-specified results here if we want to,
4765 * but for now better to silently swallow typmod, etc.
4766 * - thomas 2000-03-22
4773 * We would like to make the %TYPE productions here be ColId attrs etc,
4774 * but that causes reduce/reduce conflicts. type_function_name
4775 * is next best choice.
4777 func_type: Typename { $$ = $1; }
4778 | type_function_name attrs '%' TYPE_P
4780 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4781 $$->pct_type = true;
4784 | SETOF type_function_name attrs '%' TYPE_P
4786 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4787 $$->pct_type = true;
4793 func_arg_with_default:
4798 | func_arg DEFAULT a_expr
4803 | func_arg '=' a_expr
4811 createfunc_opt_list:
4812 /* Must be at least one to prevent conflict */
4813 createfunc_opt_item { $$ = list_make1($1); }
4814 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4818 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4820 common_func_opt_item:
4821 CALLED ON NULL_P INPUT_P
4823 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4825 | RETURNS NULL_P ON NULL_P INPUT_P
4827 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4831 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4835 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4839 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4843 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4845 | EXTERNAL SECURITY DEFINER
4847 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4849 | EXTERNAL SECURITY INVOKER
4851 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4855 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4859 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4863 $$ = makeDefElem("cost", (Node *)$2);
4867 $$ = makeDefElem("rows", (Node *)$2);
4871 /* we abuse the normal content of a DefElem here */
4872 $$ = makeDefElem("set", (Node *)$1);
4876 createfunc_opt_item:
4879 $$ = makeDefElem("as", (Node *)$2);
4881 | LANGUAGE ColId_or_Sconst
4883 $$ = makeDefElem("language", (Node *)makeString($2));
4887 $$ = makeDefElem("window", (Node *)makeInteger(TRUE));
4889 | common_func_opt_item
4895 func_as: Sconst { $$ = list_make1(makeString($1)); }
4898 $$ = list_make2(makeString($1), makeString($3));
4903 WITH definition { $$ = $2; }
4904 | /*EMPTY*/ { $$ = NIL; }
4907 table_func_column: param_name func_type
4909 FunctionParameter *n = makeNode(FunctionParameter);
4912 n->mode = FUNC_PARAM_TABLE;
4918 table_func_column_list:
4921 $$ = list_make1($1);
4923 | table_func_column_list ',' table_func_column
4925 $$ = lappend($1, $3);
4929 /*****************************************************************************
4932 * RENAME and OWNER subcommands are already provided by the generic
4933 * ALTER infrastructure, here we just specify alterations that can
4934 * only be applied to functions.
4936 *****************************************************************************/
4938 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4940 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4948 /* At least one option must be specified */
4949 common_func_opt_item { $$ = list_make1($1); }
4950 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4953 /* Ignored, merely for SQL compliance */
4960 /*****************************************************************************
4964 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4965 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4966 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4968 *****************************************************************************/
4971 DROP FUNCTION func_name func_args opt_drop_behavior
4973 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4974 n->kind = OBJECT_FUNCTION;
4976 n->args = extractArgTypes($4);
4978 n->missing_ok = false;
4981 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4983 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4984 n->kind = OBJECT_FUNCTION;
4986 n->args = extractArgTypes($6);
4988 n->missing_ok = true;
4994 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4996 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4997 n->kind = OBJECT_AGGREGATE;
5001 n->missing_ok = false;
5004 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
5006 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5007 n->kind = OBJECT_AGGREGATE;
5011 n->missing_ok = true;
5017 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
5019 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5020 n->kind = OBJECT_OPERATOR;
5024 n->missing_ok = false;
5027 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
5029 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5030 n->kind = OBJECT_OPERATOR;
5034 n->missing_ok = true;
5043 (errcode(ERRCODE_SYNTAX_ERROR),
5044 errmsg("missing argument"),
5045 errhint("Use NONE to denote the missing argument of a unary operator."),
5046 scanner_errposition(@3)));
5048 | '(' Typename ',' Typename ')'
5049 { $$ = list_make2($2, $4); }
5050 | '(' NONE ',' Typename ')' /* left unary */
5051 { $$ = list_make2(NULL, $4); }
5052 | '(' Typename ',' NONE ')' /* right unary */
5053 { $$ = list_make2($2, NULL); }
5058 { $$ = list_make1(makeString($1)); }
5059 | ColId '.' any_operator
5060 { $$ = lcons(makeString($1), $3); }
5064 /*****************************************************************************
5066 * CREATE CAST / DROP CAST
5068 *****************************************************************************/
5070 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
5071 WITH FUNCTION function_with_argtypes cast_context
5073 CreateCastStmt *n = makeNode(CreateCastStmt);
5077 n->context = (CoercionContext) $11;
5081 | CREATE CAST '(' Typename AS Typename ')'
5082 WITHOUT FUNCTION cast_context
5084 CreateCastStmt *n = makeNode(CreateCastStmt);
5088 n->context = (CoercionContext) $10;
5092 | CREATE CAST '(' Typename AS Typename ')'
5093 WITH INOUT cast_context
5095 CreateCastStmt *n = makeNode(CreateCastStmt);
5099 n->context = (CoercionContext) $10;
5105 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
5106 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
5107 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
5111 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
5113 DropCastStmt *n = makeNode(DropCastStmt);
5122 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
5123 | /*EMPTY*/ { $$ = FALSE; }
5127 /*****************************************************************************
5131 * REINDEX type <name> [FORCE]
5133 * FORCE no longer does anything, but we accept it for backwards compatibility
5134 *****************************************************************************/
5137 REINDEX reindex_type qualified_name opt_force
5139 ReindexStmt *n = makeNode(ReindexStmt);
5145 | REINDEX SYSTEM_P name opt_force
5147 ReindexStmt *n = makeNode(ReindexStmt);
5148 n->kind = OBJECT_DATABASE;
5151 n->do_system = true;
5155 | REINDEX DATABASE name opt_force
5157 ReindexStmt *n = makeNode(ReindexStmt);
5158 n->kind = OBJECT_DATABASE;
5161 n->do_system = true;
5168 INDEX { $$ = OBJECT_INDEX; }
5169 | TABLE { $$ = OBJECT_TABLE; }
5172 opt_force: FORCE { $$ = TRUE; }
5173 | /* EMPTY */ { $$ = FALSE; }
5177 /*****************************************************************************
5179 * ALTER THING name RENAME TO newname
5181 *****************************************************************************/
5183 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
5185 RenameStmt *n = makeNode(RenameStmt);
5186 n->renameType = OBJECT_AGGREGATE;
5192 | ALTER CONVERSION_P any_name RENAME TO name
5194 RenameStmt *n = makeNode(RenameStmt);
5195 n->renameType = OBJECT_CONVERSION;
5200 | ALTER DATABASE database_name RENAME TO database_name
5202 RenameStmt *n = makeNode(RenameStmt);
5203 n->renameType = OBJECT_DATABASE;
5208 | ALTER FUNCTION function_with_argtypes RENAME TO name
5210 RenameStmt *n = makeNode(RenameStmt);
5211 n->renameType = OBJECT_FUNCTION;
5212 n->object = $3->funcname;
5213 n->objarg = $3->funcargs;
5217 | ALTER GROUP_P RoleId RENAME TO RoleId
5219 RenameStmt *n = makeNode(RenameStmt);
5220 n->renameType = OBJECT_ROLE;
5225 | ALTER opt_procedural LANGUAGE name RENAME TO name
5227 RenameStmt *n = makeNode(RenameStmt);
5228 n->renameType = OBJECT_LANGUAGE;
5233 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
5235 RenameStmt *n = makeNode(RenameStmt);
5236 n->renameType = OBJECT_OPCLASS;
5242 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
5244 RenameStmt *n = makeNode(RenameStmt);
5245 n->renameType = OBJECT_OPFAMILY;
5251 | ALTER SCHEMA name RENAME TO name
5253 RenameStmt *n = makeNode(RenameStmt);
5254 n->renameType = OBJECT_SCHEMA;
5259 | ALTER TABLE relation_expr RENAME TO name
5261 RenameStmt *n = makeNode(RenameStmt);
5262 n->renameType = OBJECT_TABLE;
5268 | ALTER SEQUENCE qualified_name RENAME TO name
5270 RenameStmt *n = makeNode(RenameStmt);
5271 n->renameType = OBJECT_SEQUENCE;
5277 | ALTER VIEW qualified_name RENAME TO name
5279 RenameStmt *n = makeNode(RenameStmt);
5280 n->renameType = OBJECT_VIEW;
5286 | ALTER INDEX qualified_name RENAME TO name
5288 RenameStmt *n = makeNode(RenameStmt);
5289 n->renameType = OBJECT_INDEX;
5295 | ALTER TABLE relation_expr RENAME opt_column name TO name
5297 RenameStmt *n = makeNode(RenameStmt);
5298 n->renameType = OBJECT_COLUMN;
5304 | ALTER TRIGGER name ON qualified_name RENAME TO name
5306 RenameStmt *n = makeNode(RenameStmt);
5307 n->renameType = OBJECT_TRIGGER;
5313 | ALTER ROLE RoleId RENAME TO RoleId
5315 RenameStmt *n = makeNode(RenameStmt);
5316 n->renameType = OBJECT_ROLE;
5321 | ALTER USER RoleId RENAME TO RoleId
5323 RenameStmt *n = makeNode(RenameStmt);
5324 n->renameType = OBJECT_ROLE;
5329 | ALTER TABLESPACE name RENAME TO name
5331 RenameStmt *n = makeNode(RenameStmt);
5332 n->renameType = OBJECT_TABLESPACE;
5337 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
5339 RenameStmt *n = makeNode(RenameStmt);
5340 n->renameType = OBJECT_TSPARSER;
5345 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
5347 RenameStmt *n = makeNode(RenameStmt);
5348 n->renameType = OBJECT_TSDICTIONARY;
5353 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
5355 RenameStmt *n = makeNode(RenameStmt);
5356 n->renameType = OBJECT_TSTEMPLATE;
5361 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
5363 RenameStmt *n = makeNode(RenameStmt);
5364 n->renameType = OBJECT_TSCONFIGURATION;
5369 | ALTER TYPE_P any_name RENAME TO name
5371 RenameStmt *n = makeNode(RenameStmt);
5372 n->renameType = OBJECT_TYPE;
5379 opt_column: COLUMN { $$ = COLUMN; }
5380 | /*EMPTY*/ { $$ = 0; }
5383 opt_set_data: SET DATA_P { $$ = 1; }
5384 | /*EMPTY*/ { $$ = 0; }
5387 /*****************************************************************************
5389 * ALTER THING name SET SCHEMA name
5391 *****************************************************************************/
5393 AlterObjectSchemaStmt:
5394 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
5396 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5397 n->objectType = OBJECT_AGGREGATE;
5403 | ALTER DOMAIN_P any_name SET SCHEMA name
5405 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5406 n->objectType = OBJECT_DOMAIN;
5411 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
5413 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5414 n->objectType = OBJECT_FUNCTION;
5415 n->object = $3->funcname;
5416 n->objarg = $3->funcargs;
5420 | ALTER TABLE relation_expr SET SCHEMA name
5422 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5423 n->objectType = OBJECT_TABLE;
5428 | ALTER SEQUENCE qualified_name SET SCHEMA name
5430 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5431 n->objectType = OBJECT_SEQUENCE;
5436 | ALTER VIEW qualified_name SET SCHEMA name
5438 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5439 n->objectType = OBJECT_VIEW;
5444 | ALTER TYPE_P any_name SET SCHEMA name
5446 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5447 n->objectType = OBJECT_TYPE;
5454 /*****************************************************************************
5456 * ALTER THING name OWNER TO newname
5458 *****************************************************************************/
5460 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5462 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5463 n->objectType = OBJECT_AGGREGATE;
5469 | ALTER CONVERSION_P any_name OWNER TO RoleId
5471 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5472 n->objectType = OBJECT_CONVERSION;
5477 | ALTER DATABASE database_name OWNER TO RoleId
5479 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5480 n->objectType = OBJECT_DATABASE;
5481 n->object = list_make1(makeString($3));
5485 | ALTER DOMAIN_P any_name OWNER TO RoleId
5487 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5488 n->objectType = OBJECT_DOMAIN;
5493 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5495 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5496 n->objectType = OBJECT_FUNCTION;
5497 n->object = $3->funcname;
5498 n->objarg = $3->funcargs;
5502 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5504 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5505 n->objectType = OBJECT_LANGUAGE;
5506 n->object = list_make1(makeString($4));
5510 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5512 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5513 n->objectType = OBJECT_OPERATOR;
5519 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5521 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5522 n->objectType = OBJECT_OPCLASS;
5528 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5530 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5531 n->objectType = OBJECT_OPFAMILY;
5537 | ALTER SCHEMA name OWNER TO RoleId
5539 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5540 n->objectType = OBJECT_SCHEMA;
5541 n->object = list_make1(makeString($3));
5545 | ALTER TYPE_P any_name OWNER TO RoleId
5547 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5548 n->objectType = OBJECT_TYPE;
5553 | ALTER TABLESPACE name OWNER TO RoleId
5555 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5556 n->objectType = OBJECT_TABLESPACE;
5557 n->object = list_make1(makeString($3));
5561 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5563 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5564 n->objectType = OBJECT_TSDICTIONARY;
5569 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5571 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5572 n->objectType = OBJECT_TSCONFIGURATION;
5577 | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
5579 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5580 n->objectType = OBJECT_FDW;
5581 n->object = list_make1(makeString($5));
5585 | ALTER SERVER name OWNER TO RoleId
5587 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5588 n->objectType = OBJECT_FOREIGN_SERVER;
5589 n->object = list_make1(makeString($3));
5596 /*****************************************************************************
5598 * QUERY: Define Rewrite Rule
5600 *****************************************************************************/
5602 RuleStmt: CREATE opt_or_replace RULE name AS
5603 { QueryIsRule=TRUE; }
5604 ON event TO qualified_name where_clause
5605 DO opt_instead RuleActionList
5607 RuleStmt *n = makeNode(RuleStmt);
5611 n->whereClause = $11;
5621 NOTHING { $$ = NIL; }
5622 | RuleActionStmt { $$ = list_make1($1); }
5623 | '(' RuleActionMulti ')' { $$ = $2; }
5626 /* the thrashing around here is to discard "empty" statements... */
5628 RuleActionMulti ';' RuleActionStmtOrEmpty
5630 $$ = lappend($1, $3);
5634 | RuleActionStmtOrEmpty
5636 $$ = list_make1($1);
5650 RuleActionStmtOrEmpty:
5651 RuleActionStmt { $$ = $1; }
5652 | /*EMPTY*/ { $$ = NULL; }
5655 event: SELECT { $$ = CMD_SELECT; }
5656 | UPDATE { $$ = CMD_UPDATE; }
5657 | DELETE_P { $$ = CMD_DELETE; }
5658 | INSERT { $$ = CMD_INSERT; }
5662 INSTEAD { $$ = TRUE; }
5663 | ALSO { $$ = FALSE; }
5664 | /*EMPTY*/ { $$ = FALSE; }
5669 DROP RULE name ON qualified_name opt_drop_behavior
5671 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5675 n->removeType = OBJECT_RULE;
5676 n->missing_ok = false;
5679 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5681 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5685 n->removeType = OBJECT_RULE;
5686 n->missing_ok = true;
5692 /*****************************************************************************
5695 * NOTIFY <identifier> can appear both in rule bodies and
5696 * as a query-level command
5698 *****************************************************************************/
5700 NotifyStmt: NOTIFY ColId
5702 NotifyStmt *n = makeNode(NotifyStmt);
5703 n->conditionname = $2;
5708 ListenStmt: LISTEN ColId
5710 ListenStmt *n = makeNode(ListenStmt);
5711 n->conditionname = $2;
5719 UnlistenStmt *n = makeNode(UnlistenStmt);
5720 n->conditionname = $2;
5725 UnlistenStmt *n = makeNode(UnlistenStmt);
5726 n->conditionname = NULL;
5732 /*****************************************************************************
5736 * BEGIN / COMMIT / ROLLBACK
5737 * (also older versions END / ABORT)
5739 *****************************************************************************/
5742 ABORT_P opt_transaction
5744 TransactionStmt *n = makeNode(TransactionStmt);
5745 n->kind = TRANS_STMT_ROLLBACK;
5749 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5751 TransactionStmt *n = makeNode(TransactionStmt);
5752 n->kind = TRANS_STMT_BEGIN;
5756 | START TRANSACTION transaction_mode_list_or_empty
5758 TransactionStmt *n = makeNode(TransactionStmt);
5759 n->kind = TRANS_STMT_START;
5763 | COMMIT opt_transaction
5765 TransactionStmt *n = makeNode(TransactionStmt);
5766 n->kind = TRANS_STMT_COMMIT;
5770 | END_P opt_transaction
5772 TransactionStmt *n = makeNode(TransactionStmt);
5773 n->kind = TRANS_STMT_COMMIT;
5777 | ROLLBACK opt_transaction
5779 TransactionStmt *n = makeNode(TransactionStmt);
5780 n->kind = TRANS_STMT_ROLLBACK;
5786 TransactionStmt *n = makeNode(TransactionStmt);
5787 n->kind = TRANS_STMT_SAVEPOINT;
5788 n->options = list_make1(makeDefElem("savepoint_name",
5789 (Node *)makeString($2)));
5792 | RELEASE SAVEPOINT ColId
5794 TransactionStmt *n = makeNode(TransactionStmt);
5795 n->kind = TRANS_STMT_RELEASE;
5796 n->options = list_make1(makeDefElem("savepoint_name",
5797 (Node *)makeString($3)));
5802 TransactionStmt *n = makeNode(TransactionStmt);
5803 n->kind = TRANS_STMT_RELEASE;
5804 n->options = list_make1(makeDefElem("savepoint_name",
5805 (Node *)makeString($2)));
5808 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5810 TransactionStmt *n = makeNode(TransactionStmt);
5811 n->kind = TRANS_STMT_ROLLBACK_TO;
5812 n->options = list_make1(makeDefElem("savepoint_name",
5813 (Node *)makeString($5)));
5816 | ROLLBACK opt_transaction TO ColId
5818 TransactionStmt *n = makeNode(TransactionStmt);
5819 n->kind = TRANS_STMT_ROLLBACK_TO;
5820 n->options = list_make1(makeDefElem("savepoint_name",
5821 (Node *)makeString($4)));
5824 | PREPARE TRANSACTION Sconst
5826 TransactionStmt *n = makeNode(TransactionStmt);
5827 n->kind = TRANS_STMT_PREPARE;
5831 | COMMIT PREPARED Sconst
5833 TransactionStmt *n = makeNode(TransactionStmt);
5834 n->kind = TRANS_STMT_COMMIT_PREPARED;
5838 | ROLLBACK PREPARED Sconst
5840 TransactionStmt *n = makeNode(TransactionStmt);
5841 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5847 opt_transaction: WORK {}
5852 transaction_mode_item:
5853 ISOLATION LEVEL iso_level
5854 { $$ = makeDefElem("transaction_isolation",
5855 makeStringConst($3, @3)); }
5857 { $$ = makeDefElem("transaction_read_only",
5858 makeIntConst(TRUE, @1)); }
5860 { $$ = makeDefElem("transaction_read_only",
5861 makeIntConst(FALSE, @1)); }
5864 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5865 transaction_mode_list:
5866 transaction_mode_item
5867 { $$ = list_make1($1); }
5868 | transaction_mode_list ',' transaction_mode_item
5869 { $$ = lappend($1, $3); }
5870 | transaction_mode_list transaction_mode_item
5871 { $$ = lappend($1, $2); }
5874 transaction_mode_list_or_empty:
5875 transaction_mode_list
5881 /*****************************************************************************
5884 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5885 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5887 *****************************************************************************/
5889 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5890 AS SelectStmt opt_check_option
5892 ViewStmt *n = makeNode(ViewStmt);
5894 n->view->istemp = $2;
5900 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5901 AS SelectStmt opt_check_option
5903 ViewStmt *n = makeNode(ViewStmt);
5905 n->view->istemp = $4;
5917 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5918 errmsg("WITH CHECK OPTION is not implemented")));
5920 | WITH CASCADED CHECK OPTION
5923 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5924 errmsg("WITH CHECK OPTION is not implemented")));
5926 | WITH LOCAL CHECK OPTION
5929 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5930 errmsg("WITH CHECK OPTION is not implemented")));
5932 | /* EMPTY */ { $$ = NIL; }
5935 /*****************************************************************************
5940 *****************************************************************************/
5942 LoadStmt: LOAD file_name
5944 LoadStmt *n = makeNode(LoadStmt);
5951 /*****************************************************************************
5955 *****************************************************************************/
5958 CREATE DATABASE database_name opt_with createdb_opt_list
5960 CreatedbStmt *n = makeNode(CreatedbStmt);
5968 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5969 | /* EMPTY */ { $$ = NIL; }
5973 TABLESPACE opt_equal name
5975 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5977 | TABLESPACE opt_equal DEFAULT
5979 $$ = makeDefElem("tablespace", NULL);
5981 | LOCATION opt_equal Sconst
5983 $$ = makeDefElem("location", (Node *)makeString($3));
5985 | LOCATION opt_equal DEFAULT
5987 $$ = makeDefElem("location", NULL);
5989 | TEMPLATE opt_equal name
5991 $$ = makeDefElem("template", (Node *)makeString($3));
5993 | TEMPLATE opt_equal DEFAULT
5995 $$ = makeDefElem("template", NULL);
5997 | ENCODING opt_equal Sconst
5999 $$ = makeDefElem("encoding", (Node *)makeString($3));
6001 | ENCODING opt_equal Iconst
6003 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
6005 | ENCODING opt_equal DEFAULT
6007 $$ = makeDefElem("encoding", NULL);
6009 | LC_COLLATE_P opt_equal Sconst
6011 $$ = makeDefElem("lc_collate", (Node *)makeString($3));
6013 | LC_COLLATE_P opt_equal DEFAULT
6015 $$ = makeDefElem("lc_collate", NULL);
6017 | LC_CTYPE_P opt_equal Sconst
6019 $$ = makeDefElem("lc_ctype", (Node *)makeString($3));
6021 | LC_CTYPE_P opt_equal DEFAULT
6023 $$ = makeDefElem("lc_ctype", NULL);
6025 | CONNECTION LIMIT opt_equal SignedIconst
6027 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6029 | OWNER opt_equal name
6031 $$ = makeDefElem("owner", (Node *)makeString($3));
6033 | OWNER opt_equal DEFAULT
6035 $$ = makeDefElem("owner", NULL);
6040 * Though the equals sign doesn't match other WITH options, pg_dump uses
6041 * equals for backward compatibility, and it doesn't seem worth removing it.
6048 /*****************************************************************************
6052 *****************************************************************************/
6055 ALTER DATABASE database_name opt_with alterdb_opt_list
6057 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6062 | ALTER DATABASE database_name SET TABLESPACE name
6064 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6066 n->options = list_make1(makeDefElem("tablespace",
6067 (Node *)makeString($6)));
6072 AlterDatabaseSetStmt:
6073 ALTER DATABASE database_name SetResetClause
6075 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
6084 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
6085 | /* EMPTY */ { $$ = NIL; }
6089 CONNECTION LIMIT opt_equal SignedIconst
6091 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6096 /*****************************************************************************
6098 * DROP DATABASE [ IF EXISTS ]
6100 * This is implicitly CASCADE, no need for drop behavior
6101 *****************************************************************************/
6103 DropdbStmt: DROP DATABASE database_name
6105 DropdbStmt *n = makeNode(DropdbStmt);
6107 n->missing_ok = FALSE;
6110 | DROP DATABASE IF_P EXISTS database_name
6112 DropdbStmt *n = makeNode(DropdbStmt);
6114 n->missing_ok = TRUE;
6120 /*****************************************************************************
6122 * Manipulate a domain
6124 *****************************************************************************/
6127 CREATE DOMAIN_P any_name opt_as Typename ColQualList
6129 CreateDomainStmt *n = makeNode(CreateDomainStmt);
6132 n->constraints = $6;
6138 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
6139 ALTER DOMAIN_P any_name alter_column_default
6141 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6147 /* ALTER DOMAIN <domain> DROP NOT NULL */
6148 | ALTER DOMAIN_P any_name DROP NOT NULL_P
6150 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6155 /* ALTER DOMAIN <domain> SET NOT NULL */
6156 | ALTER DOMAIN_P any_name SET NOT NULL_P
6158 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6163 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
6164 | ALTER DOMAIN_P any_name ADD_P TableConstraint
6166 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6172 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
6173 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
6175 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6189 /*****************************************************************************
6191 * Manipulate a text search dictionary or configuration
6193 *****************************************************************************/
6195 AlterTSDictionaryStmt:
6196 ALTER TEXT_P SEARCH DICTIONARY any_name definition
6198 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
6205 AlterTSConfigurationStmt:
6206 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
6208 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6212 n->override = false;
6216 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
6218 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6226 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
6228 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6231 n->dicts = list_make2($9,$11);
6232 n->override = false;
6236 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
6238 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6241 n->dicts = list_make2($11,$13);
6242 n->override = false;
6246 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
6248 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6251 n->missing_ok = false;
6254 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
6256 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6259 n->missing_ok = true;
6265 /*****************************************************************************
6267 * Manipulate a conversion
6269 * CREATE [DEFAULT] CONVERSION <conversion_name>
6270 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
6272 *****************************************************************************/
6274 CreateConversionStmt:
6275 CREATE opt_default CONVERSION_P any_name FOR Sconst
6276 TO Sconst FROM any_name
6278 CreateConversionStmt *n = makeNode(CreateConversionStmt);
6279 n->conversion_name = $4;
6280 n->for_encoding_name = $6;
6281 n->to_encoding_name = $8;
6288 /*****************************************************************************
6291 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
6293 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
6295 *****************************************************************************/
6298 CLUSTER opt_verbose qualified_name cluster_index_specification
6300 ClusterStmt *n = makeNode(ClusterStmt);
6306 | CLUSTER opt_verbose
6308 ClusterStmt *n = makeNode(ClusterStmt);
6310 n->indexname = NULL;
6314 /* kept for pre-8.3 compatibility */
6315 | CLUSTER opt_verbose index_name ON qualified_name
6317 ClusterStmt *n = makeNode(ClusterStmt);
6325 cluster_index_specification:
6326 USING index_name { $$ = $2; }
6327 | /*EMPTY*/ { $$ = NULL; }
6331 /*****************************************************************************
6337 *****************************************************************************/
6339 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
6341 VacuumStmt *n = makeNode(VacuumStmt);
6345 n->freeze_min_age = $3 ? 0 : -1;
6346 n->freeze_table_age = $3 ? 0 : -1;
6352 | VACUUM opt_full opt_freeze opt_verbose qualified_name
6354 VacuumStmt *n = makeNode(VacuumStmt);
6358 n->freeze_min_age = $3 ? 0 : -1;
6359 n->freeze_table_age = $3 ? 0 : -1;
6365 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
6367 VacuumStmt *n = (VacuumStmt *) $5;
6370 n->freeze_min_age = $3 ? 0 : -1;
6371 n->freeze_table_age = $3 ? 0 : -1;
6378 analyze_keyword opt_verbose
6380 VacuumStmt *n = makeNode(VacuumStmt);
6384 n->freeze_min_age = -1;
6385 n->freeze_table_age = -1;
6391 | analyze_keyword opt_verbose qualified_name opt_name_list
6393 VacuumStmt *n = makeNode(VacuumStmt);
6397 n->freeze_min_age = -1;
6398 n->freeze_table_age = -1;
6408 | ANALYSE /* British */ {}
6412 VERBOSE { $$ = TRUE; }
6413 | /*EMPTY*/ { $$ = FALSE; }
6416 opt_full: FULL { $$ = TRUE; }
6417 | /*EMPTY*/ { $$ = FALSE; }
6420 opt_freeze: FREEZE { $$ = TRUE; }
6421 | /*EMPTY*/ { $$ = FALSE; }
6425 '(' name_list ')' { $$ = $2; }
6426 | /*EMPTY*/ { $$ = NIL; }
6430 /*****************************************************************************
6433 * EXPLAIN [ANALYZE] [VERBOSE] query
6435 *****************************************************************************/
6437 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
6439 ExplainStmt *n = makeNode(ExplainStmt);
6454 | ExecuteStmt /* by default all are $$=$1 */
6458 analyze_keyword { $$ = TRUE; }
6459 | /* EMPTY */ { $$ = FALSE; }
6462 /*****************************************************************************
6465 * PREPARE <plan_name> [(args, ...)] AS <query>
6467 *****************************************************************************/
6469 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
6471 PrepareStmt *n = makeNode(PrepareStmt);
6479 prep_type_clause: '(' type_list ')' { $$ = $2; }
6480 | /* EMPTY */ { $$ = NIL; }
6487 | DeleteStmt /* by default all are $$=$1 */
6490 /*****************************************************************************
6492 * EXECUTE <plan_name> [(params, ...)]
6493 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
6495 *****************************************************************************/
6497 ExecuteStmt: EXECUTE name execute_param_clause
6499 ExecuteStmt *n = makeNode(ExecuteStmt);
6505 | CREATE OptTemp TABLE create_as_target AS
6506 EXECUTE name execute_param_clause
6508 ExecuteStmt *n = makeNode(ExecuteStmt);
6511 $4->rel->istemp = $2;
6515 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6516 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6517 /* ... because it's not implemented, but it could be */
6522 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6523 | /* EMPTY */ { $$ = NIL; }
6526 /*****************************************************************************
6529 * DEALLOCATE [PREPARE] <plan_name>
6531 *****************************************************************************/
6533 DeallocateStmt: DEALLOCATE name
6535 DeallocateStmt *n = makeNode(DeallocateStmt);
6539 | DEALLOCATE PREPARE name
6541 DeallocateStmt *n = makeNode(DeallocateStmt);
6547 DeallocateStmt *n = makeNode(DeallocateStmt);
6551 | DEALLOCATE PREPARE ALL
6553 DeallocateStmt *n = makeNode(DeallocateStmt);
6559 /*****************************************************************************
6564 *****************************************************************************/
6567 INSERT INTO qualified_name insert_rest returning_clause
6570 $4->returningList = $5;
6578 $$ = makeNode(InsertStmt);
6580 $$->selectStmt = $1;
6582 | '(' insert_column_list ')' SelectStmt
6584 $$ = makeNode(InsertStmt);
6586 $$->selectStmt = $4;
6590 $$ = makeNode(InsertStmt);
6592 $$->selectStmt = NULL;
6598 { $$ = list_make1($1); }
6599 | insert_column_list ',' insert_column_item
6600 { $$ = lappend($1, $3); }
6604 ColId opt_indirection
6606 $$ = makeNode(ResTarget);
6608 $$->indirection = check_indirection($2);
6615 RETURNING target_list { $$ = $2; }
6616 | /* EMPTY */ { $$ = NIL; }
6620 /*****************************************************************************
6625 *****************************************************************************/
6627 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6628 using_clause where_or_current_clause returning_clause
6630 DeleteStmt *n = makeNode(DeleteStmt);
6632 n->usingClause = $4;
6633 n->whereClause = $5;
6634 n->returningList = $6;
6640 USING from_list { $$ = $2; }
6641 | /*EMPTY*/ { $$ = NIL; }
6645 /*****************************************************************************
6650 *****************************************************************************/
6652 LockStmt: LOCK_P opt_table relation_expr_list opt_lock opt_nowait
6654 LockStmt *n = makeNode(LockStmt);
6663 opt_lock: IN_P lock_type MODE { $$ = $2; }
6664 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6667 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6668 | ROW SHARE { $$ = RowShareLock; }
6669 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6670 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6671 | SHARE { $$ = ShareLock; }
6672 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6673 | EXCLUSIVE { $$ = ExclusiveLock; }
6674 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6677 opt_nowait: NOWAIT { $$ = TRUE; }
6678 | /*EMPTY*/ { $$ = FALSE; }
6682 /*****************************************************************************
6685 * UpdateStmt (UPDATE)
6687 *****************************************************************************/
6689 UpdateStmt: UPDATE relation_expr_opt_alias
6692 where_or_current_clause
6695 UpdateStmt *n = makeNode(UpdateStmt);
6699 n->whereClause = $6;
6700 n->returningList = $7;
6706 set_clause { $$ = $1; }
6707 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6711 single_set_clause { $$ = list_make1($1); }
6712 | multiple_set_clause { $$ = $1; }
6716 set_target '=' ctext_expr
6719 $$->val = (Node *) $3;
6723 multiple_set_clause:
6724 '(' set_target_list ')' '=' ctext_row
6730 * Break the ctext_row apart, merge individual expressions
6731 * into the destination ResTargets. XXX this approach
6732 * cannot work for general row expressions as sources.
6734 if (list_length($2) != list_length($5))
6736 (errcode(ERRCODE_SYNTAX_ERROR),
6737 errmsg("number of columns does not match number of values"),
6738 scanner_errposition(@1)));
6739 forboth(col_cell, $2, val_cell, $5)
6741 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6742 Node *res_val = (Node *) lfirst(val_cell);
6744 res_col->val = res_val;
6752 ColId opt_indirection
6754 $$ = makeNode(ResTarget);
6756 $$->indirection = check_indirection($2);
6757 $$->val = NULL; /* upper production sets this */
6763 set_target { $$ = list_make1($1); }
6764 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6768 /*****************************************************************************
6773 *****************************************************************************/
6774 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6776 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6778 /* currently we always set FAST_PLAN option */
6779 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6785 cursor_options: /*EMPTY*/ { $$ = 0; }
6786 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6787 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6788 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6789 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6792 opt_hold: /* EMPTY */ { $$ = 0; }
6793 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6794 | WITHOUT HOLD { $$ = 0; }
6797 /*****************************************************************************
6802 *****************************************************************************/
6804 /* A complete SELECT statement looks like this.
6806 * The rule returns either a single SelectStmt node or a tree of them,
6807 * representing a set-operation tree.
6809 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6810 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6811 * to the surrounding a_expr? We don't really care, but bison wants to know.
6812 * To resolve the ambiguity, we are careful to define the grammar so that
6813 * the decision is staved off as long as possible: as long as we can keep
6814 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6815 * it's no longer possible to do that will we decide that parens belong to
6816 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6817 * parentheses are treated as part of the sub-select. The necessity of doing
6818 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6819 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6820 * SELECT viewpoint when we see the UNION.
6822 * This approach is implemented by defining a nonterminal select_with_parens,
6823 * which represents a SELECT with at least one outer layer of parentheses,
6824 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6825 * in the expression grammar. We will then have shift-reduce conflicts
6826 * which we can resolve in favor of always treating '(' <select> ')' as
6827 * a select_with_parens. To resolve the conflicts, the productions that
6828 * conflict with the select_with_parens productions are manually given
6829 * precedences lower than the precedence of ')', thereby ensuring that we
6830 * shift ')' (and then reduce to select_with_parens) rather than trying to
6831 * reduce the inner <select> nonterminal to something else. We use UMINUS
6832 * precedence for this, which is a fairly arbitrary choice.
6834 * To be able to define select_with_parens itself without ambiguity, we need
6835 * a nonterminal select_no_parens that represents a SELECT structure with no
6836 * outermost parentheses. This is a little bit tedious, but it works.
6838 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6839 * with or without outer parentheses.
6842 SelectStmt: select_no_parens %prec UMINUS
6843 | select_with_parens %prec UMINUS
6847 '(' select_no_parens ')' { $$ = $2; }
6848 | '(' select_with_parens ')' { $$ = $2; }
6852 * This rule parses the equivalent of the standard's <query expression>.
6853 * The duplicative productions are annoying, but hard to get rid of without
6854 * creating shift/reduce conflicts.
6856 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6857 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6858 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6862 simple_select { $$ = $1; }
6863 | select_clause sort_clause
6865 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6869 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6871 insertSelectOptions((SelectStmt *) $1, $2, $3,
6872 list_nth($4, 0), list_nth($4, 1),
6876 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6878 insertSelectOptions((SelectStmt *) $1, $2, $4,
6879 list_nth($3, 0), list_nth($3, 1),
6883 | with_clause select_clause
6885 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6890 | with_clause select_clause sort_clause
6892 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6897 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6899 insertSelectOptions((SelectStmt *) $2, $3, $4,
6900 list_nth($5, 0), list_nth($5, 1),
6904 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6906 insertSelectOptions((SelectStmt *) $2, $3, $5,
6907 list_nth($4, 0), list_nth($4, 1),
6914 simple_select { $$ = $1; }
6915 | select_with_parens { $$ = $1; }
6919 * This rule parses SELECT statements that can appear within set operations,
6920 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6921 * the ordering of the set operations. Without '(' and ')' we want the
6922 * operations to be ordered per the precedence specs at the head of this file.
6924 * As with select_no_parens, simple_select cannot have outer parentheses,
6925 * but can have parenthesized subclauses.
6927 * Note that sort clauses cannot be included at this level --- SQL92 requires
6928 * SELECT foo UNION SELECT bar ORDER BY baz
6930 * (SELECT foo UNION SELECT bar) ORDER BY baz
6932 * SELECT foo UNION (SELECT bar ORDER BY baz)
6933 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6934 * described as part of the select_no_parens production, not simple_select.
6935 * This does not limit functionality, because you can reintroduce these
6936 * clauses inside parentheses.
6938 * NOTE: only the leftmost component SelectStmt should have INTO.
6939 * However, this is not checked by the grammar; parse analysis must check it.
6942 SELECT opt_distinct target_list
6943 into_clause from_clause where_clause
6944 group_clause having_clause window_clause
6946 SelectStmt *n = makeNode(SelectStmt);
6947 n->distinctClause = $2;
6951 n->whereClause = $6;
6952 n->groupClause = $7;
6953 n->havingClause = $8;
6954 n->windowClause = $9;
6957 | values_clause { $$ = $1; }
6958 | TABLE relation_expr
6960 /* same as SELECT * FROM relation_expr */
6961 ColumnRef *cr = makeNode(ColumnRef);
6962 ResTarget *rt = makeNode(ResTarget);
6963 SelectStmt *n = makeNode(SelectStmt);
6965 cr->fields = list_make1(makeNode(A_Star));
6969 rt->indirection = NIL;
6970 rt->val = (Node *)cr;
6973 n->targetList = list_make1(rt);
6974 n->fromClause = list_make1($2);
6977 | select_clause UNION opt_all select_clause
6979 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6981 | select_clause INTERSECT opt_all select_clause
6983 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6985 | select_clause EXCEPT opt_all select_clause
6987 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6992 * SQL standard WITH clause looks like:
6994 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6995 * AS (query) [ SEARCH or CYCLE clause ]
6997 * We don't currently support the SEARCH or CYCLE clause.
7002 $$ = makeNode(WithClause);
7004 $$->recursive = false;
7007 | WITH RECURSIVE cte_list
7009 $$ = makeNode(WithClause);
7011 $$->recursive = true;
7017 common_table_expr { $$ = list_make1($1); }
7018 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
7021 common_table_expr: name opt_name_list AS select_with_parens
7023 CommonTableExpr *n = makeNode(CommonTableExpr);
7025 n->aliascolnames = $2;
7033 INTO OptTempTableName
7035 $$ = makeNode(IntoClause);
7039 $$->onCommit = ONCOMMIT_NOOP;
7040 $$->tableSpaceName = NULL;
7047 * Redundancy here is needed to avoid shift/reduce conflicts,
7048 * since TEMP is not a reserved word. See also OptTemp.
7051 TEMPORARY opt_table qualified_name
7056 | TEMP opt_table qualified_name
7061 | LOCAL TEMPORARY opt_table qualified_name
7066 | LOCAL TEMP opt_table qualified_name
7071 | GLOBAL TEMPORARY opt_table qualified_name
7076 | GLOBAL TEMP opt_table qualified_name
7081 | TABLE qualified_name
7097 opt_all: ALL { $$ = TRUE; }
7098 | DISTINCT { $$ = FALSE; }
7099 | /*EMPTY*/ { $$ = FALSE; }
7102 /* We use (NIL) as a placeholder to indicate that all target expressions
7103 * should be placed in the DISTINCT list during parsetree analysis.
7106 DISTINCT { $$ = list_make1(NIL); }
7107 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
7109 | /*EMPTY*/ { $$ = NIL; }
7113 sort_clause { $$ = $1;}
7114 | /*EMPTY*/ { $$ = NIL; }
7118 ORDER BY sortby_list { $$ = $3; }
7122 sortby { $$ = list_make1($1); }
7123 | sortby_list ',' sortby { $$ = lappend($1, $3); }
7126 sortby: a_expr USING qual_all_Op opt_nulls_order
7128 $$ = makeNode(SortBy);
7130 $$->sortby_dir = SORTBY_USING;
7131 $$->sortby_nulls = $4;
7135 | a_expr opt_asc_desc opt_nulls_order
7137 $$ = makeNode(SortBy);
7139 $$->sortby_dir = $2;
7140 $$->sortby_nulls = $3;
7142 $$->location = -1; /* no operator */
7148 LIMIT select_limit_value OFFSET select_offset_value
7149 { $$ = list_make2($4, $2); }
7150 | OFFSET select_offset_value LIMIT select_limit_value
7151 { $$ = list_make2($2, $4); }
7152 | LIMIT select_limit_value
7153 { $$ = list_make2(NULL, $2); }
7154 | OFFSET select_offset_value
7155 { $$ = list_make2($2, NULL); }
7156 | LIMIT select_limit_value ',' select_offset_value
7158 /* Disabled because it was too confusing, bjm 2002-02-18 */
7160 (errcode(ERRCODE_SYNTAX_ERROR),
7161 errmsg("LIMIT #,# syntax is not supported"),
7162 errhint("Use separate LIMIT and OFFSET clauses."),
7163 scanner_errposition(@1)));
7165 /* SQL:2008 syntax variants */
7166 | OFFSET select_offset_value2 row_or_rows
7167 { $$ = list_make2($2, NULL); }
7168 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7169 { $$ = list_make2(NULL, $3); }
7170 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7171 { $$ = list_make2($2, $6); }
7175 select_limit { $$ = $1; }
7177 { $$ = list_make2(NULL,NULL); }
7184 /* LIMIT ALL is represented as a NULL constant */
7185 $$ = makeNullAConst(@1);
7190 * Allowing full expressions without parentheses causes various parsing
7191 * problems with the trailing ROW/ROWS key words. SQL only calls for
7192 * constants, so we allow the rest only with parentheses.
7194 opt_select_fetch_first_value:
7195 SignedIconst { $$ = makeIntConst($1, @1); }
7196 | '(' a_expr ')' { $$ = $2; }
7197 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
7200 select_offset_value:
7205 * Again, the trailing ROW/ROWS in this case prevent the full expression
7206 * syntax. c_expr is the best we can do.
7208 select_offset_value2:
7225 GROUP_P BY expr_list { $$ = $3; }
7226 | /*EMPTY*/ { $$ = NIL; }
7230 HAVING a_expr { $$ = $2; }
7231 | /*EMPTY*/ { $$ = NULL; }
7235 for_locking_items { $$ = $1; }
7236 | FOR READ ONLY { $$ = NIL; }
7239 opt_for_locking_clause:
7240 for_locking_clause { $$ = $1; }
7241 | /* EMPTY */ { $$ = NIL; }
7245 for_locking_item { $$ = list_make1($1); }
7246 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
7250 FOR UPDATE locked_rels_list opt_nowait
7252 LockingClause *n = makeNode(LockingClause);
7254 n->forUpdate = TRUE;
7258 | FOR SHARE locked_rels_list opt_nowait
7260 LockingClause *n = makeNode(LockingClause);
7262 n->forUpdate = FALSE;
7269 OF qualified_name_list { $$ = $2; }
7270 | /* EMPTY */ { $$ = NIL; }
7277 SelectStmt *n = makeNode(SelectStmt);
7278 n->valuesLists = list_make1($2);
7281 | values_clause ',' ctext_row
7283 SelectStmt *n = (SelectStmt *) $1;
7284 n->valuesLists = lappend(n->valuesLists, $3);
7290 /*****************************************************************************
7292 * clauses common to all Optimizable Stmts:
7293 * from_clause - allow list of both JOIN expressions and table names
7294 * where_clause - qualifications for joins or restrictions
7296 *****************************************************************************/
7299 FROM from_list { $$ = $2; }
7300 | /*EMPTY*/ { $$ = NIL; }
7304 table_ref { $$ = list_make1($1); }
7305 | from_list ',' table_ref { $$ = lappend($1, $3); }
7309 * table_ref is where an alias clause can be attached. Note we cannot make
7310 * alias_clause have an empty production because that causes parse conflicts
7311 * between table_ref := '(' joined_table ')' alias_clause
7312 * and joined_table := '(' joined_table ')'. So, we must have the
7313 * redundant-looking productions here instead.
7315 table_ref: relation_expr
7319 | relation_expr alias_clause
7326 RangeFunction *n = makeNode(RangeFunction);
7327 n->funccallnode = $1;
7328 n->coldeflist = NIL;
7331 | func_table alias_clause
7333 RangeFunction *n = makeNode(RangeFunction);
7334 n->funccallnode = $1;
7336 n->coldeflist = NIL;
7339 | func_table AS '(' TableFuncElementList ')'
7341 RangeFunction *n = makeNode(RangeFunction);
7342 n->funccallnode = $1;
7346 | func_table AS ColId '(' TableFuncElementList ')'
7348 RangeFunction *n = makeNode(RangeFunction);
7349 Alias *a = makeNode(Alias);
7350 n->funccallnode = $1;
7356 | func_table ColId '(' TableFuncElementList ')'
7358 RangeFunction *n = makeNode(RangeFunction);
7359 Alias *a = makeNode(Alias);
7360 n->funccallnode = $1;
7366 | select_with_parens
7369 * The SQL spec does not permit a subselect
7370 * (<derived_table>) without an alias clause,
7371 * so we don't either. This avoids the problem
7372 * of needing to invent a unique refname for it.
7373 * That could be surmounted if there's sufficient
7374 * popular demand, but for now let's just implement
7375 * the spec and see if anyone complains.
7376 * However, it does seem like a good idea to emit
7377 * an error message that's better than "syntax error".
7379 if (IsA($1, SelectStmt) &&
7380 ((SelectStmt *) $1)->valuesLists)
7382 (errcode(ERRCODE_SYNTAX_ERROR),
7383 errmsg("VALUES in FROM must have an alias"),
7384 errhint("For example, FROM (VALUES ...) [AS] foo."),
7385 scanner_errposition(@1)));
7388 (errcode(ERRCODE_SYNTAX_ERROR),
7389 errmsg("subquery in FROM must have an alias"),
7390 errhint("For example, FROM (SELECT ...) [AS] foo."),
7391 scanner_errposition(@1)));
7394 | select_with_parens alias_clause
7396 RangeSubselect *n = makeNode(RangeSubselect);
7405 | '(' joined_table ')' alias_clause
7414 * It may seem silly to separate joined_table from table_ref, but there is
7415 * method in SQL92's madness: if you don't do it this way you get reduce-
7416 * reduce conflicts, because it's not clear to the parser generator whether
7417 * to expect alias_clause after ')' or not. For the same reason we must
7418 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
7419 * join_type to expand to empty; if we try it, the parser generator can't
7420 * figure out when to reduce an empty join_type right after table_ref.
7422 * Note that a CROSS JOIN is the same as an unqualified
7423 * INNER JOIN, and an INNER JOIN/ON has the same shape
7424 * but a qualification expression to limit membership.
7425 * A NATURAL JOIN implicitly matches column names between
7426 * tables and the shape is determined by which columns are
7427 * in common. We'll collect columns during the later transformations.
7431 '(' joined_table ')'
7435 | table_ref CROSS JOIN table_ref
7437 /* CROSS JOIN is same as unqualified inner join */
7438 JoinExpr *n = makeNode(JoinExpr);
7439 n->jointype = JOIN_INNER;
7440 n->isNatural = FALSE;
7447 | table_ref join_type JOIN table_ref join_qual
7449 JoinExpr *n = makeNode(JoinExpr);
7451 n->isNatural = FALSE;
7454 if ($5 != NULL && IsA($5, List))
7455 n->using = (List *) $5; /* USING clause */
7457 n->quals = $5; /* ON clause */
7460 | table_ref JOIN table_ref join_qual
7462 /* letting join_type reduce to empty doesn't work */
7463 JoinExpr *n = makeNode(JoinExpr);
7464 n->jointype = JOIN_INNER;
7465 n->isNatural = FALSE;
7468 if ($4 != NULL && IsA($4, List))
7469 n->using = (List *) $4; /* USING clause */
7471 n->quals = $4; /* ON clause */
7474 | table_ref NATURAL join_type JOIN table_ref
7476 JoinExpr *n = makeNode(JoinExpr);
7478 n->isNatural = TRUE;
7481 n->using = NIL; /* figure out which columns later... */
7482 n->quals = NULL; /* fill later */
7485 | table_ref NATURAL JOIN table_ref
7487 /* letting join_type reduce to empty doesn't work */
7488 JoinExpr *n = makeNode(JoinExpr);
7489 n->jointype = JOIN_INNER;
7490 n->isNatural = TRUE;
7493 n->using = NIL; /* figure out which columns later... */
7494 n->quals = NULL; /* fill later */
7500 AS ColId '(' name_list ')'
7502 $$ = makeNode(Alias);
7508 $$ = makeNode(Alias);
7511 | ColId '(' name_list ')'
7513 $$ = makeNode(Alias);
7519 $$ = makeNode(Alias);
7524 join_type: FULL join_outer { $$ = JOIN_FULL; }
7525 | LEFT join_outer { $$ = JOIN_LEFT; }
7526 | RIGHT join_outer { $$ = JOIN_RIGHT; }
7527 | INNER_P { $$ = JOIN_INNER; }
7530 /* OUTER is just noise... */
7531 join_outer: OUTER_P { $$ = NULL; }
7532 | /*EMPTY*/ { $$ = NULL; }
7535 /* JOIN qualification clauses
7536 * Possibilities are:
7537 * USING ( column list ) allows only unqualified column names,
7538 * which must match between tables.
7539 * ON expr allows more general qualifications.
7541 * We return USING as a List node, while an ON-expr will not be a List.
7544 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
7545 | ON a_expr { $$ = $2; }
7552 /* default inheritance */
7554 $$->inhOpt = INH_DEFAULT;
7557 | qualified_name '*'
7559 /* inheritance query */
7561 $$->inhOpt = INH_YES;
7564 | ONLY qualified_name
7566 /* no inheritance */
7568 $$->inhOpt = INH_NO;
7571 | ONLY '(' qualified_name ')'
7573 /* no inheritance, SQL99-style syntax */
7575 $$->inhOpt = INH_NO;
7582 relation_expr { $$ = list_make1($1); }
7583 | relation_expr_list ',' relation_expr { $$ = lappend($1, $3); }
7588 * Given "UPDATE foo set set ...", we have to decide without looking any
7589 * further ahead whether the first "set" is an alias or the UPDATE's SET
7590 * keyword. Since "set" is allowed as a column name both interpretations
7591 * are feasible. We resolve the shift/reduce conflict by giving the first
7592 * relation_expr_opt_alias production a higher precedence than the SET token
7593 * has, causing the parser to prefer to reduce, in effect assuming that the
7594 * SET is not an alias.
7596 relation_expr_opt_alias: relation_expr %prec UMINUS
7600 | relation_expr ColId
7602 Alias *alias = makeNode(Alias);
7603 alias->aliasname = $2;
7607 | relation_expr AS ColId
7609 Alias *alias = makeNode(Alias);
7610 alias->aliasname = $3;
7617 func_table: func_expr { $$ = $1; }
7622 WHERE a_expr { $$ = $2; }
7623 | /*EMPTY*/ { $$ = NULL; }
7626 /* variant for UPDATE and DELETE */
7627 where_or_current_clause:
7628 WHERE a_expr { $$ = $2; }
7629 | WHERE CURRENT_P OF name
7631 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7632 /* cvarno is filled in by parse analysis */
7633 n->cursor_name = $4;
7634 n->cursor_param = 0;
7637 | WHERE CURRENT_P OF PARAM
7639 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7640 /* cvarno is filled in by parse analysis */
7641 n->cursor_name = NULL;
7642 n->cursor_param = $4;
7645 | /*EMPTY*/ { $$ = NULL; }
7649 TableFuncElementList:
7652 $$ = list_make1($1);
7654 | TableFuncElementList ',' TableFuncElement
7656 $$ = lappend($1, $3);
7660 TableFuncElement: ColId Typename
7662 ColumnDef *n = makeNode(ColumnDef);
7665 n->constraints = NIL;
7671 /*****************************************************************************
7674 * SQL92 introduces a large amount of type-specific syntax.
7675 * Define individual clauses to handle these cases, and use
7676 * the generic case to handle regular type-extensible Postgres syntax.
7677 * - thomas 1997-10-10
7679 *****************************************************************************/
7681 Typename: SimpleTypename opt_array_bounds
7684 $$->arrayBounds = $2;
7686 | SETOF SimpleTypename opt_array_bounds
7689 $$->arrayBounds = $3;
7692 /* SQL standard syntax, currently only one-dimensional */
7693 | SimpleTypename ARRAY '[' Iconst ']'
7696 $$->arrayBounds = list_make1(makeInteger($4));
7698 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7701 $$->arrayBounds = list_make1(makeInteger($5));
7704 | SimpleTypename ARRAY
7707 $$->arrayBounds = list_make1(makeInteger(-1));
7709 | SETOF SimpleTypename ARRAY
7712 $$->arrayBounds = list_make1(makeInteger(-1));
7718 opt_array_bounds '[' ']'
7719 { $$ = lappend($1, makeInteger(-1)); }
7720 | opt_array_bounds '[' Iconst ']'
7721 { $$ = lappend($1, makeInteger($3)); }
7727 GenericType { $$ = $1; }
7728 | Numeric { $$ = $1; }
7730 | Character { $$ = $1; }
7731 | ConstDatetime { $$ = $1; }
7732 | ConstInterval opt_interval
7737 | ConstInterval '(' Iconst ')' opt_interval
7742 if (list_length($5) != 1)
7744 (errcode(ERRCODE_SYNTAX_ERROR),
7745 errmsg("interval precision specified twice"),
7746 scanner_errposition(@1)));
7747 $$->typmods = lappend($5, makeIntConst($3, @3));
7750 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7751 makeIntConst($3, @3));
7755 /* We have a separate ConstTypename to allow defaulting fixed-length
7756 * types such as CHAR() and BIT() to an unspecified length.
7757 * SQL9x requires that these default to a length of one, but this
7758 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7759 * where there is an obvious better choice to make.
7760 * Note that ConstInterval is not included here since it must
7761 * be pushed up higher in the rules to accomodate the postfix
7762 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7763 * the generic-type-name case in AExprConst to avoid premature
7764 * reduce/reduce conflicts against function names.
7767 Numeric { $$ = $1; }
7768 | ConstBit { $$ = $1; }
7769 | ConstCharacter { $$ = $1; }
7770 | ConstDatetime { $$ = $1; }
7774 * GenericType covers all type names that don't have special syntax mandated
7775 * by the standard, including qualified names. We also allow type modifiers.
7776 * To avoid parsing conflicts against function invocations, the modifiers
7777 * have to be shown as expr_list here, but parse analysis will only accept
7778 * constants for them.
7781 type_function_name opt_type_modifiers
7783 $$ = makeTypeName($1);
7787 | type_function_name attrs opt_type_modifiers
7789 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7795 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7796 | /* EMPTY */ { $$ = NIL; }
7800 * SQL92 numeric data types
7804 $$ = SystemTypeName("int4");
7809 $$ = SystemTypeName("int4");
7814 $$ = SystemTypeName("int2");
7819 $$ = SystemTypeName("int8");
7824 $$ = SystemTypeName("float4");
7832 | DOUBLE_P PRECISION
7834 $$ = SystemTypeName("float8");
7837 | DECIMAL_P opt_type_modifiers
7839 $$ = SystemTypeName("numeric");
7843 | DEC opt_type_modifiers
7845 $$ = SystemTypeName("numeric");
7849 | NUMERIC opt_type_modifiers
7851 $$ = SystemTypeName("numeric");
7857 $$ = SystemTypeName("bool");
7862 opt_float: '(' Iconst ')'
7865 * Check FLOAT() precision limits assuming IEEE floating
7866 * types - thomas 1997-09-18
7870 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7871 errmsg("precision for type float must be at least 1 bit"),
7872 scanner_errposition(@2)));
7874 $$ = SystemTypeName("float4");
7876 $$ = SystemTypeName("float8");
7879 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7880 errmsg("precision for type float must be less than 54 bits"),
7881 scanner_errposition(@2)));
7885 $$ = SystemTypeName("float8");
7890 * SQL92 bit-field data types
7891 * The following implements BIT() and BIT VARYING().
7903 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7904 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7905 ConstBit: BitWithLength
7917 BIT opt_varying '(' expr_list ')'
7921 typname = $2 ? "varbit" : "bit";
7922 $$ = SystemTypeName(typname);
7931 /* bit defaults to bit(1), varbit to no limit */
7934 $$ = SystemTypeName("varbit");
7938 $$ = SystemTypeName("bit");
7939 $$->typmods = list_make1(makeIntConst(1, -1));
7947 * SQL92 character data types
7948 * The following implements CHAR() and VARCHAR().
7950 Character: CharacterWithLength
7954 | CharacterWithoutLength
7960 ConstCharacter: CharacterWithLength
7964 | CharacterWithoutLength
7966 /* Length was not specified so allow to be unrestricted.
7967 * This handles problems with fixed-length (bpchar) strings
7968 * which in column definitions must default to a length
7969 * of one, but should not be constrained if the length
7970 * was not specified.
7977 CharacterWithLength: character '(' Iconst ')' opt_charset
7979 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7983 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7990 $$ = SystemTypeName($1);
7991 $$->typmods = list_make1(makeIntConst($3, @3));
7996 CharacterWithoutLength: character opt_charset
7998 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
8002 type = palloc(strlen($1) + 1 + strlen($2) + 1);
8009 $$ = SystemTypeName($1);
8011 /* char defaults to char(1), varchar to no limit */
8012 if (strcmp($1, "bpchar") == 0)
8013 $$->typmods = list_make1(makeIntConst(1, -1));
8019 character: CHARACTER opt_varying
8020 { $$ = $2 ? "varchar": "bpchar"; }
8021 | CHAR_P opt_varying
8022 { $$ = $2 ? "varchar": "bpchar"; }
8025 | NATIONAL CHARACTER opt_varying
8026 { $$ = $3 ? "varchar": "bpchar"; }
8027 | NATIONAL CHAR_P opt_varying
8028 { $$ = $3 ? "varchar": "bpchar"; }
8030 { $$ = $2 ? "varchar": "bpchar"; }
8034 VARYING { $$ = TRUE; }
8035 | /*EMPTY*/ { $$ = FALSE; }
8039 CHARACTER SET ColId { $$ = $3; }
8040 | /*EMPTY*/ { $$ = NULL; }
8044 * SQL92 date/time types
8047 TIMESTAMP '(' Iconst ')' opt_timezone
8050 $$ = SystemTypeName("timestamptz");
8052 $$ = SystemTypeName("timestamp");
8053 $$->typmods = list_make1(makeIntConst($3, @3));
8056 | TIMESTAMP opt_timezone
8059 $$ = SystemTypeName("timestamptz");
8061 $$ = SystemTypeName("timestamp");
8064 | TIME '(' Iconst ')' opt_timezone
8067 $$ = SystemTypeName("timetz");
8069 $$ = SystemTypeName("time");
8070 $$->typmods = list_make1(makeIntConst($3, @3));
8076 $$ = SystemTypeName("timetz");
8078 $$ = SystemTypeName("time");
8086 $$ = SystemTypeName("interval");
8092 WITH_TIME ZONE { $$ = TRUE; }
8093 | WITHOUT TIME ZONE { $$ = FALSE; }
8094 | /*EMPTY*/ { $$ = FALSE; }
8099 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
8101 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
8103 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
8105 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
8107 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
8112 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
8113 INTERVAL_MASK(MONTH), @1));
8117 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8118 INTERVAL_MASK(HOUR), @1));
8122 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8123 INTERVAL_MASK(HOUR) |
8124 INTERVAL_MASK(MINUTE), @1));
8126 | DAY_P TO interval_second
8129 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
8130 INTERVAL_MASK(HOUR) |
8131 INTERVAL_MASK(MINUTE) |
8132 INTERVAL_MASK(SECOND), @1);
8134 | HOUR_P TO MINUTE_P
8136 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
8137 INTERVAL_MASK(MINUTE), @1));
8139 | HOUR_P TO interval_second
8142 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
8143 INTERVAL_MASK(MINUTE) |
8144 INTERVAL_MASK(SECOND), @1);
8146 | MINUTE_P TO interval_second
8149 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
8150 INTERVAL_MASK(SECOND), @1);
8159 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
8161 | SECOND_P '(' Iconst ')'
8163 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
8164 makeIntConst($3, @3));
8169 /*****************************************************************************
8171 * expression grammar
8173 *****************************************************************************/
8176 * General expressions
8177 * This is the heart of the expression syntax.
8179 * We have two expression types: a_expr is the unrestricted kind, and
8180 * b_expr is a subset that must be used in some places to avoid shift/reduce
8181 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
8182 * because that use of AND conflicts with AND as a boolean operator. So,
8183 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
8185 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
8186 * always be used by surrounding it with parens.
8188 * c_expr is all the productions that are common to a_expr and b_expr;
8189 * it's factored out just to eliminate redundant coding.
8191 a_expr: c_expr { $$ = $1; }
8192 | a_expr TYPECAST Typename
8193 { $$ = makeTypeCast($1, $3, @2); }
8194 | a_expr AT TIME ZONE a_expr
8196 FuncCall *n = makeNode(FuncCall);
8197 n->funcname = SystemFuncName("timezone");
8198 n->args = list_make2($5, $1);
8199 n->agg_star = FALSE;
8200 n->agg_distinct = FALSE;
8201 n->func_variadic = FALSE;
8207 * These operators must be called out explicitly in order to make use
8208 * of bison's automatic operator-precedence handling. All other
8209 * operator names are handled by the generic productions using "Op",
8210 * below; and all those operators will have the same precedence.
8212 * If you add more explicitly-known operators, be sure to add them
8213 * also to b_expr and to the MathOp list above.
8215 | '+' a_expr %prec UMINUS
8216 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8217 | '-' a_expr %prec UMINUS
8218 { $$ = doNegate($2, @1); }
8220 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8222 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8224 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8226 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8228 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8230 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8232 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8234 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8236 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8238 | a_expr qual_Op a_expr %prec Op
8239 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8240 | qual_Op a_expr %prec Op
8241 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8242 | a_expr qual_Op %prec POSTFIXOP
8243 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8246 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
8248 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
8250 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
8252 | a_expr LIKE a_expr
8253 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
8254 | a_expr LIKE a_expr ESCAPE a_expr
8256 FuncCall *n = makeNode(FuncCall);
8257 n->funcname = SystemFuncName("like_escape");
8258 n->args = list_make2($3, $5);
8259 n->agg_star = FALSE;
8260 n->agg_distinct = FALSE;
8261 n->func_variadic = FALSE;
8264 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
8266 | a_expr NOT LIKE a_expr
8267 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
8268 | a_expr NOT LIKE a_expr ESCAPE a_expr
8270 FuncCall *n = makeNode(FuncCall);
8271 n->funcname = SystemFuncName("like_escape");
8272 n->args = list_make2($4, $6);
8273 n->agg_star = FALSE;
8274 n->agg_distinct = FALSE;
8275 n->func_variadic = FALSE;
8278 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
8280 | a_expr ILIKE a_expr
8281 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
8282 | a_expr ILIKE a_expr ESCAPE a_expr
8284 FuncCall *n = makeNode(FuncCall);
8285 n->funcname = SystemFuncName("like_escape");
8286 n->args = list_make2($3, $5);
8287 n->agg_star = FALSE;
8288 n->agg_distinct = FALSE;
8289 n->func_variadic = FALSE;
8292 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
8294 | a_expr NOT ILIKE a_expr
8295 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
8296 | a_expr NOT ILIKE a_expr ESCAPE a_expr
8298 FuncCall *n = makeNode(FuncCall);
8299 n->funcname = SystemFuncName("like_escape");
8300 n->args = list_make2($4, $6);
8301 n->agg_star = FALSE;
8302 n->agg_distinct = FALSE;
8303 n->func_variadic = FALSE;
8306 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
8309 | a_expr SIMILAR TO a_expr %prec SIMILAR
8311 FuncCall *n = makeNode(FuncCall);
8312 n->funcname = SystemFuncName("similar_escape");
8313 n->args = list_make2($4, makeNullAConst(-1));
8314 n->agg_star = FALSE;
8315 n->agg_distinct = FALSE;
8316 n->func_variadic = FALSE;
8319 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8321 | a_expr SIMILAR TO a_expr ESCAPE a_expr
8323 FuncCall *n = makeNode(FuncCall);
8324 n->funcname = SystemFuncName("similar_escape");
8325 n->args = list_make2($4, $6);
8326 n->agg_star = FALSE;
8327 n->agg_distinct = FALSE;
8328 n->func_variadic = FALSE;
8331 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8333 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
8335 FuncCall *n = makeNode(FuncCall);
8336 n->funcname = SystemFuncName("similar_escape");
8337 n->args = list_make2($5, makeNullAConst(-1));
8338 n->agg_star = FALSE;
8339 n->agg_distinct = FALSE;
8340 n->func_variadic = FALSE;
8343 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8345 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
8347 FuncCall *n = makeNode(FuncCall);
8348 n->funcname = SystemFuncName("similar_escape");
8349 n->args = list_make2($5, $7);
8350 n->agg_star = FALSE;
8351 n->agg_distinct = FALSE;
8352 n->func_variadic = FALSE;
8355 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8359 * Define SQL92-style Null test clause.
8360 * Allow two forms described in the standard:
8363 * Allow two SQL extensions
8369 NullTest *n = makeNode(NullTest);
8370 n->arg = (Expr *) $1;
8371 n->nulltesttype = IS_NULL;
8376 NullTest *n = makeNode(NullTest);
8377 n->arg = (Expr *) $1;
8378 n->nulltesttype = IS_NULL;
8381 | a_expr IS NOT NULL_P
8383 NullTest *n = makeNode(NullTest);
8384 n->arg = (Expr *) $1;
8385 n->nulltesttype = IS_NOT_NULL;
8390 NullTest *n = makeNode(NullTest);
8391 n->arg = (Expr *) $1;
8392 n->nulltesttype = IS_NOT_NULL;
8397 $$ = (Node *)makeOverlaps($1, $3, @2);
8401 BooleanTest *b = makeNode(BooleanTest);
8402 b->arg = (Expr *) $1;
8403 b->booltesttype = IS_TRUE;
8406 | a_expr IS NOT TRUE_P
8408 BooleanTest *b = makeNode(BooleanTest);
8409 b->arg = (Expr *) $1;
8410 b->booltesttype = IS_NOT_TRUE;
8415 BooleanTest *b = makeNode(BooleanTest);
8416 b->arg = (Expr *) $1;
8417 b->booltesttype = IS_FALSE;
8420 | a_expr IS NOT FALSE_P
8422 BooleanTest *b = makeNode(BooleanTest);
8423 b->arg = (Expr *) $1;
8424 b->booltesttype = IS_NOT_FALSE;
8429 BooleanTest *b = makeNode(BooleanTest);
8430 b->arg = (Expr *) $1;
8431 b->booltesttype = IS_UNKNOWN;
8434 | a_expr IS NOT UNKNOWN
8436 BooleanTest *b = makeNode(BooleanTest);
8437 b->arg = (Expr *) $1;
8438 b->booltesttype = IS_NOT_UNKNOWN;
8441 | a_expr IS DISTINCT FROM a_expr %prec IS
8443 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8445 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
8447 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8448 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
8453 | a_expr IS OF '(' type_list ')' %prec IS
8455 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8457 | a_expr IS NOT OF '(' type_list ')' %prec IS
8459 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8462 * Ideally we would not use hard-wired operators below but instead use
8463 * opclasses. However, mixed data types and other issues make this
8464 * difficult: http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
8466 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8468 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8469 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8470 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8473 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8475 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8476 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8477 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8480 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8482 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8483 (Node *) makeA_Expr(AEXPR_AND, NIL,
8484 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8485 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8487 (Node *) makeA_Expr(AEXPR_AND, NIL,
8488 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
8489 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
8493 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8495 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8496 (Node *) makeA_Expr(AEXPR_OR, NIL,
8497 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8498 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8500 (Node *) makeA_Expr(AEXPR_OR, NIL,
8501 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
8502 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
8506 | a_expr IN_P in_expr
8508 /* in_expr returns a SubLink or a list of a_exprs */
8509 if (IsA($3, SubLink))
8511 /* generate foo = ANY (subquery) */
8512 SubLink *n = (SubLink *) $3;
8513 n->subLinkType = ANY_SUBLINK;
8515 n->operName = list_make1(makeString("="));
8521 /* generate scalar IN expression */
8522 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
8525 | a_expr NOT IN_P in_expr
8527 /* in_expr returns a SubLink or a list of a_exprs */
8528 if (IsA($4, SubLink))
8530 /* generate NOT (foo = ANY (subquery)) */
8531 /* Make an = ANY node */
8532 SubLink *n = (SubLink *) $4;
8533 n->subLinkType = ANY_SUBLINK;
8535 n->operName = list_make1(makeString("="));
8537 /* Stick a NOT on top */
8538 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
8542 /* generate scalar NOT IN expression */
8543 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
8546 | a_expr subquery_Op sub_type select_with_parens %prec Op
8548 SubLink *n = makeNode(SubLink);
8549 n->subLinkType = $3;
8556 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
8558 if ($3 == ANY_SUBLINK)
8559 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
8561 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
8563 | UNIQUE select_with_parens
8565 /* Not sure how to get rid of the parentheses
8566 * but there are lots of shift/reduce errors without them.
8568 * Should be able to implement this by plopping the entire
8569 * select into a node, then transforming the target expressions
8570 * from whatever they are into count(*), and testing the
8571 * entire result equal to one.
8572 * But, will probably implement a separate node in the executor.
8575 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8576 errmsg("UNIQUE predicate is not yet implemented"),
8577 scanner_errposition(@1)));
8579 | a_expr IS DOCUMENT_P %prec IS
8581 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8582 list_make1($1), @2);
8584 | a_expr IS NOT DOCUMENT_P %prec IS
8586 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8587 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8588 list_make1($1), @2),
8594 * Restricted expressions
8596 * b_expr is a subset of the complete expression syntax defined by a_expr.
8598 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8599 * cause trouble in the places where b_expr is used. For simplicity, we
8600 * just eliminate all the boolean-keyword-operator productions from b_expr.
8604 | b_expr TYPECAST Typename
8605 { $$ = makeTypeCast($1, $3, @2); }
8606 | '+' b_expr %prec UMINUS
8607 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8608 | '-' b_expr %prec UMINUS
8609 { $$ = doNegate($2, @1); }
8611 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8613 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8615 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8617 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8619 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8621 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8623 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8625 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8627 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8628 | b_expr qual_Op b_expr %prec Op
8629 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8630 | qual_Op b_expr %prec Op
8631 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8632 | b_expr qual_Op %prec POSTFIXOP
8633 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8634 | b_expr IS DISTINCT FROM b_expr %prec IS
8636 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8638 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8640 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8641 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8643 | b_expr IS OF '(' type_list ')' %prec IS
8645 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8647 | b_expr IS NOT OF '(' type_list ')' %prec IS
8649 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8651 | b_expr IS DOCUMENT_P %prec IS
8653 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8654 list_make1($1), @2);
8656 | b_expr IS NOT DOCUMENT_P %prec IS
8658 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8659 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8660 list_make1($1), @2),
8666 * Productions that can be used in both a_expr and b_expr.
8668 * Note: productions that refer recursively to a_expr or b_expr mostly
8669 * cannot appear here. However, it's OK to refer to a_exprs that occur
8670 * inside parentheses, such as function arguments; that cannot introduce
8671 * ambiguity to the b_expr syntax.
8673 c_expr: columnref { $$ = $1; }
8674 | AexprConst { $$ = $1; }
8675 | PARAM opt_indirection
8677 ParamRef *p = makeNode(ParamRef);
8682 A_Indirection *n = makeNode(A_Indirection);
8683 n->arg = (Node *) p;
8684 n->indirection = check_indirection($2);
8690 | '(' a_expr ')' opt_indirection
8694 A_Indirection *n = makeNode(A_Indirection);
8696 n->indirection = check_indirection($4);
8706 | select_with_parens %prec UMINUS
8708 SubLink *n = makeNode(SubLink);
8709 n->subLinkType = EXPR_SUBLINK;
8716 | EXISTS select_with_parens
8718 SubLink *n = makeNode(SubLink);
8719 n->subLinkType = EXISTS_SUBLINK;
8726 | ARRAY select_with_parens
8728 SubLink *n = makeNode(SubLink);
8729 n->subLinkType = ARRAY_SUBLINK;
8738 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8739 Assert(IsA(n, A_ArrayExpr));
8740 /* point outermost A_ArrayExpr to the ARRAY keyword */
8746 RowExpr *r = makeNode(RowExpr);
8748 r->row_typeid = InvalidOid; /* not analyzed yet */
8755 * func_expr is split out from c_expr just so that we have a classification
8756 * for "everything that is a function call or looks like one". This isn't
8757 * very important, but it saves us having to document which variants are
8758 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8759 * (Note that many of the special SQL functions wouldn't actually make any
8760 * sense as functional index entries, but we ignore that consideration here.)
8762 func_expr: func_name '(' ')' over_clause
8764 FuncCall *n = makeNode(FuncCall);
8767 n->agg_star = FALSE;
8768 n->agg_distinct = FALSE;
8769 n->func_variadic = FALSE;
8774 | func_name '(' expr_list ')' over_clause
8776 FuncCall *n = makeNode(FuncCall);
8779 n->agg_star = FALSE;
8780 n->agg_distinct = FALSE;
8781 n->func_variadic = FALSE;
8786 | func_name '(' VARIADIC a_expr ')' over_clause
8788 FuncCall *n = makeNode(FuncCall);
8790 n->args = list_make1($4);
8791 n->agg_star = FALSE;
8792 n->agg_distinct = FALSE;
8793 n->func_variadic = TRUE;
8798 | func_name '(' expr_list ',' VARIADIC a_expr ')' over_clause
8800 FuncCall *n = makeNode(FuncCall);
8802 n->args = lappend($3, $6);
8803 n->agg_star = FALSE;
8804 n->agg_distinct = FALSE;
8805 n->func_variadic = TRUE;
8810 | func_name '(' ALL expr_list ')' over_clause
8812 FuncCall *n = makeNode(FuncCall);
8815 n->agg_star = FALSE;
8816 n->agg_distinct = FALSE;
8817 /* Ideally we'd mark the FuncCall node to indicate
8818 * "must be an aggregate", but there's no provision
8819 * for that in FuncCall at the moment.
8821 n->func_variadic = FALSE;
8826 | func_name '(' DISTINCT expr_list ')' over_clause
8828 FuncCall *n = makeNode(FuncCall);
8831 n->agg_star = FALSE;
8832 n->agg_distinct = TRUE;
8833 n->func_variadic = FALSE;
8838 | func_name '(' '*' ')' over_clause
8841 * We consider AGGREGATE(*) to invoke a parameterless
8842 * aggregate. This does the right thing for COUNT(*),
8843 * and there are no other aggregates in SQL92 that accept
8846 * The FuncCall node is also marked agg_star = true,
8847 * so that later processing can detect what the argument
8850 FuncCall *n = makeNode(FuncCall);
8854 n->agg_distinct = FALSE;
8855 n->func_variadic = FALSE;
8863 * Translate as "'now'::text::date".
8865 * We cannot use "'now'::date" because coerce_type() will
8866 * immediately reduce that to a constant representing
8867 * today's date. We need to delay the conversion until
8868 * runtime, else the wrong things will happen when
8869 * CURRENT_DATE is used in a column default value or rule.
8871 * This could be simplified if we had a way to generate
8872 * an expression tree representing runtime application
8873 * of type-input conversion functions. (As of PG 7.3
8874 * that is actually possible, but not clear that we want
8878 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8879 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8884 * Translate as "'now'::text::timetz".
8885 * See comments for CURRENT_DATE.
8888 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8889 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8891 | CURRENT_TIME '(' Iconst ')'
8894 * Translate as "'now'::text::timetz(n)".
8895 * See comments for CURRENT_DATE.
8899 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8900 d = SystemTypeName("timetz");
8901 d->typmods = list_make1(makeIntConst($3, @3));
8902 $$ = makeTypeCast(n, d, -1);
8907 * Translate as "now()", since we have a function that
8908 * does exactly what is needed.
8910 FuncCall *n = makeNode(FuncCall);
8911 n->funcname = SystemFuncName("now");
8913 n->agg_star = FALSE;
8914 n->agg_distinct = FALSE;
8915 n->func_variadic = FALSE;
8920 | CURRENT_TIMESTAMP '(' Iconst ')'
8923 * Translate as "'now'::text::timestamptz(n)".
8924 * See comments for CURRENT_DATE.
8928 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8929 d = SystemTypeName("timestamptz");
8930 d->typmods = list_make1(makeIntConst($3, @3));
8931 $$ = makeTypeCast(n, d, -1);
8936 * Translate as "'now'::text::time".
8937 * See comments for CURRENT_DATE.
8940 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8941 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8943 | LOCALTIME '(' Iconst ')'
8946 * Translate as "'now'::text::time(n)".
8947 * See comments for CURRENT_DATE.
8951 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8952 d = SystemTypeName("time");
8953 d->typmods = list_make1(makeIntConst($3, @3));
8954 $$ = makeTypeCast((Node *)n, d, -1);
8959 * Translate as "'now'::text::timestamp".
8960 * See comments for CURRENT_DATE.
8963 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8964 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8966 | LOCALTIMESTAMP '(' Iconst ')'
8969 * Translate as "'now'::text::timestamp(n)".
8970 * See comments for CURRENT_DATE.
8974 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8975 d = SystemTypeName("timestamp");
8976 d->typmods = list_make1(makeIntConst($3, @3));
8977 $$ = makeTypeCast(n, d, -1);
8981 FuncCall *n = makeNode(FuncCall);
8982 n->funcname = SystemFuncName("current_user");
8984 n->agg_star = FALSE;
8985 n->agg_distinct = FALSE;
8986 n->func_variadic = FALSE;
8993 FuncCall *n = makeNode(FuncCall);
8994 n->funcname = SystemFuncName("current_user");
8996 n->agg_star = FALSE;
8997 n->agg_distinct = FALSE;
8998 n->func_variadic = FALSE;
9005 FuncCall *n = makeNode(FuncCall);
9006 n->funcname = SystemFuncName("session_user");
9008 n->agg_star = FALSE;
9009 n->agg_distinct = FALSE;
9010 n->func_variadic = FALSE;
9017 FuncCall *n = makeNode(FuncCall);
9018 n->funcname = SystemFuncName("current_user");
9020 n->agg_star = FALSE;
9021 n->agg_distinct = FALSE;
9022 n->func_variadic = FALSE;
9029 FuncCall *n = makeNode(FuncCall);
9030 n->funcname = SystemFuncName("current_database");
9032 n->agg_star = FALSE;
9033 n->agg_distinct = FALSE;
9034 n->func_variadic = FALSE;
9041 FuncCall *n = makeNode(FuncCall);
9042 n->funcname = SystemFuncName("current_schema");
9044 n->agg_star = FALSE;
9045 n->agg_distinct = FALSE;
9046 n->func_variadic = FALSE;
9051 | CAST '(' a_expr AS Typename ')'
9052 { $$ = makeTypeCast($3, $5, @1); }
9053 | EXTRACT '(' extract_list ')'
9055 FuncCall *n = makeNode(FuncCall);
9056 n->funcname = SystemFuncName("date_part");
9058 n->agg_star = FALSE;
9059 n->agg_distinct = FALSE;
9060 n->func_variadic = FALSE;
9065 | OVERLAY '(' overlay_list ')'
9067 /* overlay(A PLACING B FROM C FOR D) is converted to
9068 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
9069 * overlay(A PLACING B FROM C) is converted to
9070 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
9072 FuncCall *n = makeNode(FuncCall);
9073 n->funcname = SystemFuncName("overlay");
9075 n->agg_star = FALSE;
9076 n->agg_distinct = FALSE;
9077 n->func_variadic = FALSE;
9082 | POSITION '(' position_list ')'
9084 /* position(A in B) is converted to position(B, A) */
9085 FuncCall *n = makeNode(FuncCall);
9086 n->funcname = SystemFuncName("position");
9088 n->agg_star = FALSE;
9089 n->agg_distinct = FALSE;
9090 n->func_variadic = FALSE;
9095 | SUBSTRING '(' substr_list ')'
9097 /* substring(A from B for C) is converted to
9098 * substring(A, B, C) - thomas 2000-11-28
9100 FuncCall *n = makeNode(FuncCall);
9101 n->funcname = SystemFuncName("substring");
9103 n->agg_star = FALSE;
9104 n->agg_distinct = FALSE;
9105 n->func_variadic = FALSE;
9110 | TREAT '(' a_expr AS Typename ')'
9112 /* TREAT(expr AS target) converts expr of a particular type to target,
9113 * which is defined to be a subtype of the original expression.
9114 * In SQL99, this is intended for use with structured UDTs,
9115 * but let's make this a generally useful form allowing stronger
9116 * coercions than are handled by implicit casting.
9118 FuncCall *n = makeNode(FuncCall);
9119 /* Convert SystemTypeName() to SystemFuncName() even though
9120 * at the moment they result in the same thing.
9122 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
9123 n->args = list_make1($3);
9124 n->agg_star = FALSE;
9125 n->agg_distinct = FALSE;
9126 n->func_variadic = FALSE;
9131 | TRIM '(' BOTH trim_list ')'
9133 /* various trim expressions are defined in SQL92
9134 * - thomas 1997-07-19
9136 FuncCall *n = makeNode(FuncCall);
9137 n->funcname = SystemFuncName("btrim");
9139 n->agg_star = FALSE;
9140 n->agg_distinct = FALSE;
9141 n->func_variadic = FALSE;
9146 | TRIM '(' LEADING trim_list ')'
9148 FuncCall *n = makeNode(FuncCall);
9149 n->funcname = SystemFuncName("ltrim");
9151 n->agg_star = FALSE;
9152 n->agg_distinct = FALSE;
9153 n->func_variadic = FALSE;
9158 | TRIM '(' TRAILING trim_list ')'
9160 FuncCall *n = makeNode(FuncCall);
9161 n->funcname = SystemFuncName("rtrim");
9163 n->agg_star = FALSE;
9164 n->agg_distinct = FALSE;
9165 n->func_variadic = FALSE;
9170 | TRIM '(' trim_list ')'
9172 FuncCall *n = makeNode(FuncCall);
9173 n->funcname = SystemFuncName("btrim");
9175 n->agg_star = FALSE;
9176 n->agg_distinct = FALSE;
9177 n->func_variadic = FALSE;
9182 | NULLIF '(' a_expr ',' a_expr ')'
9184 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
9186 | COALESCE '(' expr_list ')'
9188 CoalesceExpr *c = makeNode(CoalesceExpr);
9193 | GREATEST '(' expr_list ')'
9195 MinMaxExpr *v = makeNode(MinMaxExpr);
9197 v->op = IS_GREATEST;
9201 | LEAST '(' expr_list ')'
9203 MinMaxExpr *v = makeNode(MinMaxExpr);
9209 | XMLCONCAT '(' expr_list ')'
9211 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
9213 | XMLELEMENT '(' NAME_P ColLabel ')'
9215 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
9217 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
9219 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
9221 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
9223 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
9225 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
9227 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
9229 | XMLFOREST '(' xml_attribute_list ')'
9231 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
9233 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
9235 XmlExpr *x = (XmlExpr *)
9236 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
9237 list_make2($4, makeBoolAConst($5, -1)),
9242 | XMLPI '(' NAME_P ColLabel ')'
9244 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
9246 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
9248 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
9250 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
9252 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
9253 list_make3($3, $5, $6), @1);
9255 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
9257 XmlSerialize *n = makeNode(XmlSerialize);
9269 xml_root_version: VERSION_P a_expr
9271 | VERSION_P NO VALUE_P
9272 { $$ = makeNullAConst(-1); }
9275 opt_xml_root_standalone: ',' STANDALONE_P YES_P
9276 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
9277 | ',' STANDALONE_P NO
9278 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
9279 | ',' STANDALONE_P NO VALUE_P
9280 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
9282 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
9285 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
9288 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
9289 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
9292 xml_attribute_el: a_expr AS ColLabel
9294 $$ = makeNode(ResTarget);
9296 $$->indirection = NIL;
9297 $$->val = (Node *) $1;
9302 $$ = makeNode(ResTarget);
9304 $$->indirection = NIL;
9305 $$->val = (Node *) $1;
9310 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
9311 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
9314 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
9315 | STRIP_P WHITESPACE_P { $$ = FALSE; }
9316 | /*EMPTY*/ { $$ = FALSE; }
9320 * Window Definitions
9323 WINDOW window_definition_list { $$ = $2; }
9324 | /*EMPTY*/ { $$ = NIL; }
9327 window_definition_list:
9328 window_definition { $$ = list_make1($1); }
9329 | window_definition_list ',' window_definition
9330 { $$ = lappend($1, $3); }
9334 ColId AS window_specification
9342 over_clause: OVER window_specification
9346 WindowDef *n = makeNode(WindowDef);
9349 n->partitionClause = NIL;
9350 n->orderClause = NIL;
9351 n->frameOptions = FRAMEOPTION_DEFAULTS;
9359 window_specification: '(' opt_existing_window_name opt_partition_clause
9360 opt_sort_clause opt_frame_clause ')'
9362 WindowDef *n = makeNode(WindowDef);
9365 n->partitionClause = $3;
9366 n->orderClause = $4;
9367 n->frameOptions = $5;
9374 * If we see PARTITION, RANGE, or ROWS as the first token after the '('
9375 * of a window_specification, we want the assumption to be that there is
9376 * no existing_window_name; but those keywords are unreserved and so could
9377 * be ColIds. We fix this by making them have the same precedence as IDENT
9378 * and giving the empty production here a slightly higher precedence, so
9379 * that the shift/reduce conflict is resolved in favor of reducing the rule.
9380 * These keywords are thus precluded from being an existing_window_name but
9381 * are not reserved for any other purpose.
9383 opt_existing_window_name: ColId { $$ = $1; }
9384 | /*EMPTY*/ %prec Op { $$ = NULL; }
9387 opt_partition_clause: PARTITION BY expr_list { $$ = $3; }
9388 | /*EMPTY*/ { $$ = NIL; }
9392 * This is only a subset of the full SQL:2008 frame_clause grammar.
9393 * We don't support <expression> PRECEDING, <expression> FOLLOWING,
9394 * nor <window frame exclusion> yet.
9399 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE | $2;
9403 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS | $2;
9406 { $$ = FRAMEOPTION_DEFAULTS; }
9409 frame_extent: frame_bound
9411 /* reject invalid cases */
9412 if ($1 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9414 (errcode(ERRCODE_WINDOWING_ERROR),
9415 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9416 scanner_errposition(@1)));
9417 if ($1 & FRAMEOPTION_START_CURRENT_ROW)
9419 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9420 errmsg("frame start at CURRENT ROW is not implemented"),
9421 scanner_errposition(@1)));
9422 $$ = $1 | FRAMEOPTION_END_CURRENT_ROW;
9424 | BETWEEN frame_bound AND frame_bound
9426 /* reject invalid cases */
9427 if ($2 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9429 (errcode(ERRCODE_WINDOWING_ERROR),
9430 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9431 scanner_errposition(@2)));
9432 if ($2 & FRAMEOPTION_START_CURRENT_ROW)
9434 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9435 errmsg("frame start at CURRENT ROW is not implemented"),
9436 scanner_errposition(@2)));
9437 if ($4 & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
9439 (errcode(ERRCODE_WINDOWING_ERROR),
9440 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
9441 scanner_errposition(@4)));
9442 /* shift converts START_ options to END_ options */
9443 $$ = FRAMEOPTION_BETWEEN | $2 | ($4 << 1);
9448 * This is used for both frame start and frame end, with output set up on
9449 * the assumption it's frame start; the frame_extent productions must reject
9455 $$ = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
9457 | UNBOUNDED FOLLOWING
9459 $$ = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
9463 $$ = FRAMEOPTION_START_CURRENT_ROW;
9469 * Supporting nonterminals for expressions.
9472 /* Explicit row production.
9474 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
9475 * without conflicting with the parenthesized a_expr production. Without the
9476 * ROW keyword, there must be more than one a_expr inside the parens.
9478 row: ROW '(' expr_list ')' { $$ = $3; }
9479 | ROW '(' ')' { $$ = NIL; }
9480 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
9483 sub_type: ANY { $$ = ANY_SUBLINK; }
9484 | SOME { $$ = ANY_SUBLINK; }
9485 | ALL { $$ = ALL_SUBLINK; }
9488 all_Op: Op { $$ = $1; }
9489 | MathOp { $$ = $1; }
9492 MathOp: '+' { $$ = "+"; }
9504 { $$ = list_make1(makeString($1)); }
9505 | OPERATOR '(' any_operator ')'
9511 { $$ = list_make1(makeString($1)); }
9512 | OPERATOR '(' any_operator ')'
9518 { $$ = list_make1(makeString($1)); }
9519 | OPERATOR '(' any_operator ')'
9522 { $$ = list_make1(makeString("~~")); }
9524 { $$ = list_make1(makeString("!~~")); }
9526 { $$ = list_make1(makeString("~~*")); }
9528 { $$ = list_make1(makeString("!~~*")); }
9529 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
9530 * the regular expression is preprocessed by a function (similar_escape),
9531 * and the ~ operator for posix regular expressions is used.
9532 * x SIMILAR TO y -> x ~ similar_escape(y)
9533 * this transformation is made on the fly by the parser upwards.
9534 * however the SubLink structure which handles any/some/all stuff
9535 * is not ready for such a thing.
9541 $$ = list_make1($1);
9543 | expr_list ',' a_expr
9545 $$ = lappend($1, $3);
9549 type_list: Typename { $$ = list_make1($1); }
9550 | type_list ',' Typename { $$ = lappend($1, $3); }
9553 array_expr: '[' expr_list ']'
9555 $$ = makeAArrayExpr($2, @1);
9557 | '[' array_expr_list ']'
9559 $$ = makeAArrayExpr($2, @1);
9563 $$ = makeAArrayExpr(NIL, @1);
9567 array_expr_list: array_expr { $$ = list_make1($1); }
9568 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
9573 extract_arg FROM a_expr
9575 $$ = list_make2(makeStringConst($1, @1), $3);
9577 | /*EMPTY*/ { $$ = NIL; }
9580 /* Allow delimited string Sconst in extract_arg as an SQL extension.
9581 * - thomas 2001-04-12
9585 | YEAR_P { $$ = "year"; }
9586 | MONTH_P { $$ = "month"; }
9587 | DAY_P { $$ = "day"; }
9588 | HOUR_P { $$ = "hour"; }
9589 | MINUTE_P { $$ = "minute"; }
9590 | SECOND_P { $$ = "second"; }
9591 | Sconst { $$ = $1; }
9594 /* OVERLAY() arguments
9595 * SQL99 defines the OVERLAY() function:
9596 * o overlay(text placing text from int for int)
9597 * o overlay(text placing text from int)
9600 a_expr overlay_placing substr_from substr_for
9602 $$ = list_make4($1, $2, $3, $4);
9604 | a_expr overlay_placing substr_from
9606 $$ = list_make3($1, $2, $3);
9615 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
9618 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
9619 | /*EMPTY*/ { $$ = NIL; }
9622 /* SUBSTRING() arguments
9623 * SQL9x defines a specific syntax for arguments to SUBSTRING():
9624 * o substring(text from int for int)
9625 * o substring(text from int) get entire string from starting point "int"
9626 * o substring(text for int) get first "int" characters of string
9627 * o substring(text from pattern) get entire string matching pattern
9628 * o substring(text from pattern for escape) same with specified escape char
9629 * We also want to support generic substring functions which accept
9630 * the usual generic list of arguments. So we will accept both styles
9631 * here, and convert the SQL9x style to the generic list for further
9632 * processing. - thomas 2000-11-28
9635 a_expr substr_from substr_for
9637 $$ = list_make3($1, $2, $3);
9639 | a_expr substr_for substr_from
9641 /* not legal per SQL99, but might as well allow it */
9642 $$ = list_make3($1, $3, $2);
9644 | a_expr substr_from
9646 $$ = list_make2($1, $2);
9651 * Since there are no cases where this syntax allows
9652 * a textual FOR value, we forcibly cast the argument
9653 * to int4. The possible matches in pg_proc are
9654 * substring(text,int4) and substring(text,text),
9655 * and we don't want the parser to choose the latter,
9656 * which it is likely to do if the second argument
9657 * is unknown or doesn't have an implicit cast to int4.
9659 $$ = list_make3($1, makeIntConst(1, -1),
9661 SystemTypeName("int4"), -1));
9672 FROM a_expr { $$ = $2; }
9675 substr_for: FOR a_expr { $$ = $2; }
9678 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
9679 | FROM expr_list { $$ = $2; }
9680 | expr_list { $$ = $1; }
9683 in_expr: select_with_parens
9685 SubLink *n = makeNode(SubLink);
9687 /* other fields will be filled later */
9690 | '(' expr_list ')' { $$ = (Node *)$2; }
9694 * Define SQL92-style case clause.
9695 * - Full specification
9696 * CASE WHEN a = b THEN c ... ELSE d END
9697 * - Implicit argument
9698 * CASE a WHEN b THEN c ... ELSE d END
9700 case_expr: CASE case_arg when_clause_list case_default END_P
9702 CaseExpr *c = makeNode(CaseExpr);
9703 c->casetype = InvalidOid; /* not analyzed yet */
9704 c->arg = (Expr *) $2;
9706 c->defresult = (Expr *) $4;
9713 /* There must be at least one */
9714 when_clause { $$ = list_make1($1); }
9715 | when_clause_list when_clause { $$ = lappend($1, $2); }
9719 WHEN a_expr THEN a_expr
9721 CaseWhen *w = makeNode(CaseWhen);
9722 w->expr = (Expr *) $2;
9723 w->result = (Expr *) $4;
9730 ELSE a_expr { $$ = $2; }
9731 | /*EMPTY*/ { $$ = NULL; }
9734 case_arg: a_expr { $$ = $1; }
9735 | /*EMPTY*/ { $$ = NULL; }
9739 * columnref starts with relation_name not ColId, so that OLD and NEW
9740 * references can be accepted. Note that when there are more than two
9741 * dotted names, the first name is not actually a relation name...
9743 columnref: relation_name
9745 $$ = makeColumnRef($1, NIL, @1);
9747 | relation_name indirection
9749 $$ = makeColumnRef($1, $2, @1);
9756 $$ = (Node *) makeString($2);
9760 $$ = (Node *) makeNode(A_Star);
9764 A_Indices *ai = makeNode(A_Indices);
9769 | '[' a_expr ':' a_expr ']'
9771 A_Indices *ai = makeNode(A_Indices);
9779 indirection_el { $$ = list_make1($1); }
9780 | indirection indirection_el { $$ = lappend($1, $2); }
9784 /*EMPTY*/ { $$ = NIL; }
9785 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9788 opt_asymmetric: ASYMMETRIC
9793 * The SQL spec defines "contextually typed value expressions" and
9794 * "contextually typed row value constructors", which for our purposes
9795 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9800 a_expr { $$ = (Node *) $1; }
9803 SetToDefault *n = makeNode(SetToDefault);
9810 ctext_expr { $$ = list_make1($1); }
9811 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9815 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9816 * making VALUES a fully reserved word, which will probably break more apps
9817 * than allowing the noise-word is worth.
9819 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9823 /*****************************************************************************
9825 * target list for SELECT
9827 *****************************************************************************/
9830 target_el { $$ = list_make1($1); }
9831 | target_list ',' target_el { $$ = lappend($1, $3); }
9834 target_el: a_expr AS ColLabel
9836 $$ = makeNode(ResTarget);
9838 $$->indirection = NIL;
9839 $$->val = (Node *)$1;
9843 * We support omitting AS only for column labels that aren't
9844 * any known keyword. There is an ambiguity against postfix
9845 * operators: is "a ! b" an infix expression, or a postfix
9846 * expression and a column label? We prefer to resolve this
9847 * as an infix expression, which we accomplish by assigning
9848 * IDENT a precedence higher than POSTFIXOP.
9852 $$ = makeNode(ResTarget);
9854 $$->indirection = NIL;
9855 $$->val = (Node *)$1;
9860 $$ = makeNode(ResTarget);
9862 $$->indirection = NIL;
9863 $$->val = (Node *)$1;
9868 ColumnRef *n = makeNode(ColumnRef);
9869 n->fields = list_make1(makeNode(A_Star));
9872 $$ = makeNode(ResTarget);
9874 $$->indirection = NIL;
9875 $$->val = (Node *)n;
9881 /*****************************************************************************
9883 * Names and constants
9885 *****************************************************************************/
9888 SpecialRuleRelation { $$ = $1; }
9889 | ColId { $$ = $1; }
9892 qualified_name_list:
9893 qualified_name { $$ = list_make1($1); }
9894 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9898 * The production for a qualified relation name has to exactly match the
9899 * production for a qualified func_name, because in a FROM clause we cannot
9900 * tell which we are parsing until we see what comes after it ('(' for a
9901 * func_name, something else for a relation). Therefore we allow 'indirection'
9902 * which may contain subscripts, and reject that case in the C code.
9907 $$ = makeNode(RangeVar);
9908 $$->catalogname = NULL;
9909 $$->schemaname = NULL;
9913 | relation_name indirection
9915 check_qualified_name($2);
9916 $$ = makeNode(RangeVar);
9917 switch (list_length($2))
9920 $$->catalogname = NULL;
9921 $$->schemaname = $1;
9922 $$->relname = strVal(linitial($2));
9925 $$->catalogname = $1;
9926 $$->schemaname = strVal(linitial($2));
9927 $$->relname = strVal(lsecond($2));
9931 (errcode(ERRCODE_SYNTAX_ERROR),
9932 errmsg("improper qualified name (too many dotted names): %s",
9933 NameListToString(lcons(makeString($1), $2))),
9934 scanner_errposition(@1)));
9942 { $$ = list_make1(makeString($1)); }
9943 | name_list ',' name
9944 { $$ = lappend($1, makeString($3)); }
9948 name: ColId { $$ = $1; };
9956 attr_name: ColLabel { $$ = $1; };
9958 index_name: ColId { $$ = $1; };
9960 file_name: Sconst { $$ = $1; };
9963 * The production for a qualified func_name has to exactly match the
9964 * production for a qualified columnref, because we cannot tell which we
9965 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9966 * anything else for a columnref). Therefore we allow 'indirection' which
9967 * may contain subscripts, and reject that case in the C code. (If we
9968 * ever implement SQL99-like methods, such syntax may actually become legal!)
9970 func_name: type_function_name
9971 { $$ = list_make1(makeString($1)); }
9972 | relation_name indirection
9973 { $$ = check_func_name(lcons(makeString($1), $2)); }
9982 $$ = makeIntConst($1, @1);
9986 $$ = makeFloatConst($1, @1);
9990 $$ = makeStringConst($1, @1);
9994 $$ = makeBitStringConst($1, @1);
9998 /* This is a bit constant per SQL99:
9999 * Without Feature F511, "BIT data type",
10000 * a <general literal> shall not be a
10001 * <bit string literal> or a <hex string literal>.
10003 $$ = makeBitStringConst($1, @1);
10007 /* generic type 'literal' syntax */
10008 TypeName *t = makeTypeNameFromNameList($1);
10010 $$ = makeStringConstCast($2, @2, t);
10012 | func_name '(' expr_list ')' Sconst
10014 /* generic syntax with a type modifier */
10015 TypeName *t = makeTypeNameFromNameList($1);
10018 $$ = makeStringConstCast($5, @5, t);
10020 | ConstTypename Sconst
10022 $$ = makeStringConstCast($2, @2, $1);
10024 | ConstInterval Sconst opt_interval
10028 $$ = makeStringConstCast($2, @2, t);
10030 | ConstInterval '(' Iconst ')' Sconst opt_interval
10035 if (list_length($6) != 1)
10037 (errcode(ERRCODE_SYNTAX_ERROR),
10038 errmsg("interval precision specified twice"),
10039 scanner_errposition(@1)));
10040 t->typmods = lappend($6, makeIntConst($3, @3));
10043 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
10044 makeIntConst($3, @3));
10045 $$ = makeStringConstCast($5, @5, t);
10049 $$ = makeBoolAConst(TRUE, @1);
10053 $$ = makeBoolAConst(FALSE, @1);
10057 $$ = makeNullAConst(@1);
10061 Iconst: ICONST { $$ = $1; };
10062 Sconst: SCONST { $$ = $1; };
10063 RoleId: ColId { $$ = $1; };
10065 SignedIconst: Iconst { $$ = $1; }
10066 | '+' Iconst { $$ = + $2; }
10067 | '-' Iconst { $$ = - $2; }
10071 * Name classification hierarchy.
10073 * IDENT is the lexeme returned by the lexer for identifiers that match
10074 * no known keyword. In most cases, we can accept certain keywords as
10075 * names, not only IDENTs. We prefer to accept as many such keywords
10076 * as possible to minimize the impact of "reserved words" on programmers.
10077 * So, we divide names into several possible classes. The classification
10078 * is chosen in part to make keywords acceptable as names wherever possible.
10081 /* Column identifier --- names that can be column, table, etc names.
10083 ColId: IDENT { $$ = $1; }
10084 | unreserved_keyword { $$ = pstrdup($1); }
10085 | col_name_keyword { $$ = pstrdup($1); }
10088 /* Type/function identifier --- names that can be type or function names.
10090 type_function_name: IDENT { $$ = $1; }
10091 | unreserved_keyword { $$ = pstrdup($1); }
10092 | type_func_name_keyword { $$ = pstrdup($1); }
10095 /* Column label --- allowed labels in "AS" clauses.
10096 * This presently includes *all* Postgres keywords.
10098 ColLabel: IDENT { $$ = $1; }
10099 | unreserved_keyword { $$ = pstrdup($1); }
10100 | col_name_keyword { $$ = pstrdup($1); }
10101 | type_func_name_keyword { $$ = pstrdup($1); }
10102 | reserved_keyword { $$ = pstrdup($1); }
10107 * Keyword category lists. Generally, every keyword present in
10108 * the Postgres grammar should appear in exactly one of these lists.
10110 * Put a new keyword into the first list that it can go into without causing
10111 * shift or reduce conflicts. The earlier lists define "less reserved"
10112 * categories of keywords.
10114 * Make sure that each keyword's category in keywords.c matches where
10115 * it is listed here. (Someday we may be able to generate these lists and
10116 * keywords.c's table from a common master list.)
10119 /* "Unreserved" keywords --- available for use as any kind of name.
10121 unreserved_keyword:
10370 /* Column identifier --- keywords that can be column, table, etc names.
10372 * Many of these keywords will in fact be recognized as type or function
10373 * names too; but they have special productions for the purpose, and so
10374 * can't be treated as "generic" type or function names.
10376 * The type names appearing here are not usable as function names
10377 * because they can be followed by '(' in typename productions, which
10378 * looks too much like a function call for an LR(1) parser.
10428 /* Type/function identifier --- keywords that can be type or function names.
10430 * Most of these are keywords that are used as operators in expressions;
10431 * in general such keywords can't be column names because they would be
10432 * ambiguous with variables, but they are unambiguous as function identifiers.
10434 * Do not include POSITION, SUBSTRING, etc here since they have explicit
10435 * productions in a_expr to support the goofy SQL9x argument syntax.
10436 * - thomas 2000-11-28
10438 type_func_name_keyword:
10463 /* Reserved keyword --- these keywords are usable only as a ColLabel.
10465 * Keywords appear here if they could not be distinguished from variable,
10466 * type, or function names in some contexts. Don't put things here unless
10491 | CURRENT_TIMESTAMP
10552 SpecialRuleRelation:
10559 (errcode(ERRCODE_SYNTAX_ERROR),
10560 errmsg("OLD used in query that is not in a rule"),
10561 scanner_errposition(@1)));
10569 (errcode(ERRCODE_SYNTAX_ERROR),
10570 errmsg("NEW used in query that is not in a rule"),
10571 scanner_errposition(@1)));
10578 makeColumnRef(char *colname, List *indirection, int location)
10581 * Generate a ColumnRef node, with an A_Indirection node added if there
10582 * is any subscripting in the specified indirection list. However,
10583 * any field selection at the start of the indirection list must be
10584 * transposed into the "fields" part of the ColumnRef node.
10586 ColumnRef *c = makeNode(ColumnRef);
10590 c->location = location;
10591 foreach(l, indirection)
10593 if (IsA(lfirst(l), A_Indices))
10595 A_Indirection *i = makeNode(A_Indirection);
10599 /* easy case - all indirection goes to A_Indirection */
10600 c->fields = list_make1(makeString(colname));
10601 i->indirection = check_indirection(indirection);
10605 /* got to split the list in two */
10606 i->indirection = check_indirection(list_copy_tail(indirection,
10608 indirection = list_truncate(indirection, nfields);
10609 c->fields = lcons(makeString(colname), indirection);
10611 i->arg = (Node *) c;
10614 else if (IsA(lfirst(l), A_Star))
10616 /* We only allow '*' at the end of a ColumnRef */
10617 if (lnext(l) != NULL)
10618 yyerror("improper use of \"*\"");
10622 /* No subscripting, so all indirection gets added to field list */
10623 c->fields = lcons(makeString(colname), indirection);
10628 makeTypeCast(Node *arg, TypeName *typename, int location)
10630 TypeCast *n = makeNode(TypeCast);
10632 n->typename = typename;
10633 n->location = location;
10638 makeStringConst(char *str, int location)
10640 A_Const *n = makeNode(A_Const);
10642 n->val.type = T_String;
10643 n->val.val.str = str;
10644 n->location = location;
10650 makeStringConstCast(char *str, int location, TypeName *typename)
10652 Node *s = makeStringConst(str, location);
10654 return makeTypeCast(s, typename, -1);
10658 makeIntConst(int val, int location)
10660 A_Const *n = makeNode(A_Const);
10662 n->val.type = T_Integer;
10663 n->val.val.ival = val;
10664 n->location = location;
10670 makeFloatConst(char *str, int location)
10672 A_Const *n = makeNode(A_Const);
10674 n->val.type = T_Float;
10675 n->val.val.str = str;
10676 n->location = location;
10682 makeBitStringConst(char *str, int location)
10684 A_Const *n = makeNode(A_Const);
10686 n->val.type = T_BitString;
10687 n->val.val.str = str;
10688 n->location = location;
10694 makeNullAConst(int location)
10696 A_Const *n = makeNode(A_Const);
10698 n->val.type = T_Null;
10699 n->location = location;
10705 makeAConst(Value *v, int location)
10712 n = makeFloatConst(v->val.str, location);
10716 n = makeIntConst(v->val.ival, location);
10721 n = makeStringConst(v->val.str, location);
10728 /* makeBoolAConst()
10729 * Create an A_Const string node and put it inside a boolean cast.
10732 makeBoolAConst(bool state, int location)
10734 A_Const *n = makeNode(A_Const);
10736 n->val.type = T_String;
10737 n->val.val.str = (state ? "t" : "f");
10738 n->location = location;
10740 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
10744 * Create and populate a FuncCall node to support the OVERLAPS operator.
10747 makeOverlaps(List *largs, List *rargs, int location)
10749 FuncCall *n = makeNode(FuncCall);
10751 n->funcname = SystemFuncName("overlaps");
10752 if (list_length(largs) == 1)
10753 largs = lappend(largs, largs);
10754 else if (list_length(largs) != 2)
10756 (errcode(ERRCODE_SYNTAX_ERROR),
10757 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10758 scanner_errposition(location)));
10759 if (list_length(rargs) == 1)
10760 rargs = lappend(rargs, rargs);
10761 else if (list_length(rargs) != 2)
10763 (errcode(ERRCODE_SYNTAX_ERROR),
10764 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10765 scanner_errposition(location)));
10766 n->args = list_concat(largs, rargs);
10767 n->agg_star = FALSE;
10768 n->agg_distinct = FALSE;
10769 n->func_variadic = FALSE;
10771 n->location = location;
10775 /* check_qualified_name --- check the result of qualified_name production
10777 * It's easiest to let the grammar production for qualified_name allow
10778 * subscripts and '*', which we then must reject here.
10781 check_qualified_name(List *names)
10787 if (!IsA(lfirst(i), String))
10788 yyerror("syntax error");
10792 /* check_func_name --- check the result of func_name production
10794 * It's easiest to let the grammar production for func_name allow subscripts
10795 * and '*', which we then must reject here.
10798 check_func_name(List *names)
10804 if (!IsA(lfirst(i), String))
10805 yyerror("syntax error");
10810 /* check_indirection --- check the result of indirection production
10812 * We only allow '*' at the end of the list, but it's hard to enforce that
10813 * in the grammar, so do it here.
10816 check_indirection(List *indirection)
10820 foreach(l, indirection)
10822 if (IsA(lfirst(l), A_Star))
10824 if (lnext(l) != NULL)
10825 yyerror("improper use of \"*\"");
10828 return indirection;
10831 /* extractArgTypes()
10832 * Given a list of FunctionParameter nodes, extract a list of just the
10833 * argument types (TypeNames) for input parameters only. This is what
10834 * is needed to look up an existing function, which is what is wanted by
10835 * the productions that use this call.
10838 extractArgTypes(List *parameters)
10840 List *result = NIL;
10843 foreach(i, parameters)
10845 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10847 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10848 result = lappend(result, p->argType);
10853 /* findLeftmostSelect()
10854 * Find the leftmost component SelectStmt in a set-operation parsetree.
10856 static SelectStmt *
10857 findLeftmostSelect(SelectStmt *node)
10859 while (node && node->op != SETOP_NONE)
10861 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10865 /* insertSelectOptions()
10866 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10868 * This routine is just to avoid duplicating code in SelectStmt productions.
10871 insertSelectOptions(SelectStmt *stmt,
10872 List *sortClause, List *lockingClause,
10873 Node *limitOffset, Node *limitCount,
10874 WithClause *withClause)
10876 Assert(IsA(stmt, SelectStmt));
10879 * Tests here are to reject constructs like
10880 * (SELECT foo ORDER BY bar) ORDER BY baz
10884 if (stmt->sortClause)
10886 (errcode(ERRCODE_SYNTAX_ERROR),
10887 errmsg("multiple ORDER BY clauses not allowed"),
10888 scanner_errposition(exprLocation((Node *) sortClause))));
10889 stmt->sortClause = sortClause;
10891 /* We can handle multiple locking clauses, though */
10892 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10895 if (stmt->limitOffset)
10897 (errcode(ERRCODE_SYNTAX_ERROR),
10898 errmsg("multiple OFFSET clauses not allowed"),
10899 scanner_errposition(exprLocation(limitOffset))));
10900 stmt->limitOffset = limitOffset;
10904 if (stmt->limitCount)
10906 (errcode(ERRCODE_SYNTAX_ERROR),
10907 errmsg("multiple LIMIT clauses not allowed"),
10908 scanner_errposition(exprLocation(limitCount))));
10909 stmt->limitCount = limitCount;
10913 if (stmt->withClause)
10915 (errcode(ERRCODE_SYNTAX_ERROR),
10916 errmsg("multiple WITH clauses not allowed"),
10917 scanner_errposition(exprLocation((Node *) withClause))));
10918 stmt->withClause = withClause;
10923 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10925 SelectStmt *n = makeNode(SelectStmt);
10929 n->larg = (SelectStmt *) larg;
10930 n->rarg = (SelectStmt *) rarg;
10934 /* SystemFuncName()
10935 * Build a properly-qualified reference to a built-in function.
10938 SystemFuncName(char *name)
10940 return list_make2(makeString("pg_catalog"), makeString(name));
10943 /* SystemTypeName()
10944 * Build a properly-qualified reference to a built-in type.
10946 * typmod is defaulted, but may be changed afterwards by caller.
10947 * Likewise for the location.
10950 SystemTypeName(char *name)
10952 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10953 makeString(name)));
10957 * Handle negation of a numeric constant.
10959 * Formerly, we did this here because the optimizer couldn't cope with
10960 * indexquals that looked like "var = -4" --- it wants "var = const"
10961 * and a unary minus operator applied to a constant didn't qualify.
10962 * As of Postgres 7.0, that problem doesn't exist anymore because there
10963 * is a constant-subexpression simplifier in the optimizer. However,
10964 * there's still a good reason for doing this here, which is that we can
10965 * postpone committing to a particular internal representation for simple
10966 * negative constants. It's better to leave "-123.456" in string form
10967 * until we know what the desired type is.
10970 doNegate(Node *n, int location)
10972 if (IsA(n, A_Const))
10974 A_Const *con = (A_Const *)n;
10976 /* report the constant's location as that of the '-' sign */
10977 con->location = location;
10979 if (con->val.type == T_Integer)
10981 con->val.val.ival = -con->val.val.ival;
10984 if (con->val.type == T_Float)
10986 doNegateFloat(&con->val);
10991 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10995 doNegateFloat(Value *v)
10997 char *oldval = v->val.str;
10999 Assert(IsA(v, Float));
11000 if (*oldval == '+')
11002 if (*oldval == '-')
11003 v->val.str = oldval+1; /* just strip the '-' */
11006 char *newval = (char *) palloc(strlen(oldval) + 2);
11009 strcpy(newval+1, oldval);
11010 v->val.str = newval;
11015 makeAArrayExpr(List *elements, int location)
11017 A_ArrayExpr *n = makeNode(A_ArrayExpr);
11019 n->elements = elements;
11020 n->location = location;
11025 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
11028 XmlExpr *x = makeNode(XmlExpr);
11033 * named_args is a list of ResTarget; it'll be split apart into separate
11034 * expression and name lists in transformXmlExpr().
11036 x->named_args = named_args;
11037 x->arg_names = NIL;
11039 /* xmloption, if relevant, must be filled in by caller */
11040 /* type and typmod will be filled in during parse analysis */
11041 x->location = location;
11046 * Initialize to parse one query string
11051 QueryIsRule = FALSE;
11055 * Merge the input and output parameters of a table function.
11058 mergeTableFuncParameters(List *func_args, List *columns)
11062 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
11063 foreach(lc, func_args)
11065 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
11067 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
11069 (errcode(ERRCODE_SYNTAX_ERROR),
11070 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
11073 return list_concat(func_args, columns);
11077 * Determine return type of a TABLE function. A single result column
11078 * returns setof that column's type; otherwise return setof record.
11081 TableFuncTypeName(List *columns)
11085 if (list_length(columns) == 1)
11087 FunctionParameter *p = (FunctionParameter *) linitial(columns);
11089 result = (TypeName *) copyObject(p->argType);
11092 result = SystemTypeName("record");
11094 result->setof = true;
11100 * Must undefine base_yylex before including scan.c, since we want it
11101 * to create the function base_yylex not filtered_base_yylex.