1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2010, 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.130 2010/02/26 02:01:35 momjian Exp $
13 *-------------------------------------------------------------------------
21 #include "access/xact.h"
23 #include "commands/trigger.h"
24 #include "executor/spi.h"
25 #include "lib/stringinfo.h"
26 #include "nodes/bitmapset.h"
27 #include "utils/tuplestore.h"
29 /**********************************************************************
31 **********************************************************************/
33 /* define our text domain for translations */
35 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
38 #define _(x) dgettext(TEXTDOMAIN, x)
41 * Compiler's namespace item types
53 * Datum array node types
61 PLPGSQL_DTYPE_RECFIELD,
62 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 * Behavioral modes for plpgsql variable resolution
150 PLPGSQL_RESOLVE_ERROR, /* throw error if ambiguous */
151 PLPGSQL_RESOLVE_VARIABLE, /* prefer plpgsql var to table column */
152 PLPGSQL_RESOLVE_COLUMN /* prefer table column to plpgsql var */
153 } PLpgSQL_resolve_option;
156 /**********************************************************************
157 * Node and structure definitions
158 **********************************************************************/
162 { /* Postgres data type */
163 char *typname; /* (simple) name of the type */
164 Oid typoid; /* OID of the data type */
165 int ttype; /* PLPGSQL_TTYPE_ code */
166 int16 typlen; /* stuff copied from its pg_type entry */
170 FmgrInfo typinput; /* lookup info for typinput function */
171 int32 atttypmod; /* typmod (taken from someplace else) */
176 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
177 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, and PLpgSQL_arrayelem
180 { /* Generic datum array item */
186 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
190 { /* Scalar or composite variable */
197 typedef struct PLpgSQL_expr
198 { /* SQL Query to plan and execute */
203 Bitmapset *paramnos; /* all dnos referenced by this query */
205 /* function containing this expr (not set until we first parse query) */
206 struct PLpgSQL_function *func;
208 /* namespace chain visible to this expr */
209 struct PLpgSQL_nsitem *ns;
211 /* fields for "simple expression" fast-path execution: */
212 Expr *expr_simple_expr; /* NULL means not a simple expr */
213 int expr_simple_generation; /* plancache generation we checked */
214 Oid expr_simple_type; /* result type Oid, if simple */
217 * if expr is simple AND prepared in current transaction,
218 * expr_simple_state is valid. Test validity by seeing if expr_simple_lxid
219 * matches current LXID.
221 ExprState *expr_simple_state;
222 LocalTransactionId expr_simple_lxid;
227 { /* Scalar variable */
233 PLpgSQL_type *datatype;
236 PLpgSQL_expr *default_val;
237 PLpgSQL_expr *cursor_explicit_expr;
238 int cursor_explicit_argrow;
254 TupleDesc rowtupdesc;
257 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
259 * Note: if the underlying rowtype contains a dropped column, the
260 * corresponding fieldnames[] entry will be NULL, and there is no
261 * corresponding var (varnos[] will be -1).
270 { /* Record variable (non-fixed structure) */
284 { /* Field in record */
288 int recparentno; /* dno of parent record */
293 { /* Element of array variable */
296 PLpgSQL_expr *subscript;
297 int arrayparentno; /* dno of parent array variable */
301 typedef struct PLpgSQL_nsitem
302 { /* Item in the compilers namespace tree */
305 struct PLpgSQL_nsitem *prev;
306 char name[1]; /* actually, as long as needed */
311 { /* Generic execution node */
317 typedef struct PLpgSQL_condition
318 { /* One EXCEPTION condition name */
319 int sqlerrstate; /* SQLSTATE code */
320 char *condname; /* condition name (for debugging) */
321 struct PLpgSQL_condition *next;
328 List *exc_list; /* List of WHEN clauses */
329 } PLpgSQL_exception_block;
332 { /* One EXCEPTION ... WHEN clause */
334 PLpgSQL_condition *conditions;
335 List *action; /* List of statements */
340 { /* Block of statements */
344 List *body; /* List of statements */
347 PLpgSQL_exception_block *exceptions;
348 } PLpgSQL_stmt_block;
352 { /* Assign statement */
357 } PLpgSQL_stmt_assign;
360 { /* PERFORM statement */
364 } PLpgSQL_stmt_perform;
367 { /* Get Diagnostics item */
368 int kind; /* id for diagnostic value desired */
369 int target; /* where to assign it */
373 { /* Get Diagnostics statement */
376 List *diag_items; /* List of PLpgSQL_diag_item */
377 } PLpgSQL_stmt_getdiag;
385 List *true_body; /* List of statements */
386 List *false_body; /* List of statements */
390 typedef struct /* CASE statement */
394 PLpgSQL_expr *t_expr; /* test expression, or NULL if none */
395 int t_varno; /* var to store test expression value into */
396 List *case_when_list; /* List of PLpgSQL_case_when structs */
397 bool have_else; /* flag needed because list could be empty */
398 List *else_stmts; /* List of statements */
401 typedef struct /* one arm of CASE statement */
404 PLpgSQL_expr *expr; /* boolean expression for this case */
405 List *stmts; /* List of statements */
410 { /* Unconditional LOOP statement */
414 List *body; /* List of statements */
419 { /* WHILE cond LOOP statement */
424 List *body; /* List of statements */
425 } PLpgSQL_stmt_while;
429 { /* FOR statement with integer loopvar */
436 PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
438 List *body; /* List of statements */
443 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
444 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
445 * and PLpgSQL_dynfors.
454 List *body; /* List of statements */
458 { /* FOR statement running over SELECT */
464 List *body; /* List of statements */
465 /* end of fields that must match PLpgSQL_stmt_forq */
470 { /* FOR statement running over cursor */
476 List *body; /* List of statements */
477 /* end of fields that must match PLpgSQL_stmt_forq */
479 PLpgSQL_expr *argquery; /* cursor arguments if any */
483 { /* FOR statement running over EXECUTE */
489 List *body; /* List of statements */
490 /* end of fields that must match PLpgSQL_stmt_forq */
492 List *params; /* USING expressions */
493 } PLpgSQL_stmt_dynfors;
497 { /* OPEN a curvar */
502 PLpgSQL_row *returntype;
503 PLpgSQL_expr *argquery;
505 PLpgSQL_expr *dynquery;
506 List *params; /* USING expressions */
511 { /* FETCH or MOVE statement */
514 PLpgSQL_rec *rec; /* target, as record or row */
516 int curvar; /* cursor variable to fetch from */
517 FetchDirection direction; /* fetch direction */
518 long how_many; /* count, if constant (expr is NULL) */
519 PLpgSQL_expr *expr; /* count, if expression */
520 bool is_move; /* is this a fetch or move? */
521 bool returns_multiple_rows; /* can return more than one row? */
522 } PLpgSQL_stmt_fetch;
530 } PLpgSQL_stmt_close;
534 { /* EXIT or CONTINUE statement */
537 bool is_exit; /* Is this an exit or a continue? */
538 char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
544 { /* RETURN statement */
549 } PLpgSQL_stmt_return;
552 { /* RETURN NEXT statement */
557 } PLpgSQL_stmt_return_next;
560 { /* RETURN QUERY statement */
563 PLpgSQL_expr *query; /* if static query */
564 PLpgSQL_expr *dynquery; /* if dynamic query (RETURN QUERY EXECUTE) */
565 List *params; /* USING arguments for dynamic query */
566 } PLpgSQL_stmt_return_query;
569 { /* RAISE statement */
573 char *condname; /* condition name, SQLSTATE, or NULL */
574 char *message; /* old-style message format literal, or NULL */
575 List *params; /* list of expressions for old-style message */
576 List *options; /* list of PLpgSQL_raise_option */
577 } PLpgSQL_stmt_raise;
580 { /* RAISE statement option */
583 } PLpgSQL_raise_option;
587 { /* Generic SQL statement to execute */
590 PLpgSQL_expr *sqlstmt;
591 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
592 /* note: mod_stmt is set when we plan the query */
593 bool into; /* INTO supplied? */
594 bool strict; /* INTO STRICT flag */
595 PLpgSQL_rec *rec; /* INTO target, if record */
596 PLpgSQL_row *row; /* INTO target, if row */
597 } PLpgSQL_stmt_execsql;
601 { /* Dynamic SQL string to execute */
604 PLpgSQL_expr *query; /* string expression */
605 bool into; /* INTO supplied? */
606 bool strict; /* INTO STRICT flag */
607 PLpgSQL_rec *rec; /* INTO target, if record */
608 PLpgSQL_row *row; /* INTO target, if row */
609 List *params; /* USING expressions */
610 } PLpgSQL_stmt_dynexecute;
613 typedef struct PLpgSQL_func_hashkey
614 { /* Hash lookup key for functions */
617 bool isTrigger; /* true if called as a trigger */
619 /* be careful that pad bytes in this struct get zeroed! */
622 * For a trigger function, the OID of the relation triggered on is part of
623 * the hashkey --- we want to compile the trigger separately for each
624 * relation it is used with, in case the rowtype is different. Zero if
625 * not called as a trigger.
630 * We include actual argument types in the hash key to support polymorphic
631 * PLpgSQL functions. Be careful that extra positions are zeroed!
633 Oid argtypes[FUNC_MAX_ARGS];
634 } PLpgSQL_func_hashkey;
637 typedef struct PLpgSQL_function
638 { /* Complete compiled function */
641 TransactionId fn_xmin;
642 ItemPointerData fn_tid;
644 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
645 MemoryContext fn_cxt;
650 FmgrInfo fn_retinput;
651 Oid fn_rettypioparam;
657 int fn_argvarnos[FUNC_MAX_ARGS];
667 int tg_relname_varno;
668 int tg_table_name_varno;
669 int tg_table_schema_varno;
673 PLpgSQL_resolve_option resolve_option;
676 PLpgSQL_datum **datums;
677 PLpgSQL_stmt_block *action;
679 /* these fields change when the function is used */
680 struct PLpgSQL_execstate *cur_estate;
681 unsigned long use_count;
685 typedef struct PLpgSQL_execstate
686 { /* Runtime execution data */
687 PLpgSQL_function *func; /* function being executed */
691 Oid rettype; /* type of current retval */
693 Oid fn_rettype; /* info about declared function rettype */
699 TupleDesc rettupdesc;
700 char *exitlabel; /* the "target" label of the current EXIT or
701 * CONTINUE stmt, if any */
703 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
704 MemoryContext tuple_store_cxt;
705 ResourceOwner tuple_store_owner;
710 PLpgSQL_datum **datums;
712 /* temporary state for results from evaluation of query or expr */
713 SPITupleTable *eval_tuptable;
714 uint32 eval_processed;
716 ExprContext *eval_econtext; /* for executing simple expressions */
717 PLpgSQL_expr *cur_expr; /* current query/expr being evaluated */
719 /* status information for error context reporting */
720 PLpgSQL_stmt *err_stmt; /* current stmt */
721 const char *err_text; /* additional state info */
723 void *plugin_info; /* reserved for use by optional plugin */
728 * A PLpgSQL_plugin structure represents an instrumentation plugin.
729 * To instrument PL/pgSQL, a plugin library must access the rendezvous
730 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
731 * Typically the struct could just be static data in the plugin library.
732 * We expect that a plugin would do this at library load time (_PG_init()).
733 * It must also be careful to set the rendezvous variable back to NULL
734 * if it is unloaded (_PG_fini()).
736 * This structure is basically a collection of function pointers --- at
737 * various interesting points in pl_exec.c, we call these functions
738 * (if the pointers are non-NULL) to give the plugin a chance to watch
741 * func_setup is called when we start a function, before we've initialized
742 * the local variables defined by the function.
744 * func_beg is called when we start a function, after we've initialized
745 * the local variables.
747 * func_end is called at the end of a function.
749 * stmt_beg and stmt_end are called before and after (respectively) each
752 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
753 * error_callback and assign_expr fields with pointers to its own
754 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
755 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
760 /* Function pointers set up by the plugin */
761 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
762 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
763 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
764 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
765 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
767 /* Function pointers set by PL/pgSQL itself */
768 void (*error_callback) (void *arg);
769 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
774 /* Struct types used during parsing */
778 char *ident; /* palloc'd converted identifier */
779 bool quoted; /* Was it double-quoted? */
784 List *idents; /* composite identifiers (list of String) */
789 PLpgSQL_datum *datum; /* referenced variable */
790 char *ident; /* valid if simple name */
792 List *idents; /* valid if composite name */
795 /**********************************************************************
796 * Global variable declarations
797 **********************************************************************/
801 IDENTIFIER_LOOKUP_NORMAL, /* normal processing of var names */
802 IDENTIFIER_LOOKUP_DECLARE, /* In DECLARE --- don't look up names */
803 IDENTIFIER_LOOKUP_EXPR /* In SQL expression --- special case */
806 extern IdentifierLookup plpgsql_IdentifierLookup;
808 extern int plpgsql_variable_conflict;
810 extern bool plpgsql_check_syntax;
811 extern bool plpgsql_DumpExecTree;
813 extern PLpgSQL_stmt_block *plpgsql_parse_result;
815 extern int plpgsql_nDatums;
816 extern PLpgSQL_datum **plpgsql_Datums;
818 extern char *plpgsql_error_funcname;
820 extern PLpgSQL_function *plpgsql_curr_compile;
821 extern MemoryContext compile_tmp_cxt;
823 extern PLpgSQL_plugin **plugin_ptr;
825 /**********************************************************************
826 * Function declarations
827 **********************************************************************/
830 * Functions in pl_comp.c
833 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
835 extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
836 extern void plpgsql_parser_setup(struct ParseState *pstate,
838 extern bool plpgsql_parse_word(char *word1, const char *yytxt,
839 PLwdatum *wdatum, PLword *word);
840 extern bool plpgsql_parse_dblword(char *word1, char *word2,
841 PLwdatum *wdatum, PLcword *cword);
842 extern bool plpgsql_parse_tripword(char *word1, char *word2, char *word3,
843 PLwdatum *wdatum, PLcword *cword);
844 extern PLpgSQL_type *plpgsql_parse_wordtype(char *ident);
845 extern PLpgSQL_type *plpgsql_parse_cwordtype(List *idents);
846 extern PLpgSQL_type *plpgsql_parse_wordrowtype(char *ident);
847 extern PLpgSQL_type *plpgsql_parse_cwordrowtype(List *idents);
848 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
849 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
852 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
854 extern int plpgsql_recognize_err_condition(const char *condname,
855 bool allow_sqlstate);
856 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
857 extern void plpgsql_adddatum(PLpgSQL_datum *new);
858 extern int plpgsql_add_initdatums(int **varnos);
859 extern void plpgsql_HashTableInit(void);
862 * Functions in pl_handler.c
865 extern void _PG_init(void);
866 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
867 extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
868 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
871 * Functions in pl_exec.c
874 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
875 FunctionCallInfo fcinfo);
876 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
877 TriggerData *trigdata);
878 extern void plpgsql_xact_cb(XactEvent event, void *arg);
879 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
880 SubTransactionId parentSubid, void *arg);
881 extern Oid exec_get_datum_type(PLpgSQL_execstate *estate,
882 PLpgSQL_datum *datum);
883 extern Oid exec_get_rec_fieldtype(PLpgSQL_rec *rec, const char *fieldname,
887 * Functions for namespace handling in pl_funcs.c
890 extern void plpgsql_ns_init(void);
891 extern void plpgsql_ns_push(const char *label);
892 extern void plpgsql_ns_pop(void);
893 extern PLpgSQL_nsitem *plpgsql_ns_top(void);
894 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
895 extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
896 const char *name1, const char *name2,
897 const char *name3, int *names_used);
898 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
902 * Other functions in pl_funcs.c
905 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
906 extern void plpgsql_dumptree(PLpgSQL_function *func);
909 * Scanner functions in pl_scanner.c
912 extern int plpgsql_base_yylex(void);
913 extern int plpgsql_yylex(void);
914 extern void plpgsql_push_back_token(int token);
915 extern void plpgsql_append_source_text(StringInfo buf,
916 int startlocation, int endlocation);
917 extern int plpgsql_scanner_errposition(int location);
918 extern void plpgsql_yyerror(const char *message);
919 extern int plpgsql_location_to_lineno(int location);
920 extern int plpgsql_latest_lineno(void);
921 extern void plpgsql_scanner_init(const char *str);
922 extern void plpgsql_scanner_finish(void);
928 extern int plpgsql_yyparse(void);
930 #endif /* PLPGSQL_H */