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.112 2009/04/19 21:50:09 tgl 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 { /* Dynamic string control structure */
153 int used; /* Including NUL terminator */
159 { /* Postgres data type */
160 char *typname; /* (simple) name of the type */
161 Oid typoid; /* OID of the data type */
162 int ttype; /* PLPGSQL_TTYPE_ code */
163 int16 typlen; /* stuff copied from its pg_type entry */
167 FmgrInfo typinput; /* lookup info for typinput function */
168 int32 atttypmod; /* typmod (taken from someplace else) */
173 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
174 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
178 { /* Generic datum array item */
184 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
188 { /* Scalar or composite variable */
195 typedef struct PLpgSQL_expr
196 { /* SQL Query to plan and execute */
202 /* fields for "simple expression" fast-path execution: */
203 Expr *expr_simple_expr; /* NULL means not a simple expr */
204 int expr_simple_generation; /* plancache generation we checked */
205 Oid expr_simple_type; /* result type Oid, if simple */
208 * if expr is simple AND prepared in current transaction,
209 * expr_simple_state is valid. Test validity by seeing if expr_simple_lxid
210 * matches current LXID.
212 ExprState *expr_simple_state;
213 LocalTransactionId expr_simple_lxid;
215 /* params to pass to expr */
217 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
222 { /* Scalar variable */
228 PLpgSQL_type *datatype;
231 PLpgSQL_expr *default_val;
232 PLpgSQL_expr *cursor_explicit_expr;
233 int cursor_explicit_argrow;
249 TupleDesc rowtupdesc;
252 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
254 * Note: if the underlying rowtype contains a dropped column, the
255 * corresponding fieldnames[] entry will be NULL, and there is no
256 * corresponding var (varnos[] will be -1).
265 { /* Record variable (non-fixed structure) */
279 { /* Field in record */
283 int recparentno; /* dno of parent record */
288 { /* Element of array variable */
291 PLpgSQL_expr *subscript;
292 int arrayparentno; /* dno of parent array variable */
297 { /* Positional argument to trigger */
300 PLpgSQL_expr *argnum;
305 { /* Item in the compilers namestack */
312 /* XXX: consider adapting this to use List */
313 typedef struct PLpgSQL_ns
314 { /* Compiler namestack level */
317 PLpgSQL_nsitem **items;
318 struct PLpgSQL_ns *upper;
323 { /* Generic execution node */
329 typedef struct PLpgSQL_condition
330 { /* One EXCEPTION condition name */
331 int sqlerrstate; /* SQLSTATE code */
332 char *condname; /* condition name (for debugging) */
333 struct PLpgSQL_condition *next;
340 List *exc_list; /* List of WHEN clauses */
341 } PLpgSQL_exception_block;
344 { /* One EXCEPTION ... WHEN clause */
346 PLpgSQL_condition *conditions;
347 List *action; /* List of statements */
352 { /* Block of statements */
356 List *body; /* List of statements */
359 PLpgSQL_exception_block *exceptions;
360 } PLpgSQL_stmt_block;
364 { /* Assign statement */
369 } PLpgSQL_stmt_assign;
372 { /* PERFORM statement */
376 } PLpgSQL_stmt_perform;
379 { /* Get Diagnostics item */
380 int kind; /* id for diagnostic value desired */
381 int target; /* where to assign it */
385 { /* Get Diagnostics statement */
388 List *diag_items; /* List of PLpgSQL_diag_item */
389 } PLpgSQL_stmt_getdiag;
397 List *true_body; /* List of statements */
398 List *false_body; /* List of statements */
402 typedef struct /* CASE statement */
406 PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
407 int t_varno; /* var to store test expression value into */
408 List *case_when_list; /* List of PLpgSQL_case_when structs */
409 bool have_else; /* flag needed because list could be empty */
410 List *else_stmts; /* List of statements */
413 typedef struct /* one arm of CASE statement */
416 PLpgSQL_expr *expr; /* boolean expression for this case */
417 List *stmts; /* List of statements */
422 { /* Unconditional LOOP statement */
426 List *body; /* List of statements */
431 { /* WHILE cond LOOP statement */
436 List *body; /* List of statements */
437 } PLpgSQL_stmt_while;
441 { /* FOR statement with integer loopvar */
448 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
450 List *body; /* List of statements */
455 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
456 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
457 * and PLpgSQL_dynfors.
466 List *body; /* List of statements */
470 { /* FOR statement running over SELECT */
476 List *body; /* List of statements */
477 /* end of fields that must match PLpgSQL_stmt_forq */
482 { /* FOR statement running over cursor */
488 List *body; /* List of statements */
489 /* end of fields that must match PLpgSQL_stmt_forq */
491 PLpgSQL_expr *argquery; /* cursor arguments if any */
495 { /* FOR statement running over EXECUTE */
501 List *body; /* List of statements */
502 /* end of fields that must match PLpgSQL_stmt_forq */
504 List *params; /* USING expressions */
505 } PLpgSQL_stmt_dynfors;
509 { /* OPEN a curvar */
514 PLpgSQL_row *returntype;
515 PLpgSQL_expr *argquery;
517 PLpgSQL_expr *dynquery;
522 { /* FETCH or MOVE statement */
525 PLpgSQL_rec *rec; /* target, as record or row */
527 int curvar; /* cursor variable to fetch from */
528 FetchDirection direction; /* fetch direction */
529 int how_many; /* count, if constant (expr is NULL) */
530 PLpgSQL_expr *expr; /* count, if expression */
531 bool is_move; /* is this a fetch or move? */
532 } PLpgSQL_stmt_fetch;
540 } PLpgSQL_stmt_close;
544 { /* EXIT or CONTINUE statement */
547 bool is_exit; /* Is this an exit or a continue? */
548 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
554 { /* RETURN statement */
559 } PLpgSQL_stmt_return;
562 { /* RETURN NEXT statement */
567 } PLpgSQL_stmt_return_next;
570 { /* RETURN QUERY statement */
573 PLpgSQL_expr *query; /* if static query */
574 PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
575 List *params; /* USING arguments for dynamic query */
576 } PLpgSQL_stmt_return_query;
579 { /* RAISE statement */
583 char *condname; /* condition name, SQLSTATE, or NULL */
584 char *message; /* old-style message format literal, or NULL */
585 List *params; /* list of expressions for old-style message */
586 List *options; /* list of PLpgSQL_raise_option */
587 } PLpgSQL_stmt_raise;
590 { /* RAISE statement option */
593 } PLpgSQL_raise_option;
597 { /* Generic SQL statement to execute */
600 PLpgSQL_expr *sqlstmt;
601 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
602 /* note: mod_stmt is set when we plan the query */
603 bool into; /* INTO supplied? */
604 bool strict; /* INTO STRICT flag */
605 PLpgSQL_rec *rec; /* INTO target, if record */
606 PLpgSQL_row *row; /* INTO target, if row */
607 } PLpgSQL_stmt_execsql;
611 { /* Dynamic SQL string to execute */
614 PLpgSQL_expr *query; /* string expression */
615 bool into; /* INTO supplied? */
616 bool strict; /* INTO STRICT flag */
617 PLpgSQL_rec *rec; /* INTO target, if record */
618 PLpgSQL_row *row; /* INTO target, if row */
619 List *params; /* USING expressions */
620 } PLpgSQL_stmt_dynexecute;
623 typedef struct PLpgSQL_func_hashkey
624 { /* Hash lookup key for functions */
627 bool isTrigger; /* true if called as a trigger */
629 /* be careful that pad bytes in this struct get zeroed! */
632 * For a trigger function, the OID of the relation triggered on is part of
633 * the hashkey --- we want to compile the trigger separately for each
634 * relation it is used with, in case the rowtype is different. Zero if
635 * not called as a trigger.
640 * We include actual argument types in the hash key to support polymorphic
641 * PLpgSQL functions. Be careful that extra positions are zeroed!
643 Oid argtypes[FUNC_MAX_ARGS];
644 } PLpgSQL_func_hashkey;
647 typedef struct PLpgSQL_function
648 { /* Complete compiled function */
651 TransactionId fn_xmin;
652 ItemPointerData fn_tid;
654 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
655 MemoryContext fn_cxt;
660 FmgrInfo fn_retinput;
661 Oid fn_rettypioparam;
667 int fn_argvarnos[FUNC_MAX_ARGS];
677 int tg_relname_varno;
678 int tg_table_name_varno;
679 int tg_table_schema_varno;
683 PLpgSQL_datum **datums;
684 PLpgSQL_stmt_block *action;
686 unsigned long use_count;
691 { /* Runtime execution data */
694 Oid rettype; /* type of current retval */
696 Oid fn_rettype; /* info about declared function rettype */
702 TupleDesc rettupdesc;
703 char *exitlabel; /* the "target" label of the current EXIT or
704 * CONTINUE stmt, if any */
706 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
707 MemoryContext tuple_store_cxt;
715 PLpgSQL_datum **datums;
717 /* temporary state for results from evaluation of query or expr */
718 SPITupleTable *eval_tuptable;
719 uint32 eval_processed;
721 ExprContext *eval_econtext; /* for executing simple expressions */
723 /* status information for error context reporting */
724 PLpgSQL_function *err_func; /* current func */
725 PLpgSQL_stmt *err_stmt; /* current stmt */
726 const char *err_text; /* additional state info */
727 void *plugin_info; /* reserved for use by optional plugin */
732 * A PLpgSQL_plugin structure represents an instrumentation plugin.
733 * To instrument PL/pgSQL, a plugin library must access the rendezvous
734 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
735 * Typically the struct could just be static data in the plugin library.
736 * We expect that a plugin would do this at library load time (_PG_init()).
737 * It must also be careful to set the rendezvous variable back to NULL
738 * if it is unloaded (_PG_fini()).
740 * This structure is basically a collection of function pointers --- at
741 * various interesting points in pl_exec.c, we call these functions
742 * (if the pointers are non-NULL) to give the plugin a chance to watch
745 * func_setup is called when we start a function, before we've initialized
746 * the local variables defined by the function.
748 * func_beg is called when we start a function, after we've initialized
749 * the local variables.
751 * func_end is called at the end of a function.
753 * stmt_beg and stmt_end are called before and after (respectively) each
756 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
757 * error_callback and assign_expr fields with pointers to its own
758 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
759 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
764 /* Function pointers set up by the plugin */
765 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
766 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
767 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
768 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
769 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
771 /* Function pointers set by PL/pgSQL itself */
772 void (*error_callback) (void *arg);
773 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
778 /**********************************************************************
779 * Global variable declarations
780 **********************************************************************/
782 extern bool plpgsql_DumpExecTree;
783 extern bool plpgsql_SpaceScanned;
784 extern int plpgsql_nDatums;
785 extern PLpgSQL_datum **plpgsql_Datums;
787 extern int plpgsql_error_lineno;
788 extern char *plpgsql_error_funcname;
790 /* linkage to the real yytext variable */
791 extern char *plpgsql_base_yytext;
793 #define yytext plpgsql_base_yytext
795 extern PLpgSQL_function *plpgsql_curr_compile;
796 extern bool plpgsql_check_syntax;
797 extern MemoryContext compile_tmp_cxt;
799 extern PLpgSQL_plugin **plugin_ptr;
801 /**********************************************************************
802 * Function declarations
803 **********************************************************************/
806 * Functions in pl_comp.c
809 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
811 extern int plpgsql_parse_word(const char *word);
812 extern int plpgsql_parse_dblword(const char *word);
813 extern int plpgsql_parse_tripword(const char *word);
814 extern int plpgsql_parse_wordtype(char *word);
815 extern int plpgsql_parse_dblwordtype(char *word);
816 extern int plpgsql_parse_tripwordtype(char *word);
817 extern int plpgsql_parse_wordrowtype(char *word);
818 extern int plpgsql_parse_dblwordrowtype(char *word);
819 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
820 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
821 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
824 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
826 extern int plpgsql_recognize_err_condition(const char *condname,
827 bool allow_sqlstate);
828 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
829 extern void plpgsql_adddatum(PLpgSQL_datum *new);
830 extern int plpgsql_add_initdatums(int **varnos);
831 extern void plpgsql_HashTableInit(void);
832 extern void plpgsql_compile_error_callback(void *arg);
835 * Functions in pl_handler.c
838 extern void _PG_init(void);
839 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
840 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
843 * Functions in pl_exec.c
846 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
847 FunctionCallInfo fcinfo);
848 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
849 TriggerData *trigdata);
850 extern void plpgsql_xact_cb(XactEvent event, void *arg);
851 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
852 SubTransactionId parentSubid, void *arg);
855 * Functions for the dynamic string handling in pl_funcs.c
858 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
859 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
860 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
861 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
862 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
865 * Functions for namestack handling in pl_funcs.c
868 extern void plpgsql_ns_init(void);
869 extern bool plpgsql_ns_setlocal(bool flag);
870 extern void plpgsql_ns_push(const char *label);
871 extern void plpgsql_ns_pop(void);
872 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
873 extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
874 const char *name3, int *names_used);
875 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
876 extern void plpgsql_ns_rename(char *oldname, char *newname);
879 * Other functions in pl_funcs.c
882 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
883 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
884 extern void plpgsql_dumptree(PLpgSQL_function *func);
887 * Externs in gram.y and scan.l
890 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
891 extern int plpgsql_yyparse(void);
892 extern int plpgsql_base_yylex(void);
893 extern int plpgsql_yylex(void);
894 extern void plpgsql_push_back_token(int token);
895 extern void plpgsql_yyerror(const char *message);
896 extern int plpgsql_scanner_lineno(void);
897 extern void plpgsql_scanner_init(const char *str);
898 extern void plpgsql_scanner_finish(void);
900 #endif /* PLPGSQL_H */