1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.105 2008/11/05 00:07:54 tgl Exp $
13 *-------------------------------------------------------------------------
22 #include "miscadmin.h"
23 #include "commands/trigger.h"
24 #include "executor/spi.h"
25 #include "utils/tuplestore.h"
27 /**********************************************************************
29 **********************************************************************/
31 /* define our text domain for translations */
33 #define TEXTDOMAIN "plpgsql"
36 * Compiler's namestack item types
48 * Datum array node types
56 PLPGSQL_DTYPE_RECFIELD,
57 PLPGSQL_DTYPE_ARRAYELEM,
63 * Variants distinguished in PLpgSQL_type structs
68 PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
69 PLPGSQL_TTYPE_ROW, /* composite types */
70 PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
71 PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
75 * Execution tree node types
78 enum PLpgSQL_stmt_types
91 PLPGSQL_STMT_RETURN_NEXT,
92 PLPGSQL_STMT_RETURN_QUERY,
95 PLPGSQL_STMT_DYNEXECUTE,
106 * Execution node return codes
119 * GET DIAGNOSTICS system attrs
124 PLPGSQL_GETDIAG_ROW_COUNT,
125 PLPGSQL_GETDIAG_RESULT_OID
129 * RAISE statement options
134 PLPGSQL_RAISEOPTION_ERRCODE,
135 PLPGSQL_RAISEOPTION_MESSAGE,
136 PLPGSQL_RAISEOPTION_DETAIL,
137 PLPGSQL_RAISEOPTION_HINT
141 /**********************************************************************
142 * Node and structure definitions
143 **********************************************************************/
147 { /* Dynamic string control structure */
149 int used; /* Including NUL terminator */
155 { /* Postgres data type */
156 char *typname; /* (simple) name of the type */
157 Oid typoid; /* OID of the data type */
158 int ttype; /* PLPGSQL_TTYPE_ code */
159 int16 typlen; /* stuff copied from its pg_type entry */
163 FmgrInfo typinput; /* lookup info for typinput function */
164 int32 atttypmod; /* typmod (taken from someplace else) */
169 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
170 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
174 { /* Generic datum array item */
180 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
184 { /* Scalar or composite variable */
191 typedef struct PLpgSQL_expr
192 { /* SQL Query to plan and execute */
198 /* fields for "simple expression" fast-path execution: */
199 Expr *expr_simple_expr; /* NULL means not a simple expr */
200 int expr_simple_generation; /* plancache generation we checked */
201 Oid expr_simple_type; /* result type Oid, if simple */
204 * if expr is simple AND prepared in current eval_estate,
205 * expr_simple_state is valid. Test validity by seeing if expr_simple_id
206 * matches eval_estate_simple_id.
208 ExprState *expr_simple_state;
209 long int expr_simple_id;
211 /* params to pass to expr */
213 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
218 { /* Scalar variable */
224 PLpgSQL_type *datatype;
227 PLpgSQL_expr *default_val;
228 PLpgSQL_expr *cursor_explicit_expr;
229 int cursor_explicit_argrow;
245 TupleDesc rowtupdesc;
248 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
250 * Note: if the underlying rowtype contains a dropped column, the
251 * corresponding fieldnames[] entry will be NULL, and there is no
252 * corresponding var (varnos[] will be -1).
261 { /* Record variable (non-fixed structure) */
275 { /* Field in record */
279 int recparentno; /* dno of parent record */
284 { /* Element of array variable */
287 PLpgSQL_expr *subscript;
288 int arrayparentno; /* dno of parent array variable */
293 { /* Positional argument to trigger */
296 PLpgSQL_expr *argnum;
301 { /* Item in the compilers namestack */
308 /* XXX: consider adapting this to use List */
309 typedef struct PLpgSQL_ns
310 { /* Compiler namestack level */
313 PLpgSQL_nsitem **items;
314 struct PLpgSQL_ns *upper;
319 { /* Generic execution node */
325 typedef struct PLpgSQL_condition
326 { /* One EXCEPTION condition name */
327 int sqlerrstate; /* SQLSTATE code */
328 char *condname; /* condition name (for debugging) */
329 struct PLpgSQL_condition *next;
336 List *exc_list; /* List of WHEN clauses */
337 } PLpgSQL_exception_block;
340 { /* One EXCEPTION ... WHEN clause */
342 PLpgSQL_condition *conditions;
343 List *action; /* List of statements */
348 { /* Block of statements */
352 List *body; /* List of statements */
355 PLpgSQL_exception_block *exceptions;
356 } PLpgSQL_stmt_block;
360 { /* Assign statement */
365 } PLpgSQL_stmt_assign;
368 { /* PERFORM statement */
372 } PLpgSQL_stmt_perform;
375 { /* Get Diagnostics item */
376 int kind; /* id for diagnostic value desired */
377 int target; /* where to assign it */
381 { /* Get Diagnostics statement */
384 List *diag_items; /* List of PLpgSQL_diag_item */
385 } PLpgSQL_stmt_getdiag;
393 List *true_body; /* List of statements */
394 List *false_body; /* List of statements */
398 typedef struct /* CASE statement */
402 PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
403 int t_varno; /* var to store test expression value into */
404 List *case_when_list; /* List of PLpgSQL_case_when structs */
405 bool have_else; /* flag needed because list could be empty */
406 List *else_stmts; /* List of statements */
409 typedef struct /* one arm of CASE statement */
412 PLpgSQL_expr *expr; /* boolean expression for this case */
413 List *stmts; /* List of statements */
418 { /* Unconditional LOOP statement */
422 List *body; /* List of statements */
427 { /* WHILE cond LOOP statement */
432 List *body; /* List of statements */
433 } PLpgSQL_stmt_while;
437 { /* FOR statement with integer loopvar */
444 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
446 List *body; /* List of statements */
451 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
452 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
453 * and PLpgSQL_dynfors.
462 List *body; /* List of statements */
466 { /* FOR statement running over SELECT */
472 List *body; /* List of statements */
473 /* end of fields that must match PLpgSQL_stmt_forq */
478 { /* FOR statement running over cursor */
484 List *body; /* List of statements */
485 /* end of fields that must match PLpgSQL_stmt_forq */
487 PLpgSQL_expr *argquery; /* cursor arguments if any */
491 { /* FOR statement running over EXECUTE */
497 List *body; /* List of statements */
498 /* end of fields that must match PLpgSQL_stmt_forq */
500 List *params; /* USING expressions */
501 } PLpgSQL_stmt_dynfors;
505 { /* OPEN a curvar */
510 PLpgSQL_row *returntype;
511 PLpgSQL_expr *argquery;
513 PLpgSQL_expr *dynquery;
518 { /* FETCH or MOVE statement */
521 PLpgSQL_rec *rec; /* target, as record or row */
523 int curvar; /* cursor variable to fetch from */
524 FetchDirection direction; /* fetch direction */
525 int how_many; /* count, if constant (expr is NULL) */
526 PLpgSQL_expr *expr; /* count, if expression */
527 bool is_move; /* is this a fetch or move? */
528 } PLpgSQL_stmt_fetch;
536 } PLpgSQL_stmt_close;
540 { /* EXIT or CONTINUE statement */
543 bool is_exit; /* Is this an exit or a continue? */
544 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
550 { /* RETURN statement */
555 } PLpgSQL_stmt_return;
558 { /* RETURN NEXT statement */
563 } PLpgSQL_stmt_return_next;
566 { /* RETURN QUERY statement */
569 PLpgSQL_expr *query; /* if static query */
570 PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
571 List *params; /* USING arguments for dynamic query */
572 } PLpgSQL_stmt_return_query;
575 { /* RAISE statement */
579 char *condname; /* condition name, SQLSTATE, or NULL */
580 char *message; /* old-style message format literal, or NULL */
581 List *params; /* list of expressions for old-style message */
582 List *options; /* list of PLpgSQL_raise_option */
583 } PLpgSQL_stmt_raise;
586 { /* RAISE statement option */
589 } PLpgSQL_raise_option;
593 { /* Generic SQL statement to execute */
596 PLpgSQL_expr *sqlstmt;
597 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
598 /* note: mod_stmt is set when we plan the query */
599 bool into; /* INTO supplied? */
600 bool strict; /* INTO STRICT flag */
601 PLpgSQL_rec *rec; /* INTO target, if record */
602 PLpgSQL_row *row; /* INTO target, if row */
603 } PLpgSQL_stmt_execsql;
607 { /* Dynamic SQL string to execute */
610 PLpgSQL_expr *query; /* string expression */
611 bool into; /* INTO supplied? */
612 bool strict; /* INTO STRICT flag */
613 PLpgSQL_rec *rec; /* INTO target, if record */
614 PLpgSQL_row *row; /* INTO target, if row */
615 List *params; /* USING expressions */
616 } PLpgSQL_stmt_dynexecute;
619 typedef struct PLpgSQL_func_hashkey
620 { /* Hash lookup key for functions */
623 bool isTrigger; /* true if called as a trigger */
625 /* be careful that pad bytes in this struct get zeroed! */
628 * For a trigger function, the OID of the relation triggered on is part of
629 * the hashkey --- we want to compile the trigger separately for each
630 * relation it is used with, in case the rowtype is different. Zero if
631 * not called as a trigger.
636 * We include actual argument types in the hash key to support polymorphic
637 * PLpgSQL functions. Be careful that extra positions are zeroed!
639 Oid argtypes[FUNC_MAX_ARGS];
640 } PLpgSQL_func_hashkey;
643 typedef struct PLpgSQL_function
644 { /* Complete compiled function */
647 TransactionId fn_xmin;
648 ItemPointerData fn_tid;
650 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
651 MemoryContext fn_cxt;
656 FmgrInfo fn_retinput;
657 Oid fn_rettypioparam;
663 int fn_argvarnos[FUNC_MAX_ARGS];
673 int tg_relname_varno;
674 int tg_table_name_varno;
675 int tg_table_schema_varno;
679 PLpgSQL_datum **datums;
680 PLpgSQL_stmt_block *action;
682 unsigned long use_count;
687 { /* Runtime execution data */
690 Oid rettype; /* type of current retval */
692 Oid fn_rettype; /* info about declared function rettype */
698 TupleDesc rettupdesc;
699 char *exitlabel; /* the "target" label of the current EXIT or
700 * CONTINUE stmt, if any */
702 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
703 MemoryContext tuple_store_cxt;
711 PLpgSQL_datum **datums;
713 /* temporary state for results from evaluation of query or expr */
714 SPITupleTable *eval_tuptable;
715 uint32 eval_processed;
717 ExprContext *eval_econtext; /* for executing simple expressions */
718 EState *eval_estate; /* EState containing eval_econtext */
719 long int eval_estate_simple_id; /* ID for eval_estate */
721 /* status information for error context reporting */
722 PLpgSQL_function *err_func; /* current func */
723 PLpgSQL_stmt *err_stmt; /* current stmt */
724 const char *err_text; /* additional state info */
725 void *plugin_info; /* reserved for use by optional plugin */
730 * A PLpgSQL_plugin structure represents an instrumentation plugin.
731 * To instrument PL/pgSQL, a plugin library must access the rendezvous
732 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
733 * Typically the struct could just be static data in the plugin library.
734 * We expect that a plugin would do this at library load time (_PG_init()).
735 * It must also be careful to set the rendezvous variable back to NULL
736 * if it is unloaded (_PG_fini()).
738 * This structure is basically a collection of function pointers --- at
739 * various interesting points in pl_exec.c, we call these functions
740 * (if the pointers are non-NULL) to give the plugin a chance to watch
743 * func_setup is called when we start a function, before we've initialized
744 * the local variables defined by the function.
746 * func_beg is called when we start a function, after we've initialized
747 * the local variables.
749 * func_end is called at the end of a function.
751 * stmt_beg and stmt_end are called before and after (respectively) each
754 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
755 * error_callback and assign_expr fields with pointers to its own
756 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
757 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
762 /* Function pointers set up by the plugin */
763 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
764 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
765 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
766 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
767 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
769 /* Function pointers set by PL/pgSQL itself */
770 void (*error_callback) (void *arg);
771 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
776 /**********************************************************************
777 * Global variable declarations
778 **********************************************************************/
780 extern bool plpgsql_DumpExecTree;
781 extern bool plpgsql_SpaceScanned;
782 extern int plpgsql_nDatums;
783 extern PLpgSQL_datum **plpgsql_Datums;
785 extern int plpgsql_error_lineno;
786 extern char *plpgsql_error_funcname;
788 /* linkage to the real yytext variable */
789 extern char *plpgsql_base_yytext;
791 #define yytext plpgsql_base_yytext
793 extern PLpgSQL_function *plpgsql_curr_compile;
794 extern bool plpgsql_check_syntax;
795 extern MemoryContext compile_tmp_cxt;
797 extern PLpgSQL_plugin **plugin_ptr;
799 /**********************************************************************
800 * Function declarations
801 **********************************************************************/
804 * Functions in pl_comp.c
807 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
809 extern int plpgsql_parse_word(const char *word);
810 extern int plpgsql_parse_dblword(const char *word);
811 extern int plpgsql_parse_tripword(const char *word);
812 extern int plpgsql_parse_wordtype(char *word);
813 extern int plpgsql_parse_dblwordtype(char *word);
814 extern int plpgsql_parse_tripwordtype(char *word);
815 extern int plpgsql_parse_wordrowtype(char *word);
816 extern int plpgsql_parse_dblwordrowtype(char *word);
817 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
818 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
819 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
822 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
824 extern int plpgsql_recognize_err_condition(const char *condname,
825 bool allow_sqlstate);
826 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
827 extern void plpgsql_adddatum(PLpgSQL_datum *new);
828 extern int plpgsql_add_initdatums(int **varnos);
829 extern void plpgsql_HashTableInit(void);
830 extern void plpgsql_compile_error_callback(void *arg);
833 * Functions in pl_handler.c
836 extern void _PG_init(void);
837 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
838 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
841 * Functions in pl_exec.c
844 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
845 FunctionCallInfo fcinfo);
846 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
847 TriggerData *trigdata);
848 extern void plpgsql_xact_cb(XactEvent event, void *arg);
849 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
850 SubTransactionId parentSubid, void *arg);
853 * Functions for the dynamic string handling in pl_funcs.c
856 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
857 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
858 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
859 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
860 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
863 * Functions for namestack handling in pl_funcs.c
866 extern void plpgsql_ns_init(void);
867 extern bool plpgsql_ns_setlocal(bool flag);
868 extern void plpgsql_ns_push(const char *label);
869 extern void plpgsql_ns_pop(void);
870 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
871 extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
872 const char *name3, int *names_used);
873 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
874 extern void plpgsql_ns_rename(char *oldname, char *newname);
877 * Other functions in pl_funcs.c
880 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
881 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
882 extern void plpgsql_dumptree(PLpgSQL_function *func);
885 * Externs in gram.y and scan.l
888 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
889 extern int plpgsql_yyparse(void);
890 extern int plpgsql_base_yylex(void);
891 extern int plpgsql_yylex(void);
892 extern void plpgsql_push_back_token(int token);
893 extern void plpgsql_yyerror(const char *message);
894 extern int plpgsql_scanner_lineno(void);
895 extern void plpgsql_scanner_init(const char *str, int functype);
896 extern void plpgsql_scanner_finish(void);
897 extern char *plpgsql_get_string_value(void);
899 #endif /* PLPGSQL_H */