1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2009, 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.114 2009/07/22 02:31:38 joe Exp $
13 *-------------------------------------------------------------------------
21 #include "access/xact.h"
23 #include "miscadmin.h"
24 #include "commands/trigger.h"
25 #include "executor/spi.h"
26 #include "utils/tuplestore.h"
28 /**********************************************************************
30 **********************************************************************/
32 /* define our text domain for translations */
34 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
37 #define _(x) dgettext(TEXTDOMAIN, x)
40 * Compiler's namestack item types
52 * Datum array node types
60 PLPGSQL_DTYPE_RECFIELD,
61 PLPGSQL_DTYPE_ARRAYELEM,
67 * Variants distinguished in PLpgSQL_type structs
72 PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
73 PLPGSQL_TTYPE_ROW, /* composite types */
74 PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
75 PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
79 * Execution tree node types
82 enum PLpgSQL_stmt_types
95 PLPGSQL_STMT_RETURN_NEXT,
96 PLPGSQL_STMT_RETURN_QUERY,
99 PLPGSQL_STMT_DYNEXECUTE,
100 PLPGSQL_STMT_DYNFORS,
101 PLPGSQL_STMT_GETDIAG,
110 * Execution node return codes
123 * GET DIAGNOSTICS system attrs
128 PLPGSQL_GETDIAG_ROW_COUNT,
129 PLPGSQL_GETDIAG_RESULT_OID
133 * RAISE statement options
138 PLPGSQL_RAISEOPTION_ERRCODE,
139 PLPGSQL_RAISEOPTION_MESSAGE,
140 PLPGSQL_RAISEOPTION_DETAIL,
141 PLPGSQL_RAISEOPTION_HINT
145 /**********************************************************************
146 * Node and structure definitions
147 **********************************************************************/
151 { /* Postgres data type */
152 char *typname; /* (simple) name of the type */
153 Oid typoid; /* OID of the data type */
154 int ttype; /* PLPGSQL_TTYPE_ code */
155 int16 typlen; /* stuff copied from its pg_type entry */
159 FmgrInfo typinput; /* lookup info for typinput function */
160 int32 atttypmod; /* typmod (taken from someplace else) */
165 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
166 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
170 { /* Generic datum array item */
176 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
180 { /* Scalar or composite variable */
187 typedef struct PLpgSQL_expr
188 { /* SQL Query to plan and execute */
194 /* fields for "simple expression" fast-path execution: */
195 Expr *expr_simple_expr; /* NULL means not a simple expr */
196 int expr_simple_generation; /* plancache generation we checked */
197 Oid expr_simple_type; /* result type Oid, if simple */
200 * if expr is simple AND prepared in current transaction,
201 * expr_simple_state is valid. Test validity by seeing if expr_simple_lxid
202 * matches current LXID.
204 ExprState *expr_simple_state;
205 LocalTransactionId expr_simple_lxid;
207 /* params to pass to expr */
209 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
214 { /* Scalar variable */
220 PLpgSQL_type *datatype;
223 PLpgSQL_expr *default_val;
224 PLpgSQL_expr *cursor_explicit_expr;
225 int cursor_explicit_argrow;
241 TupleDesc rowtupdesc;
244 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
246 * Note: if the underlying rowtype contains a dropped column, the
247 * corresponding fieldnames[] entry will be NULL, and there is no
248 * corresponding var (varnos[] will be -1).
257 { /* Record variable (non-fixed structure) */
271 { /* Field in record */
275 int recparentno; /* dno of parent record */
280 { /* Element of array variable */
283 PLpgSQL_expr *subscript;
284 int arrayparentno; /* dno of parent array variable */
289 { /* Positional argument to trigger */
292 PLpgSQL_expr *argnum;
297 { /* Item in the compilers namestack */
304 /* XXX: consider adapting this to use List */
305 typedef struct PLpgSQL_ns
306 { /* Compiler namestack level */
309 PLpgSQL_nsitem **items;
310 struct PLpgSQL_ns *upper;
315 { /* Generic execution node */
321 typedef struct PLpgSQL_condition
322 { /* One EXCEPTION condition name */
323 int sqlerrstate; /* SQLSTATE code */
324 char *condname; /* condition name (for debugging) */
325 struct PLpgSQL_condition *next;
332 List *exc_list; /* List of WHEN clauses */
333 } PLpgSQL_exception_block;
336 { /* One EXCEPTION ... WHEN clause */
338 PLpgSQL_condition *conditions;
339 List *action; /* List of statements */
344 { /* Block of statements */
348 List *body; /* List of statements */
351 PLpgSQL_exception_block *exceptions;
352 } PLpgSQL_stmt_block;
356 { /* Assign statement */
361 } PLpgSQL_stmt_assign;
364 { /* PERFORM statement */
368 } PLpgSQL_stmt_perform;
371 { /* Get Diagnostics item */
372 int kind; /* id for diagnostic value desired */
373 int target; /* where to assign it */
377 { /* Get Diagnostics statement */
380 List *diag_items; /* List of PLpgSQL_diag_item */
381 } PLpgSQL_stmt_getdiag;
389 List *true_body; /* List of statements */
390 List *false_body; /* List of statements */
394 typedef struct /* CASE statement */
398 PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
399 int t_varno; /* var to store test expression value into */
400 List *case_when_list; /* List of PLpgSQL_case_when structs */
401 bool have_else; /* flag needed because list could be empty */
402 List *else_stmts; /* List of statements */
405 typedef struct /* one arm of CASE statement */
408 PLpgSQL_expr *expr; /* boolean expression for this case */
409 List *stmts; /* List of statements */
414 { /* Unconditional LOOP statement */
418 List *body; /* List of statements */
423 { /* WHILE cond LOOP statement */
428 List *body; /* List of statements */
429 } PLpgSQL_stmt_while;
433 { /* FOR statement with integer loopvar */
440 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
442 List *body; /* List of statements */
447 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
448 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
449 * and PLpgSQL_dynfors.
458 List *body; /* List of statements */
462 { /* FOR statement running over SELECT */
468 List *body; /* List of statements */
469 /* end of fields that must match PLpgSQL_stmt_forq */
474 { /* FOR statement running over cursor */
480 List *body; /* List of statements */
481 /* end of fields that must match PLpgSQL_stmt_forq */
483 PLpgSQL_expr *argquery; /* cursor arguments if any */
487 { /* FOR statement running over EXECUTE */
493 List *body; /* List of statements */
494 /* end of fields that must match PLpgSQL_stmt_forq */
496 List *params; /* USING expressions */
497 } PLpgSQL_stmt_dynfors;
501 { /* OPEN a curvar */
506 PLpgSQL_row *returntype;
507 PLpgSQL_expr *argquery;
509 PLpgSQL_expr *dynquery;
514 { /* FETCH or MOVE statement */
517 PLpgSQL_rec *rec; /* target, as record or row */
519 int curvar; /* cursor variable to fetch from */
520 FetchDirection direction; /* fetch direction */
521 int how_many; /* count, if constant (expr is NULL) */
522 PLpgSQL_expr *expr; /* count, if expression */
523 bool is_move; /* is this a fetch or move? */
524 } PLpgSQL_stmt_fetch;
532 } PLpgSQL_stmt_close;
536 { /* EXIT or CONTINUE statement */
539 bool is_exit; /* Is this an exit or a continue? */
540 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
546 { /* RETURN statement */
551 } PLpgSQL_stmt_return;
554 { /* RETURN NEXT statement */
559 } PLpgSQL_stmt_return_next;
562 { /* RETURN QUERY statement */
565 PLpgSQL_expr *query; /* if static query */
566 PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
567 List *params; /* USING arguments for dynamic query */
568 } PLpgSQL_stmt_return_query;
571 { /* RAISE statement */
575 char *condname; /* condition name, SQLSTATE, or NULL */
576 char *message; /* old-style message format literal, or NULL */
577 List *params; /* list of expressions for old-style message */
578 List *options; /* list of PLpgSQL_raise_option */
579 } PLpgSQL_stmt_raise;
582 { /* RAISE statement option */
585 } PLpgSQL_raise_option;
589 { /* Generic SQL statement to execute */
592 PLpgSQL_expr *sqlstmt;
593 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
594 /* note: mod_stmt is set when we plan the query */
595 bool into; /* INTO supplied? */
596 bool strict; /* INTO STRICT flag */
597 PLpgSQL_rec *rec; /* INTO target, if record */
598 PLpgSQL_row *row; /* INTO target, if row */
599 } PLpgSQL_stmt_execsql;
603 { /* Dynamic SQL string to execute */
606 PLpgSQL_expr *query; /* string expression */
607 bool into; /* INTO supplied? */
608 bool strict; /* INTO STRICT flag */
609 PLpgSQL_rec *rec; /* INTO target, if record */
610 PLpgSQL_row *row; /* INTO target, if row */
611 List *params; /* USING expressions */
612 } PLpgSQL_stmt_dynexecute;
615 typedef struct PLpgSQL_func_hashkey
616 { /* Hash lookup key for functions */
619 bool isTrigger; /* true if called as a trigger */
621 /* be careful that pad bytes in this struct get zeroed! */
624 * For a trigger function, the OID of the relation triggered on is part of
625 * the hashkey --- we want to compile the trigger separately for each
626 * relation it is used with, in case the rowtype is different. Zero if
627 * not called as a trigger.
632 * We include actual argument types in the hash key to support polymorphic
633 * PLpgSQL functions. Be careful that extra positions are zeroed!
635 Oid argtypes[FUNC_MAX_ARGS];
636 } PLpgSQL_func_hashkey;
639 typedef struct PLpgSQL_function
640 { /* Complete compiled function */
643 TransactionId fn_xmin;
644 ItemPointerData fn_tid;
646 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
647 MemoryContext fn_cxt;
652 FmgrInfo fn_retinput;
653 Oid fn_rettypioparam;
659 int fn_argvarnos[FUNC_MAX_ARGS];
669 int tg_relname_varno;
670 int tg_table_name_varno;
671 int tg_table_schema_varno;
675 PLpgSQL_datum **datums;
676 PLpgSQL_stmt_block *action;
678 unsigned long use_count;
683 { /* Runtime execution data */
686 Oid rettype; /* type of current retval */
688 Oid fn_rettype; /* info about declared function rettype */
694 TupleDesc rettupdesc;
695 char *exitlabel; /* the "target" label of the current EXIT or
696 * CONTINUE stmt, if any */
698 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
699 MemoryContext tuple_store_cxt;
707 PLpgSQL_datum **datums;
709 /* temporary state for results from evaluation of query or expr */
710 SPITupleTable *eval_tuptable;
711 uint32 eval_processed;
713 ExprContext *eval_econtext; /* for executing simple expressions */
715 /* status information for error context reporting */
716 PLpgSQL_function *err_func; /* current func */
717 PLpgSQL_stmt *err_stmt; /* current stmt */
718 const char *err_text; /* additional state info */
719 void *plugin_info; /* reserved for use by optional plugin */
724 * A PLpgSQL_plugin structure represents an instrumentation plugin.
725 * To instrument PL/pgSQL, a plugin library must access the rendezvous
726 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
727 * Typically the struct could just be static data in the plugin library.
728 * We expect that a plugin would do this at library load time (_PG_init()).
729 * It must also be careful to set the rendezvous variable back to NULL
730 * if it is unloaded (_PG_fini()).
732 * This structure is basically a collection of function pointers --- at
733 * various interesting points in pl_exec.c, we call these functions
734 * (if the pointers are non-NULL) to give the plugin a chance to watch
737 * func_setup is called when we start a function, before we've initialized
738 * the local variables defined by the function.
740 * func_beg is called when we start a function, after we've initialized
741 * the local variables.
743 * func_end is called at the end of a function.
745 * stmt_beg and stmt_end are called before and after (respectively) each
748 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
749 * error_callback and assign_expr fields with pointers to its own
750 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
751 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
756 /* Function pointers set up by the plugin */
757 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
758 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
759 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
760 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
761 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
763 /* Function pointers set by PL/pgSQL itself */
764 void (*error_callback) (void *arg);
765 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
770 /**********************************************************************
771 * Global variable declarations
772 **********************************************************************/
774 extern bool plpgsql_DumpExecTree;
775 extern bool plpgsql_SpaceScanned;
776 extern int plpgsql_nDatums;
777 extern PLpgSQL_datum **plpgsql_Datums;
779 extern int plpgsql_error_lineno;
780 extern char *plpgsql_error_funcname;
782 /* linkage to the real yytext variable */
783 extern char *plpgsql_base_yytext;
785 #define yytext plpgsql_base_yytext
787 extern PLpgSQL_function *plpgsql_curr_compile;
788 extern bool plpgsql_check_syntax;
789 extern MemoryContext compile_tmp_cxt;
791 extern PLpgSQL_plugin **plugin_ptr;
793 /**********************************************************************
794 * Function declarations
795 **********************************************************************/
798 * Functions in pl_comp.c
801 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
803 extern int plpgsql_parse_word(const char *word);
804 extern int plpgsql_parse_dblword(const char *word);
805 extern int plpgsql_parse_tripword(const char *word);
806 extern int plpgsql_parse_wordtype(char *word);
807 extern int plpgsql_parse_dblwordtype(char *word);
808 extern int plpgsql_parse_tripwordtype(char *word);
809 extern int plpgsql_parse_wordrowtype(char *word);
810 extern int plpgsql_parse_dblwordrowtype(char *word);
811 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
812 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
813 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
816 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
818 extern int plpgsql_recognize_err_condition(const char *condname,
819 bool allow_sqlstate);
820 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
821 extern void plpgsql_adddatum(PLpgSQL_datum *new);
822 extern int plpgsql_add_initdatums(int **varnos);
823 extern void plpgsql_HashTableInit(void);
824 extern void plpgsql_compile_error_callback(void *arg);
827 * Functions in pl_handler.c
830 extern void _PG_init(void);
831 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
832 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
835 * Functions in pl_exec.c
838 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
839 FunctionCallInfo fcinfo);
840 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
841 TriggerData *trigdata);
842 extern void plpgsql_xact_cb(XactEvent event, void *arg);
843 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
844 SubTransactionId parentSubid, void *arg);
847 * Functions for namestack handling in pl_funcs.c
850 extern void plpgsql_ns_init(void);
851 extern bool plpgsql_ns_setlocal(bool flag);
852 extern void plpgsql_ns_push(const char *label);
853 extern void plpgsql_ns_pop(void);
854 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
855 extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
856 const char *name3, int *names_used);
857 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
858 extern void plpgsql_ns_rename(char *oldname, char *newname);
861 * Other functions in pl_funcs.c
864 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
865 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
866 extern void plpgsql_dumptree(PLpgSQL_function *func);
869 * Externs in gram.y and scan.l
872 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
873 extern int plpgsql_yyparse(void);
874 extern int plpgsql_base_yylex(void);
875 extern int plpgsql_yylex(void);
876 extern void plpgsql_push_back_token(int token);
877 extern void plpgsql_yyerror(const char *message);
878 extern int plpgsql_scanner_lineno(void);
879 extern void plpgsql_scanner_init(const char *str);
880 extern void plpgsql_scanner_finish(void);
882 #endif /* PLPGSQL_H */