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.97 2008/04/06 23:43:29 tgl Exp $
13 *-------------------------------------------------------------------------
22 #include "miscadmin.h"
23 #include "commands/trigger.h"
24 #include "executor/spi.h"
25 #include "utils/tuplestore.h"
27 /**********************************************************************
29 **********************************************************************/
32 * Compiler's namestack item types
44 * Datum array node types
52 PLPGSQL_DTYPE_RECFIELD,
53 PLPGSQL_DTYPE_ARRAYELEM,
59 * Variants distinguished in PLpgSQL_type structs
64 PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
65 PLPGSQL_TTYPE_ROW, /* composite types */
66 PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
67 PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
71 * Execution tree node types
86 PLPGSQL_STMT_RETURN_NEXT,
87 PLPGSQL_STMT_RETURN_QUERY,
90 PLPGSQL_STMT_DYNEXECUTE,
101 * Execution node return codes
113 * GET DIAGNOSTICS system attrs
118 PLPGSQL_GETDIAG_ROW_COUNT,
119 PLPGSQL_GETDIAG_RESULT_OID
123 /**********************************************************************
124 * Node and structure definitions
125 **********************************************************************/
129 { /* Dynamic string control structure */
131 int used; /* Including NUL terminator */
137 { /* Postgres data type */
138 char *typname; /* (simple) name of the type */
139 Oid typoid; /* OID of the data type */
140 int ttype; /* PLPGSQL_TTYPE_ code */
141 int16 typlen; /* stuff copied from its pg_type entry */
145 FmgrInfo typinput; /* lookup info for typinput function */
146 int32 atttypmod; /* typmod (taken from someplace else) */
151 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
152 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
156 { /* Generic datum array item */
162 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
166 { /* Scalar or composite variable */
173 typedef struct PLpgSQL_expr
174 { /* SQL Query to plan and execute */
180 /* fields for "simple expression" fast-path execution: */
181 Expr *expr_simple_expr; /* NULL means not a simple expr */
182 int expr_simple_generation; /* plancache generation we checked */
183 Oid expr_simple_type; /* result type Oid, if simple */
186 * if expr is simple AND prepared in current eval_estate,
187 * expr_simple_state is valid. Test validity by seeing if expr_simple_id
188 * matches eval_estate_simple_id.
190 ExprState *expr_simple_state;
191 long int expr_simple_id;
193 /* params to pass to expr */
195 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
200 { /* Scalar variable */
206 PLpgSQL_type *datatype;
209 PLpgSQL_expr *default_val;
210 PLpgSQL_expr *cursor_explicit_expr;
211 int cursor_explicit_argrow;
227 TupleDesc rowtupdesc;
230 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
232 * Note: if the underlying rowtype contains a dropped column, the
233 * corresponding fieldnames[] entry will be NULL, and there is no
234 * corresponding var (varnos[] will be -1).
243 { /* Record variable (non-fixed structure) */
257 { /* Field in record */
261 int recparentno; /* dno of parent record */
266 { /* Element of array variable */
269 PLpgSQL_expr *subscript;
270 int arrayparentno; /* dno of parent array variable */
275 { /* Positional argument to trigger */
278 PLpgSQL_expr *argnum;
283 { /* Item in the compilers namestack */
290 /* XXX: consider adapting this to use List */
291 typedef struct PLpgSQL_ns
292 { /* Compiler namestack level */
295 PLpgSQL_nsitem **items;
296 struct PLpgSQL_ns *upper;
301 { /* Generic execution node */
307 typedef struct PLpgSQL_condition
308 { /* One EXCEPTION condition name */
309 int sqlerrstate; /* SQLSTATE code */
310 char *condname; /* condition name (for debugging) */
311 struct PLpgSQL_condition *next;
318 List *exc_list; /* List of WHEN clauses */
319 } PLpgSQL_exception_block;
322 { /* One EXCEPTION ... WHEN clause */
324 PLpgSQL_condition *conditions;
325 List *action; /* List of statements */
330 { /* Block of statements */
334 List *body; /* List of statements */
337 PLpgSQL_exception_block *exceptions;
338 } PLpgSQL_stmt_block;
342 { /* Assign statement */
347 } PLpgSQL_stmt_assign;
350 { /* PERFORM statement */
354 } PLpgSQL_stmt_perform;
357 { /* Get Diagnostics item */
358 int kind; /* id for diagnostic value desired */
359 int target; /* where to assign it */
363 { /* Get Diagnostics statement */
366 List *diag_items; /* List of PLpgSQL_diag_item */
367 } PLpgSQL_stmt_getdiag;
375 List *true_body; /* List of statements */
376 List *false_body; /* List of statements */
381 { /* Unconditional LOOP statement */
385 List *body; /* List of statements */
390 { /* WHILE cond LOOP statement */
395 List *body; /* List of statements */
396 } PLpgSQL_stmt_while;
400 { /* FOR statement with integer loopvar */
407 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
409 List *body; /* List of statements */
414 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
415 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
416 * and PLpgSQL_dynfors.
425 List *body; /* List of statements */
429 { /* FOR statement running over SELECT */
435 List *body; /* List of statements */
436 /* end of fields that must match PLpgSQL_stmt_forq */
441 { /* FOR statement running over cursor */
447 List *body; /* List of statements */
448 /* end of fields that must match PLpgSQL_stmt_forq */
450 PLpgSQL_expr *argquery; /* cursor arguments if any */
454 { /* FOR statement running over EXECUTE */
460 List *body; /* List of statements */
461 /* end of fields that must match PLpgSQL_stmt_forq */
463 List *params; /* USING expressions */
464 } PLpgSQL_stmt_dynfors;
468 { /* OPEN a curvar */
473 PLpgSQL_row *returntype;
474 PLpgSQL_expr *argquery;
476 PLpgSQL_expr *dynquery;
481 { /* FETCH or MOVE statement */
484 PLpgSQL_rec *rec; /* target, as record or row */
486 int curvar; /* cursor variable to fetch from */
487 FetchDirection direction; /* fetch direction */
488 int how_many; /* count, if constant (expr is NULL) */
489 PLpgSQL_expr *expr; /* count, if expression */
490 bool is_move; /* is this a fetch or move? */
491 } PLpgSQL_stmt_fetch;
499 } PLpgSQL_stmt_close;
503 { /* EXIT or CONTINUE statement */
506 bool is_exit; /* Is this an exit or a continue? */
507 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
513 { /* RETURN statement */
518 } PLpgSQL_stmt_return;
521 { /* RETURN NEXT statement */
526 } PLpgSQL_stmt_return_next;
529 { /* RETURN QUERY statement */
533 } PLpgSQL_stmt_return_query;
536 { /* RAISE statement */
541 List *params; /* list of expressions */
542 } PLpgSQL_stmt_raise;
546 { /* Generic SQL statement to execute */
549 PLpgSQL_expr *sqlstmt;
550 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
551 /* note: mod_stmt is set when we plan the query */
552 bool into; /* INTO supplied? */
553 bool strict; /* INTO STRICT flag */
554 PLpgSQL_rec *rec; /* INTO target, if record */
555 PLpgSQL_row *row; /* INTO target, if row */
556 } PLpgSQL_stmt_execsql;
560 { /* Dynamic SQL string to execute */
563 PLpgSQL_expr *query; /* string expression */
564 bool into; /* INTO supplied? */
565 bool strict; /* INTO STRICT flag */
566 PLpgSQL_rec *rec; /* INTO target, if record */
567 PLpgSQL_row *row; /* INTO target, if row */
568 List *params; /* USING expressions */
569 } PLpgSQL_stmt_dynexecute;
572 typedef struct PLpgSQL_func_hashkey
573 { /* Hash lookup key for functions */
577 * For a trigger function, the OID of the relation triggered on is part of
578 * the hashkey --- we want to compile the trigger separately for each
579 * relation it is used with, in case the rowtype is different. Zero if
580 * not called as a trigger.
585 * We include actual argument types in the hash key to support polymorphic
586 * PLpgSQL functions. Be careful that extra positions are zeroed!
588 Oid argtypes[FUNC_MAX_ARGS];
589 } PLpgSQL_func_hashkey;
592 typedef struct PLpgSQL_function
593 { /* Complete compiled function */
596 TransactionId fn_xmin;
597 ItemPointerData fn_tid;
599 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
600 MemoryContext fn_cxt;
605 FmgrInfo fn_retinput;
606 Oid fn_rettypioparam;
612 int fn_argvarnos[FUNC_MAX_ARGS];
622 int tg_relname_varno;
623 int tg_table_name_varno;
624 int tg_table_schema_varno;
628 PLpgSQL_datum **datums;
629 PLpgSQL_stmt_block *action;
631 unsigned long use_count;
636 { /* Runtime execution data */
639 Oid rettype; /* type of current retval */
641 Oid fn_rettype; /* info about declared function rettype */
647 TupleDesc rettupdesc;
648 char *exitlabel; /* the "target" label of the current EXIT or
649 * CONTINUE stmt, if any */
651 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
652 MemoryContext tuple_store_cxt;
660 PLpgSQL_datum **datums;
662 /* temporary state for results from evaluation of query or expr */
663 SPITupleTable *eval_tuptable;
664 uint32 eval_processed;
666 ExprContext *eval_econtext; /* for executing simple expressions */
667 EState *eval_estate; /* EState containing eval_econtext */
668 long int eval_estate_simple_id; /* ID for eval_estate */
670 /* status information for error context reporting */
671 PLpgSQL_function *err_func; /* current func */
672 PLpgSQL_stmt *err_stmt; /* current stmt */
673 const char *err_text; /* additional state info */
674 void *plugin_info; /* reserved for use by optional plugin */
679 * A PLpgSQL_plugin structure represents an instrumentation plugin.
680 * To instrument PL/pgSQL, a plugin library must access the rendezvous
681 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
682 * Typically the struct could just be static data in the plugin library.
683 * We expect that a plugin would do this at library load time (_PG_init()).
684 * It must also be careful to set the rendezvous variable back to NULL
685 * if it is unloaded (_PG_fini()).
687 * This structure is basically a collection of function pointers --- at
688 * various interesting points in pl_exec.c, we call these functions
689 * (if the pointers are non-NULL) to give the plugin a chance to watch
692 * func_setup is called when we start a function, before we've initialized
693 * the local variables defined by the function.
695 * func_beg is called when we start a function, after we've initialized
696 * the local variables.
698 * func_end is called at the end of a function.
700 * stmt_beg and stmt_end are called before and after (respectively) each
703 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
704 * error_callback and assign_expr fields with pointers to its own
705 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
706 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
711 /* Function pointers set up by the plugin */
712 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
713 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
714 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
715 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
716 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
718 /* Function pointers set by PL/pgSQL itself */
719 void (*error_callback) (void *arg);
720 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
725 /**********************************************************************
726 * Global variable declarations
727 **********************************************************************/
729 extern bool plpgsql_DumpExecTree;
730 extern bool plpgsql_SpaceScanned;
731 extern int plpgsql_nDatums;
732 extern PLpgSQL_datum **plpgsql_Datums;
734 extern int plpgsql_error_lineno;
735 extern char *plpgsql_error_funcname;
737 /* linkage to the real yytext variable */
738 extern char *plpgsql_base_yytext;
740 #define yytext plpgsql_base_yytext
742 extern PLpgSQL_function *plpgsql_curr_compile;
743 extern bool plpgsql_check_syntax;
744 extern MemoryContext compile_tmp_cxt;
746 extern PLpgSQL_plugin **plugin_ptr;
748 /**********************************************************************
749 * Function declarations
750 **********************************************************************/
753 * Functions in pl_comp.c
756 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
758 extern int plpgsql_parse_word(const char *word);
759 extern int plpgsql_parse_dblword(const char *word);
760 extern int plpgsql_parse_tripword(const char *word);
761 extern int plpgsql_parse_wordtype(char *word);
762 extern int plpgsql_parse_dblwordtype(char *word);
763 extern int plpgsql_parse_tripwordtype(char *word);
764 extern int plpgsql_parse_wordrowtype(char *word);
765 extern int plpgsql_parse_dblwordrowtype(char *word);
766 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
767 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
768 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
771 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
773 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
774 extern void plpgsql_adddatum(PLpgSQL_datum *new);
775 extern int plpgsql_add_initdatums(int **varnos);
776 extern void plpgsql_HashTableInit(void);
777 extern void plpgsql_compile_error_callback(void *arg);
780 * Functions in pl_handler.c
783 extern void _PG_init(void);
784 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
785 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
788 * Functions in pl_exec.c
791 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
792 FunctionCallInfo fcinfo);
793 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
794 TriggerData *trigdata);
795 extern void plpgsql_xact_cb(XactEvent event, void *arg);
796 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
797 SubTransactionId parentSubid, void *arg);
800 * Functions for the dynamic string handling in pl_funcs.c
803 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
804 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
805 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
806 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
807 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
810 * Functions for namestack handling in pl_funcs.c
813 extern void plpgsql_ns_init(void);
814 extern bool plpgsql_ns_setlocal(bool flag);
815 extern void plpgsql_ns_push(const char *label);
816 extern void plpgsql_ns_pop(void);
817 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
818 extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
819 const char *name3, int *names_used);
820 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
821 extern void plpgsql_ns_rename(char *oldname, char *newname);
824 * Other functions in pl_funcs.c
827 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
828 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
829 extern void plpgsql_dumptree(PLpgSQL_function *func);
832 * Externs in gram.y and scan.l
835 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
836 extern int plpgsql_yyparse(void);
837 extern int plpgsql_base_yylex(void);
838 extern int plpgsql_yylex(void);
839 extern void plpgsql_push_back_token(int token);
840 extern void plpgsql_yyerror(const char *message);
841 extern int plpgsql_scanner_lineno(void);
842 extern void plpgsql_scanner_init(const char *str, int functype);
843 extern void plpgsql_scanner_finish(void);
844 extern char *plpgsql_get_string_value(void);
846 #endif /* PLPGSQL_H */