1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/pl/plpgsql/src/plpgsql.h
13 *-------------------------------------------------------------------------
21 #include "access/xact.h"
22 #include "commands/event_trigger.h"
23 #include "commands/trigger.h"
24 #include "executor/spi.h"
26 /**********************************************************************
28 **********************************************************************/
30 /* define our text domain for translations */
32 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
35 #define _(x) dgettext(TEXTDOMAIN, x)
38 * Compiler's namespace item types
50 * Datum array node types
58 PLPGSQL_DTYPE_RECFIELD,
59 PLPGSQL_DTYPE_ARRAYELEM,
64 * Variants distinguished in PLpgSQL_type structs
69 PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
70 PLPGSQL_TTYPE_ROW, /* composite types */
71 PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
72 PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
76 * Execution tree node types
79 enum PLpgSQL_stmt_types
90 PLPGSQL_STMT_FOREACH_A,
93 PLPGSQL_STMT_RETURN_NEXT,
94 PLPGSQL_STMT_RETURN_QUERY,
97 PLPGSQL_STMT_DYNEXECUTE,
108 * Execution node return codes
120 * GET DIAGNOSTICS information items
125 PLPGSQL_GETDIAG_ROW_COUNT,
126 PLPGSQL_GETDIAG_RESULT_OID,
127 PLPGSQL_GETDIAG_CONTEXT,
128 PLPGSQL_GETDIAG_ERROR_CONTEXT,
129 PLPGSQL_GETDIAG_ERROR_DETAIL,
130 PLPGSQL_GETDIAG_ERROR_HINT,
131 PLPGSQL_GETDIAG_RETURNED_SQLSTATE,
132 PLPGSQL_GETDIAG_COLUMN_NAME,
133 PLPGSQL_GETDIAG_CONSTRAINT_NAME,
134 PLPGSQL_GETDIAG_DATATYPE_NAME,
135 PLPGSQL_GETDIAG_MESSAGE_TEXT,
136 PLPGSQL_GETDIAG_TABLE_NAME,
137 PLPGSQL_GETDIAG_SCHEMA_NAME
141 * RAISE statement options
146 PLPGSQL_RAISEOPTION_ERRCODE,
147 PLPGSQL_RAISEOPTION_MESSAGE,
148 PLPGSQL_RAISEOPTION_DETAIL,
149 PLPGSQL_RAISEOPTION_HINT,
150 PLPGSQL_RAISEOPTION_COLUMN,
151 PLPGSQL_RAISEOPTION_CONSTRAINT,
152 PLPGSQL_RAISEOPTION_DATATYPE,
153 PLPGSQL_RAISEOPTION_TABLE,
154 PLPGSQL_RAISEOPTION_SCHEMA
158 * Behavioral modes for plpgsql variable resolution
163 PLPGSQL_RESOLVE_ERROR, /* throw error if ambiguous */
164 PLPGSQL_RESOLVE_VARIABLE, /* prefer plpgsql var to table column */
165 PLPGSQL_RESOLVE_COLUMN /* prefer table column to plpgsql var */
166 } PLpgSQL_resolve_option;
169 /**********************************************************************
170 * Node and structure definitions
171 **********************************************************************/
175 { /* Postgres data type */
176 char *typname; /* (simple) name of the type */
177 Oid typoid; /* OID of the data type */
178 int ttype; /* PLPGSQL_TTYPE_ code */
179 int16 typlen; /* stuff copied from its pg_type entry */
183 Oid collation; /* from pg_type, but can be overridden */
184 FmgrInfo typinput; /* lookup info for typinput function */
185 int32 atttypmod; /* typmod (taken from someplace else) */
190 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
191 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, and PLpgSQL_arrayelem
194 { /* Generic datum array item */
200 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
204 { /* Scalar or composite variable */
211 typedef struct PLpgSQL_expr
212 { /* SQL Query to plan and execute */
217 Bitmapset *paramnos; /* all dnos referenced by this query */
219 /* function containing this expr (not set until we first parse query) */
220 struct PLpgSQL_function *func;
222 /* namespace chain visible to this expr */
223 struct PLpgSQL_nsitem *ns;
225 /* fields for "simple expression" fast-path execution: */
226 Expr *expr_simple_expr; /* NULL means not a simple expr */
227 int expr_simple_generation; /* plancache generation we checked */
228 Oid expr_simple_type; /* result type Oid, if simple */
231 * if expr is simple AND prepared in current transaction,
232 * expr_simple_state and expr_simple_in_use are valid. Test validity by
233 * seeing if expr_simple_lxid matches current LXID. (If not,
234 * expr_simple_state probably points at garbage!)
236 ExprState *expr_simple_state; /* eval tree for expr_simple_expr */
237 bool expr_simple_in_use; /* true if eval tree is active */
238 LocalTransactionId expr_simple_lxid;
243 { /* Scalar variable */
249 PLpgSQL_type *datatype;
252 PLpgSQL_expr *default_val;
253 PLpgSQL_expr *cursor_explicit_expr;
254 int cursor_explicit_argrow;
270 TupleDesc rowtupdesc;
273 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
275 * Note: if the underlying rowtype contains a dropped column, the
276 * corresponding fieldnames[] entry will be NULL, and there is no
277 * corresponding var (varnos[] will be -1).
286 { /* Record variable (non-fixed structure) */
300 { /* Field in record */
304 int recparentno; /* dno of parent record */
309 { /* Element of array variable */
312 PLpgSQL_expr *subscript;
313 int arrayparentno; /* dno of parent array variable */
314 /* Remaining fields are cached info about the array variable's type */
315 Oid parenttypoid; /* type of array variable; 0 if not yet set */
316 int32 parenttypmod; /* typmod of array variable */
317 Oid arraytypoid; /* OID of actual array type */
318 int32 arraytypmod; /* typmod of array (and its elements too) */
319 int16 arraytyplen; /* typlen of array type */
320 Oid elemtypoid; /* OID of array element type */
321 int16 elemtyplen; /* typlen of element type */
322 bool elemtypbyval; /* element type is pass-by-value? */
323 char elemtypalign; /* typalign of element type */
327 typedef struct PLpgSQL_nsitem
328 { /* Item in the compilers namespace tree */
331 struct PLpgSQL_nsitem *prev;
332 char name[1]; /* actually, as long as needed */
337 { /* Generic execution node */
343 typedef struct PLpgSQL_condition
344 { /* One EXCEPTION condition name */
345 int sqlerrstate; /* SQLSTATE code */
346 char *condname; /* condition name (for debugging) */
347 struct PLpgSQL_condition *next;
354 List *exc_list; /* List of WHEN clauses */
355 } PLpgSQL_exception_block;
358 { /* One EXCEPTION ... WHEN clause */
360 PLpgSQL_condition *conditions;
361 List *action; /* List of statements */
366 { /* Block of statements */
370 List *body; /* List of statements */
373 PLpgSQL_exception_block *exceptions;
374 } PLpgSQL_stmt_block;
378 { /* Assign statement */
383 } PLpgSQL_stmt_assign;
386 { /* PERFORM statement */
390 } PLpgSQL_stmt_perform;
393 { /* Get Diagnostics item */
394 int kind; /* id for diagnostic value desired */
395 int target; /* where to assign it */
399 { /* Get Diagnostics statement */
402 bool is_stacked; /* STACKED or CURRENT diagnostics area? */
403 List *diag_items; /* List of PLpgSQL_diag_item */
404 } PLpgSQL_stmt_getdiag;
411 PLpgSQL_expr *cond; /* boolean expression for THEN */
412 List *then_body; /* List of statements */
413 List *elsif_list; /* List of PLpgSQL_if_elsif structs */
414 List *else_body; /* List of statements */
417 typedef struct /* one ELSIF arm of IF statement */
420 PLpgSQL_expr *cond; /* boolean expression for this case */
421 List *stmts; /* List of statements */
425 typedef struct /* CASE statement */
429 PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
430 int t_varno; /* var to store test expression value into */
431 List *case_when_list; /* List of PLpgSQL_case_when structs */
432 bool have_else; /* flag needed because list could be empty */
433 List *else_stmts; /* List of statements */
436 typedef struct /* one arm of CASE statement */
439 PLpgSQL_expr *expr; /* boolean expression for this case */
440 List *stmts; /* List of statements */
445 { /* Unconditional LOOP statement */
449 List *body; /* List of statements */
454 { /* WHILE cond LOOP statement */
459 List *body; /* List of statements */
460 } PLpgSQL_stmt_while;
464 { /* FOR statement with integer loopvar */
471 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
473 List *body; /* List of statements */
478 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
479 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
480 * and PLpgSQL_dynfors.
489 List *body; /* List of statements */
493 { /* FOR statement running over SELECT */
499 List *body; /* List of statements */
500 /* end of fields that must match PLpgSQL_stmt_forq */
505 { /* FOR statement running over cursor */
511 List *body; /* List of statements */
512 /* end of fields that must match PLpgSQL_stmt_forq */
514 PLpgSQL_expr *argquery; /* cursor arguments if any */
518 { /* FOR statement running over EXECUTE */
524 List *body; /* List of statements */
525 /* end of fields that must match PLpgSQL_stmt_forq */
527 List *params; /* USING expressions */
528 } PLpgSQL_stmt_dynfors;
532 { /* FOREACH item in array loop */
536 int varno; /* loop target variable */
537 int slice; /* slice dimension, or 0 */
538 PLpgSQL_expr *expr; /* array expression */
539 List *body; /* List of statements */
540 } PLpgSQL_stmt_foreach_a;
544 { /* OPEN a curvar */
549 PLpgSQL_row *returntype;
550 PLpgSQL_expr *argquery;
552 PLpgSQL_expr *dynquery;
553 List *params; /* USING expressions */
558 { /* FETCH or MOVE statement */
561 PLpgSQL_rec *rec; /* target, as record or row */
563 int curvar; /* cursor variable to fetch from */
564 FetchDirection direction; /* fetch direction */
565 long how_many; /* count, if constant (expr is NULL) */
566 PLpgSQL_expr *expr; /* count, if expression */
567 bool is_move; /* is this a fetch or move? */
568 bool returns_multiple_rows; /* can return more than one row? */
569 } PLpgSQL_stmt_fetch;
577 } PLpgSQL_stmt_close;
581 { /* EXIT or CONTINUE statement */
584 bool is_exit; /* Is this an exit or a continue? */
585 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
591 { /* RETURN statement */
596 } PLpgSQL_stmt_return;
599 { /* RETURN NEXT statement */
604 } PLpgSQL_stmt_return_next;
607 { /* RETURN QUERY statement */
610 PLpgSQL_expr *query; /* if static query */
611 PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
612 List *params; /* USING arguments for dynamic query */
613 } PLpgSQL_stmt_return_query;
616 { /* RAISE statement */
620 char *condname; /* condition name, SQLSTATE, or NULL */
621 char *message; /* old-style message format literal, or NULL */
622 List *params; /* list of expressions for old-style message */
623 List *options; /* list of PLpgSQL_raise_option */
624 } PLpgSQL_stmt_raise;
627 { /* RAISE statement option */
630 } PLpgSQL_raise_option;
634 { /* Generic SQL statement to execute */
637 PLpgSQL_expr *sqlstmt;
638 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
639 /* note: mod_stmt is set when we plan the query */
640 bool into; /* INTO supplied? */
641 bool strict; /* INTO STRICT flag */
642 PLpgSQL_rec *rec; /* INTO target, if record */
643 PLpgSQL_row *row; /* INTO target, if row */
644 } PLpgSQL_stmt_execsql;
648 { /* Dynamic SQL string to execute */
651 PLpgSQL_expr *query; /* string expression */
652 bool into; /* INTO supplied? */
653 bool strict; /* INTO STRICT flag */
654 PLpgSQL_rec *rec; /* INTO target, if record */
655 PLpgSQL_row *row; /* INTO target, if row */
656 List *params; /* USING expressions */
657 } PLpgSQL_stmt_dynexecute;
660 typedef struct PLpgSQL_func_hashkey
661 { /* Hash lookup key for functions */
664 bool isTrigger; /* true if called as a trigger */
666 /* be careful that pad bytes in this struct get zeroed! */
669 * For a trigger function, the OID of the relation triggered on is part of
670 * the hash key --- we want to compile the trigger separately for each
671 * relation it is used with, in case the rowtype is different. Zero if
672 * not called as a trigger.
677 * We must include the input collation as part of the hash key too,
678 * because we have to generate different plans (with different Param
679 * collations) for different collation settings.
684 * We include actual argument types in the hash key to support polymorphic
685 * PLpgSQL functions. Be careful that extra positions are zeroed!
687 Oid argtypes[FUNC_MAX_ARGS];
688 } PLpgSQL_func_hashkey;
690 typedef enum PLpgSQL_trigtype
693 PLPGSQL_EVENT_TRIGGER,
697 typedef struct PLpgSQL_function
698 { /* Complete compiled function */
701 TransactionId fn_xmin;
702 ItemPointerData fn_tid;
703 PLpgSQL_trigtype fn_is_trigger;
704 Oid fn_input_collation;
705 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
706 MemoryContext fn_cxt;
711 FmgrInfo fn_retinput;
712 Oid fn_rettypioparam;
718 int fn_argvarnos[FUNC_MAX_ARGS];
728 int tg_relname_varno;
729 int tg_table_name_varno;
730 int tg_table_schema_varno;
734 /* for event triggers */
738 PLpgSQL_resolve_option resolve_option;
740 bool print_strict_params;
743 PLpgSQL_datum **datums;
744 PLpgSQL_stmt_block *action;
746 /* these fields change when the function is used */
747 struct PLpgSQL_execstate *cur_estate;
748 unsigned long use_count;
752 typedef struct PLpgSQL_execstate
753 { /* Runtime execution data */
754 PLpgSQL_function *func; /* function being executed */
758 Oid rettype; /* type of current retval */
760 Oid fn_rettype; /* info about declared function rettype */
766 TupleDesc rettupdesc;
767 char *exitlabel; /* the "target" label of the current EXIT or
768 * CONTINUE stmt, if any */
769 ErrorData *cur_error; /* current exception handler's error */
771 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
772 MemoryContext tuple_store_cxt;
773 ResourceOwner tuple_store_owner;
776 /* the datums representing the function's local variables */
779 PLpgSQL_datum **datums;
781 /* EState to use for "simple" expression evaluation */
782 EState *simple_eval_estate;
784 /* temporary state for results from evaluation of query or expr */
785 SPITupleTable *eval_tuptable;
786 uint32 eval_processed;
788 ExprContext *eval_econtext; /* for executing simple expressions */
789 PLpgSQL_expr *cur_expr; /* current query/expr being evaluated */
791 /* status information for error context reporting */
792 PLpgSQL_stmt *err_stmt; /* current stmt */
793 const char *err_text; /* additional state info */
795 void *plugin_info; /* reserved for use by optional plugin */
800 * A PLpgSQL_plugin structure represents an instrumentation plugin.
801 * To instrument PL/pgSQL, a plugin library must access the rendezvous
802 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
803 * Typically the struct could just be static data in the plugin library.
804 * We expect that a plugin would do this at library load time (_PG_init()).
805 * It must also be careful to set the rendezvous variable back to NULL
806 * if it is unloaded (_PG_fini()).
808 * This structure is basically a collection of function pointers --- at
809 * various interesting points in pl_exec.c, we call these functions
810 * (if the pointers are non-NULL) to give the plugin a chance to watch
813 * func_setup is called when we start a function, before we've initialized
814 * the local variables defined by the function.
816 * func_beg is called when we start a function, after we've initialized
817 * the local variables.
819 * func_end is called at the end of a function.
821 * stmt_beg and stmt_end are called before and after (respectively) each
824 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
825 * error_callback and assign_expr fields with pointers to its own
826 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
827 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
832 /* Function pointers set up by the plugin */
833 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
834 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
835 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
836 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
837 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
839 /* Function pointers set by PL/pgSQL itself */
840 void (*error_callback) (void *arg);
841 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
846 /* Struct types used during parsing */
850 char *ident; /* palloc'd converted identifier */
851 bool quoted; /* Was it double-quoted? */
856 List *idents; /* composite identifiers (list of String) */
861 PLpgSQL_datum *datum; /* referenced variable */
862 char *ident; /* valid if simple name */
864 List *idents; /* valid if composite name */
867 /**********************************************************************
868 * Global variable declarations
869 **********************************************************************/
873 IDENTIFIER_LOOKUP_NORMAL, /* normal processing of var names */
874 IDENTIFIER_LOOKUP_DECLARE, /* In DECLARE --- don't look up names */
875 IDENTIFIER_LOOKUP_EXPR /* In SQL expression --- special case */
878 extern IdentifierLookup plpgsql_IdentifierLookup;
880 extern int plpgsql_variable_conflict;
882 extern bool plpgsql_print_strict_params;
884 extern bool plpgsql_check_syntax;
885 extern bool plpgsql_DumpExecTree;
887 extern PLpgSQL_stmt_block *plpgsql_parse_result;
889 extern int plpgsql_nDatums;
890 extern PLpgSQL_datum **plpgsql_Datums;
892 extern char *plpgsql_error_funcname;
894 extern PLpgSQL_function *plpgsql_curr_compile;
895 extern MemoryContext compile_tmp_cxt;
897 extern PLpgSQL_plugin **plugin_ptr;
899 /**********************************************************************
900 * Function declarations
901 **********************************************************************/
904 * Functions in pl_comp.c
907 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
909 extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
910 extern void plpgsql_parser_setup(struct ParseState *pstate,
912 extern bool plpgsql_parse_word(char *word1, const char *yytxt,
913 PLwdatum *wdatum, PLword *word);
914 extern bool plpgsql_parse_dblword(char *word1, char *word2,
915 PLwdatum *wdatum, PLcword *cword);
916 extern bool plpgsql_parse_tripword(char *word1, char *word2, char *word3,
917 PLwdatum *wdatum, PLcword *cword);
918 extern PLpgSQL_type *plpgsql_parse_wordtype(char *ident);
919 extern PLpgSQL_type *plpgsql_parse_cwordtype(List *idents);
920 extern PLpgSQL_type *plpgsql_parse_wordrowtype(char *ident);
921 extern PLpgSQL_type *plpgsql_parse_cwordrowtype(List *idents);
922 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod,
924 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
927 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
929 extern int plpgsql_recognize_err_condition(const char *condname,
930 bool allow_sqlstate);
931 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
932 extern void plpgsql_adddatum(PLpgSQL_datum *new);
933 extern int plpgsql_add_initdatums(int **varnos);
934 extern void plpgsql_HashTableInit(void);
937 * Functions in pl_handler.c
940 extern void _PG_init(void);
941 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
942 extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
943 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
946 * Functions in pl_exec.c
949 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
950 FunctionCallInfo fcinfo,
951 EState *simple_eval_estate);
952 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
953 TriggerData *trigdata);
954 extern void plpgsql_exec_event_trigger(PLpgSQL_function *func,
955 EventTriggerData *trigdata);
956 extern void plpgsql_xact_cb(XactEvent event, void *arg);
957 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
958 SubTransactionId parentSubid, void *arg);
959 extern Oid exec_get_datum_type(PLpgSQL_execstate *estate,
960 PLpgSQL_datum *datum);
961 extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
962 PLpgSQL_datum *datum,
963 Oid *typeid, int32 *typmod, Oid *collation);
966 * Functions for namespace handling in pl_funcs.c
969 extern void plpgsql_ns_init(void);
970 extern void plpgsql_ns_push(const char *label);
971 extern void plpgsql_ns_pop(void);
972 extern PLpgSQL_nsitem *plpgsql_ns_top(void);
973 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
974 extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
975 const char *name1, const char *name2,
976 const char *name3, int *names_used);
977 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
981 * Other functions in pl_funcs.c
984 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
985 extern const char *plpgsql_getdiag_kindname(int kind);
986 extern void plpgsql_free_function_memory(PLpgSQL_function *func);
987 extern void plpgsql_dumptree(PLpgSQL_function *func);
990 * Scanner functions in pl_scanner.c
993 extern int plpgsql_base_yylex(void);
994 extern int plpgsql_yylex(void);
995 extern void plpgsql_push_back_token(int token);
996 extern bool plpgsql_token_is_unreserved_keyword(int token);
997 extern void plpgsql_append_source_text(StringInfo buf,
998 int startlocation, int endlocation);
999 extern int plpgsql_peek(void);
1000 extern void plpgsql_peek2(int *tok1_p, int *tok2_p, int *tok1_loc,
1002 extern int plpgsql_scanner_errposition(int location);
1003 extern void plpgsql_yyerror(const char *message);
1004 extern int plpgsql_location_to_lineno(int location);
1005 extern int plpgsql_latest_lineno(void);
1006 extern void plpgsql_scanner_init(const char *str);
1007 extern void plpgsql_scanner_finish(void);
1013 extern int plpgsql_yyparse(void);
1015 #endif /* PLPGSQL_H */