1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2007, 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.85 2007/02/09 03:35:34 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
85 PLPGSQL_STMT_RETURN_NEXT,
88 PLPGSQL_STMT_DYNEXECUTE,
99 * Execution node return codes
111 * GET DIAGNOSTICS system attrs
116 PLPGSQL_GETDIAG_ROW_COUNT,
117 PLPGSQL_GETDIAG_RESULT_OID
121 /**********************************************************************
122 * Node and structure definitions
123 **********************************************************************/
127 { /* Dynamic string control structure */
129 int used; /* Including NUL terminator */
135 { /* Postgres data type */
136 char *typname; /* (simple) name of the type */
137 Oid typoid; /* OID of the data type */
138 int ttype; /* PLPGSQL_TTYPE_ code */
139 int16 typlen; /* stuff copied from its pg_type entry */
143 FmgrInfo typinput; /* lookup info for typinput function */
144 int32 atttypmod; /* typmod (taken from someplace else) */
149 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
150 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
154 { /* Generic datum array item */
160 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
164 { /* Scalar or composite variable */
171 typedef struct PLpgSQL_expr
172 { /* SQL Query to plan and execute */
178 /* fields for "simple expression" fast-path execution: */
179 Expr *expr_simple_expr; /* NULL means not a simple expr */
180 Oid expr_simple_type;
183 * if expr is simple AND prepared in current eval_estate,
184 * expr_simple_state is valid. Test validity by seeing if expr_simple_id
185 * matches eval_estate_simple_id.
187 ExprState *expr_simple_state;
188 long int expr_simple_id;
190 /* params to pass to expr */
192 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
197 { /* Scalar variable */
203 PLpgSQL_type *datatype;
206 PLpgSQL_expr *default_val;
207 PLpgSQL_expr *cursor_explicit_expr;
208 int cursor_explicit_argrow;
223 TupleDesc rowtupdesc;
226 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
228 * Note: if the underlying rowtype contains a dropped column, the
229 * corresponding fieldnames[] entry will be NULL, and there is no
230 * corresponding var (varnos[] will be -1).
239 { /* Record variable (non-fixed structure) */
253 { /* Field in record */
257 int recparentno; /* dno of parent record */
262 { /* Element of array variable */
265 PLpgSQL_expr *subscript;
266 int arrayparentno; /* dno of parent array variable */
271 { /* Positional argument to trigger */
274 PLpgSQL_expr *argnum;
279 { /* Item in the compilers namestack */
286 /* XXX: consider adapting this to use List */
287 typedef struct PLpgSQL_ns
288 { /* Compiler namestack level */
291 PLpgSQL_nsitem **items;
292 struct PLpgSQL_ns *upper;
297 { /* Generic execution node */
303 typedef struct PLpgSQL_condition
304 { /* One EXCEPTION condition name */
305 int sqlerrstate; /* SQLSTATE code */
306 char *condname; /* condition name (for debugging) */
307 struct PLpgSQL_condition *next;
314 List *exc_list; /* List of WHEN clauses */
315 } PLpgSQL_exception_block;
318 { /* One EXCEPTION ... WHEN clause */
320 PLpgSQL_condition *conditions;
321 List *action; /* List of statements */
326 { /* Block of statements */
330 List *body; /* List of statements */
333 PLpgSQL_exception_block *exceptions;
334 } PLpgSQL_stmt_block;
338 { /* Assign statement */
343 } PLpgSQL_stmt_assign;
346 { /* PERFORM statement */
350 } PLpgSQL_stmt_perform;
353 { /* Get Diagnostics item */
354 int kind; /* id for diagnostic value desired */
355 int target; /* where to assign it */
359 { /* Get Diagnostics statement */
362 List *diag_items; /* List of PLpgSQL_diag_item */
363 } PLpgSQL_stmt_getdiag;
371 List *true_body; /* List of statements */
372 List *false_body; /* List of statements */
377 { /* Unconditional LOOP statement */
381 List *body; /* List of statements */
386 { /* WHILE cond LOOP statement */
391 List *body; /* List of statements */
392 } PLpgSQL_stmt_while;
396 { /* FOR statement with integer loopvar */
405 List *body; /* List of statements */
410 { /* FOR statement running over SELECT */
417 List *body; /* List of statements */
422 { /* FOR statement running over EXECUTE */
429 List *body; /* List of statements */
430 } PLpgSQL_stmt_dynfors;
434 { /* OPEN a curvar */
438 PLpgSQL_row *returntype;
439 PLpgSQL_expr *argquery;
441 PLpgSQL_expr *dynquery;
446 { /* FETCH curvar INTO statement */
452 } PLpgSQL_stmt_fetch;
460 } PLpgSQL_stmt_close;
464 { /* EXIT or CONTINUE statement */
467 bool is_exit; /* Is this an exit or a continue? */
474 { /* RETURN statement */
479 } PLpgSQL_stmt_return;
482 { /* RETURN NEXT statement */
487 } PLpgSQL_stmt_return_next;
490 { /* RAISE statement */
495 List *params; /* list of expressions */
496 } PLpgSQL_stmt_raise;
500 { /* Generic SQL statement to execute */
503 PLpgSQL_expr *sqlstmt;
504 bool mod_stmt; /* is the stmt INSERT/UPDATE/DELETE? */
505 /* note: mod_stmt is set when we plan the query */
506 bool into; /* INTO supplied? */
507 bool strict; /* INTO STRICT flag */
508 PLpgSQL_rec *rec; /* INTO target, if record */
509 PLpgSQL_row *row; /* INTO target, if row */
510 } PLpgSQL_stmt_execsql;
514 { /* Dynamic SQL string to execute */
517 PLpgSQL_expr *query; /* string expression */
518 bool into; /* INTO supplied? */
519 bool strict; /* INTO STRICT flag */
520 PLpgSQL_rec *rec; /* INTO target, if record */
521 PLpgSQL_row *row; /* INTO target, if row */
522 } PLpgSQL_stmt_dynexecute;
525 typedef struct PLpgSQL_func_hashkey
526 { /* Hash lookup key for functions */
530 * For a trigger function, the OID of the relation triggered on is part of
531 * the hashkey --- we want to compile the trigger separately for each
532 * relation it is used with, in case the rowtype is different. Zero if
533 * not called as a trigger.
538 * We include actual argument types in the hash key to support polymorphic
539 * PLpgSQL functions. Be careful that extra positions are zeroed!
541 Oid argtypes[FUNC_MAX_ARGS];
542 } PLpgSQL_func_hashkey;
545 typedef struct PLpgSQL_function
546 { /* Complete compiled function */
549 TransactionId fn_xmin;
550 ItemPointerData fn_tid;
552 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
553 MemoryContext fn_cxt;
558 FmgrInfo fn_retinput;
559 Oid fn_rettypioparam;
565 int fn_argvarnos[FUNC_MAX_ARGS];
575 int tg_relname_varno;
576 int tg_table_name_varno;
577 int tg_table_schema_varno;
581 PLpgSQL_datum **datums;
582 PLpgSQL_stmt_block *action;
584 unsigned long use_count;
589 { /* Runtime execution data */
592 Oid rettype; /* type of current retval */
594 Oid fn_rettype; /* info about declared function rettype */
600 TupleDesc rettupdesc;
601 char *exitlabel; /* the "target" label of the current EXIT or
602 * CONTINUE stmt, if any */
604 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
605 MemoryContext tuple_store_cxt;
613 PLpgSQL_datum **datums;
615 /* temporary state for results from evaluation of query or expr */
616 SPITupleTable *eval_tuptable;
617 uint32 eval_processed;
619 ExprContext *eval_econtext; /* for executing simple expressions */
620 EState *eval_estate; /* EState containing eval_econtext */
621 long int eval_estate_simple_id; /* ID for eval_estate */
623 /* status information for error context reporting */
624 PLpgSQL_function *err_func; /* current func */
625 PLpgSQL_stmt *err_stmt; /* current stmt */
626 const char *err_text; /* additional state info */
627 void *plugin_info; /* reserved for use by optional plugin */
632 * A PLpgSQL_plugin structure represents an instrumentation plugin.
633 * To instrument PL/pgSQL, a plugin library must access the rendezvous
634 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
635 * Typically the struct could just be static data in the plugin library.
636 * We expect that a plugin would do this at library load time (_PG_init()).
637 * It must also be careful to set the rendezvous variable back to NULL
638 * if it is unloaded (_PG_fini()).
640 * This structure is basically a collection of function pointers --- at
641 * various interesting points in pl_exec.c, we call these functions
642 * (if the pointers are non-NULL) to give the plugin a chance to watch
645 * func_setup is called when we start a function, before we've initialized
646 * the local variables defined by the function.
648 * func_beg is called when we start a function, after we've initialized
649 * the local variables.
651 * func_end is called at the end of a function.
653 * stmt_beg and stmt_end are called before and after (respectively) each
656 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
657 * error_callback and assign_expr fields with pointers to its own
658 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
659 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
664 /* Function pointers set up by the plugin */
665 void (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
666 void (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
667 void (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
668 void (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
669 void (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
671 /* Function pointers set by PL/pgSQL itself */
672 void (*error_callback) (void *arg);
673 void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
678 /**********************************************************************
679 * Global variable declarations
680 **********************************************************************/
682 extern bool plpgsql_DumpExecTree;
683 extern bool plpgsql_SpaceScanned;
684 extern int plpgsql_nDatums;
685 extern PLpgSQL_datum **plpgsql_Datums;
687 extern int plpgsql_error_lineno;
688 extern char *plpgsql_error_funcname;
690 /* linkage to the real yytext variable */
691 extern char *plpgsql_base_yytext;
693 #define yytext plpgsql_base_yytext
695 extern PLpgSQL_function *plpgsql_curr_compile;
696 extern bool plpgsql_check_syntax;
697 extern MemoryContext compile_tmp_cxt;
699 extern PLpgSQL_plugin **plugin_ptr;
701 /**********************************************************************
702 * Function declarations
703 **********************************************************************/
706 * Functions in pl_comp.c
709 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
711 extern int plpgsql_parse_word(char *word);
712 extern int plpgsql_parse_dblword(char *word);
713 extern int plpgsql_parse_tripword(char *word);
714 extern int plpgsql_parse_wordtype(char *word);
715 extern int plpgsql_parse_dblwordtype(char *word);
716 extern int plpgsql_parse_tripwordtype(char *word);
717 extern int plpgsql_parse_wordrowtype(char *word);
718 extern int plpgsql_parse_dblwordrowtype(char *word);
719 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
720 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
721 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
724 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
725 extern void plpgsql_adddatum(PLpgSQL_datum *new);
726 extern int plpgsql_add_initdatums(int **varnos);
727 extern void plpgsql_HashTableInit(void);
728 extern void plpgsql_compile_error_callback(void *arg);
731 * Functions in pl_handler.c
734 extern void _PG_init(void);
735 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
736 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
739 * Functions in pl_exec.c
742 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
743 FunctionCallInfo fcinfo);
744 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
745 TriggerData *trigdata);
746 extern void plpgsql_xact_cb(XactEvent event, void *arg);
747 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
748 SubTransactionId parentSubid, void *arg);
751 * Functions for the dynamic string handling in pl_funcs.c
754 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
755 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
756 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
757 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
758 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
761 * Functions for the namestack handling in pl_funcs.c
764 extern void plpgsql_ns_init(void);
765 extern bool plpgsql_ns_setlocal(bool flag);
766 extern void plpgsql_ns_push(char *label);
767 extern void plpgsql_ns_pop(void);
768 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
769 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
770 extern void plpgsql_ns_rename(char *oldname, char *newname);
773 * Other functions in pl_funcs.c
776 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
777 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
778 extern void plpgsql_dumptree(PLpgSQL_function *func);
781 * Externs in gram.y and scan.l
784 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
785 extern int plpgsql_yyparse(void);
786 extern int plpgsql_base_yylex(void);
787 extern int plpgsql_yylex(void);
788 extern void plpgsql_push_back_token(int token);
789 extern void plpgsql_yyerror(const char *message);
790 extern int plpgsql_scanner_lineno(void);
791 extern void plpgsql_scanner_init(const char *str, int functype);
792 extern void plpgsql_scanner_finish(void);
793 extern char *plpgsql_get_string_value(void);
795 #endif /* PLPGSQL_H */