1 /**********************************************************************
2 * plpgsql.h - Definitions for the PL/pgSQL
6 * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.68 2006/03/07 01:03:12 tgl Exp $
8 * This software is copyrighted by Jan Wieck - Hamburg.
10 * The author hereby grants permission to use, copy, modify,
11 * distribute, and license this software and its documentation
12 * for any purpose, provided that existing copyright notices are
13 * retained in all copies and that this notice is included
14 * verbatim in any distributions. No written agreement, license,
15 * or royalty fee is required for any of the authorized uses.
16 * Modifications to this software may be copyrighted by their
17 * author and need not follow the licensing terms described
18 * here, provided that the new terms are clearly indicated on
19 * the first page of each file where they apply.
21 * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
22 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
23 * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
24 * SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
25 * IF THE AUTHOR HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
28 * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
31 * PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON
32 * AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAVE NO
33 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
34 * ENHANCEMENTS, OR MODIFICATIONS.
36 **********************************************************************/
43 #include "miscadmin.h"
44 #include "executor/spi.h"
45 #include "commands/trigger.h"
46 #include "utils/tuplestore.h"
48 /**********************************************************************
50 **********************************************************************/
53 * Compiler's namestack item types
65 * Datum array node types
73 PLPGSQL_DTYPE_RECFIELD,
74 PLPGSQL_DTYPE_ARRAYELEM,
80 * Variants distinguished in PLpgSQL_type structs
85 PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
86 PLPGSQL_TTYPE_ROW, /* composite types */
87 PLPGSQL_TTYPE_REC, /* RECORD pseudotype */
88 PLPGSQL_TTYPE_PSEUDO /* other pseudotypes */
92 * Execution tree node types
107 PLPGSQL_STMT_RETURN_NEXT,
109 PLPGSQL_STMT_EXECSQL,
110 PLPGSQL_STMT_DYNEXECUTE,
111 PLPGSQL_STMT_DYNFORS,
112 PLPGSQL_STMT_GETDIAG,
121 * Execution node return codes
133 * GET DIAGNOSTICS system attrs
138 PLPGSQL_GETDIAG_ROW_COUNT,
139 PLPGSQL_GETDIAG_RESULT_OID
143 /**********************************************************************
144 * Node and structure definitions
145 **********************************************************************/
149 { /* Dynamic string control structure */
151 int used; /* Including NUL terminator */
157 { /* Postgres data type */
158 char *typname; /* (simple) name of the type */
159 Oid typoid; /* OID of the data type */
160 int ttype; /* PLPGSQL_TTYPE_ code */
161 int16 typlen; /* stuff copied from its pg_type entry */
165 FmgrInfo typinput; /* lookup info for typinput function */
166 int32 atttypmod; /* typmod (taken from someplace else) */
171 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
172 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
176 { /* Generic datum array item */
182 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
186 { /* Scalar or composite variable */
193 typedef struct PLpgSQL_expr
194 { /* SQL Query to plan and execute */
200 /* fields for "simple expression" fast-path execution: */
201 Expr *expr_simple_expr; /* NULL means not a simple expr */
202 Oid expr_simple_type;
204 * if expr is simple AND in use in current xact, expr_simple_state is
205 * valid. Test validity by seeing if expr_simple_xid matches current XID.
207 ExprState *expr_simple_state;
208 TransactionId expr_simple_xid;
209 /* params to pass to expr */
211 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
216 { /* Scalar variable */
222 PLpgSQL_type *datatype;
225 PLpgSQL_expr *default_val;
226 PLpgSQL_expr *cursor_explicit_expr;
227 int cursor_explicit_argrow;
242 TupleDesc rowtupdesc;
245 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
247 * Note: if the underlying rowtype contains a dropped column, the
248 * corresponding fieldnames[] entry will be NULL, and there is no
249 * corresponding var (varnos[] will be -1).
258 { /* Record variable (non-fixed structure) */
272 { /* Field in record */
276 int recparentno; /* dno of parent record */
281 { /* Element of array variable */
284 PLpgSQL_expr *subscript;
285 int arrayparentno; /* dno of parent array variable */
290 { /* Positional argument to trigger */
293 PLpgSQL_expr *argnum;
298 { /* Item in the compilers namestack */
305 /* XXX: consider adapting this to use List */
306 typedef struct PLpgSQL_ns
307 { /* Compiler namestack level */
310 PLpgSQL_nsitem **items;
311 struct PLpgSQL_ns *upper;
316 { /* Generic execution node */
322 typedef struct PLpgSQL_condition
323 { /* One EXCEPTION condition name */
324 int sqlerrstate; /* SQLSTATE code */
325 char *condname; /* condition name (for debugging) */
326 struct PLpgSQL_condition *next;
333 List *exc_list; /* List of WHEN clauses */
334 } PLpgSQL_exception_block;
337 { /* One EXCEPTION ... WHEN clause */
339 PLpgSQL_condition *conditions;
340 List *action; /* List of statements */
345 { /* Block of statements */
349 List *body; /* List of statements */
352 PLpgSQL_exception_block *exceptions;
353 } PLpgSQL_stmt_block;
357 { /* Assign statement */
362 } PLpgSQL_stmt_assign;
365 { /* PERFORM statement */
369 } PLpgSQL_stmt_perform;
372 { /* Get Diagnostics item */
373 int kind; /* id for diagnostic value desired */
374 int target; /* where to assign it */
378 { /* Get Diagnostics statement */
381 List *diag_items; /* List of PLpgSQL_diag_item */
382 } PLpgSQL_stmt_getdiag;
390 List *true_body; /* List of statements */
391 List *false_body; /* List of statements */
396 { /* Unconditional LOOP statement */
400 List *body; /* List of statements */
405 { /* WHILE cond LOOP statement */
410 List *body; /* List of statements */
411 } PLpgSQL_stmt_while;
415 { /* FOR statement with integer loopvar */
423 List *body; /* List of statements */
428 { /* FOR statement running over SELECT */
435 List *body; /* List of statements */
440 { /* FOR statement running over EXECUTE */
447 List *body; /* List of statements */
448 } PLpgSQL_stmt_dynfors;
452 { /* SELECT ... INTO statement */
458 } PLpgSQL_stmt_select;
462 { /* OPEN a curvar */
466 PLpgSQL_row *returntype;
467 PLpgSQL_expr *argquery;
469 PLpgSQL_expr *dynquery;
474 { /* FETCH curvar INTO statement */
480 } PLpgSQL_stmt_fetch;
488 } PLpgSQL_stmt_close;
492 { /* EXIT or CONTINUE statement */
495 bool is_exit; /* Is this an exit or a continue? */
502 { /* RETURN statement */
507 } PLpgSQL_stmt_return;
510 { /* RETURN NEXT statement */
515 } PLpgSQL_stmt_return_next;
518 { /* RAISE statement */
523 List *params; /* list of expressions */
524 } PLpgSQL_stmt_raise;
528 { /* Generic SQL statement to execute */
531 PLpgSQL_expr *sqlstmt;
532 } PLpgSQL_stmt_execsql;
536 { /* Dynamic SQL string to execute */
539 PLpgSQL_rec *rec; /* INTO record or row variable */
542 } PLpgSQL_stmt_dynexecute;
545 typedef struct PLpgSQL_func_hashkey
546 { /* Hash lookup key for functions */
550 * For a trigger function, the OID of the relation triggered on is part of
551 * the hashkey --- we want to compile the trigger separately for each
552 * relation it is used with, in case the rowtype is different. Zero if
553 * not called as a trigger.
558 * We include actual argument types in the hash key to support polymorphic
559 * PLpgSQL functions. Be careful that extra positions are zeroed!
561 Oid argtypes[FUNC_MAX_ARGS];
562 } PLpgSQL_func_hashkey;
565 typedef struct PLpgSQL_function
566 { /* Complete compiled function */
569 TransactionId fn_xmin;
572 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
573 MemoryContext fn_cxt;
578 FmgrInfo fn_retinput;
579 Oid fn_rettypioparam;
585 int fn_argvarnos[FUNC_MAX_ARGS];
595 int tg_relname_varno;
599 PLpgSQL_datum **datums;
600 PLpgSQL_stmt_block *action;
605 { /* Runtime execution data */
608 Oid rettype; /* type of current retval */
610 Oid fn_rettype; /* info about declared function rettype */
616 TupleDesc rettupdesc;
617 char *exitlabel; /* the "target" label of the current EXIT or
618 * CONTINUE stmt, if any */
620 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
621 MemoryContext tuple_store_cxt;
629 PLpgSQL_datum **datums;
631 /* temporary state for results from evaluation of query or expr */
632 SPITupleTable *eval_tuptable;
633 uint32 eval_processed;
635 ExprContext *eval_econtext;
637 /* status information for error context reporting */
638 PLpgSQL_function *err_func; /* current func */
639 PLpgSQL_stmt *err_stmt; /* current stmt */
640 const char *err_text; /* additional state info */
644 /**********************************************************************
645 * Global variable declarations
646 **********************************************************************/
648 extern bool plpgsql_DumpExecTree;
649 extern bool plpgsql_SpaceScanned;
650 extern int plpgsql_nDatums;
651 extern PLpgSQL_datum **plpgsql_Datums;
653 extern int plpgsql_error_lineno;
654 extern char *plpgsql_error_funcname;
656 /* linkage to the real yytext variable */
657 extern char *plpgsql_base_yytext;
659 #define yytext plpgsql_base_yytext
661 extern PLpgSQL_function *plpgsql_curr_compile;
662 extern bool plpgsql_check_syntax;
663 extern MemoryContext compile_tmp_cxt;
665 /**********************************************************************
666 * Function declarations
667 **********************************************************************/
670 * Functions in pl_comp.c
673 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
675 extern int plpgsql_parse_word(char *word);
676 extern int plpgsql_parse_dblword(char *word);
677 extern int plpgsql_parse_tripword(char *word);
678 extern int plpgsql_parse_wordtype(char *word);
679 extern int plpgsql_parse_dblwordtype(char *word);
680 extern int plpgsql_parse_tripwordtype(char *word);
681 extern int plpgsql_parse_wordrowtype(char *word);
682 extern int plpgsql_parse_dblwordrowtype(char *word);
683 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
684 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
685 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
688 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
689 extern void plpgsql_adddatum(PLpgSQL_datum *new);
690 extern int plpgsql_add_initdatums(int **varnos);
691 extern void plpgsql_HashTableInit(void);
692 extern void plpgsql_compile_error_callback(void *arg);
695 * Functions in pl_handler.c
698 extern void plpgsql_init(void);
699 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
700 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
703 * Functions in pl_exec.c
706 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
707 FunctionCallInfo fcinfo);
708 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
709 TriggerData *trigdata);
710 extern void plpgsql_xact_cb(XactEvent event, void *arg);
713 * Functions for the dynamic string handling in pl_funcs.c
716 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
717 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
718 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
719 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
720 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
723 * Functions for the namestack handling in pl_funcs.c
726 extern void plpgsql_ns_init(void);
727 extern bool plpgsql_ns_setlocal(bool flag);
728 extern void plpgsql_ns_push(char *label);
729 extern void plpgsql_ns_pop(void);
730 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
731 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
732 extern void plpgsql_ns_rename(char *oldname, char *newname);
735 * Other functions in pl_funcs.c
738 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
739 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
740 extern void plpgsql_dumptree(PLpgSQL_function *func);
743 * Externs in gram.y and scan.l
746 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
747 extern int plpgsql_yyparse(void);
748 extern int plpgsql_base_yylex(void);
749 extern int plpgsql_yylex(void);
750 extern void plpgsql_push_back_token(int token);
751 extern void plpgsql_yyerror(const char *message);
752 extern int plpgsql_scanner_lineno(void);
753 extern void plpgsql_scanner_init(const char *str, int functype);
754 extern void plpgsql_scanner_finish(void);
755 extern char *plpgsql_get_string_value(void);
757 #endif /* PLPGSQL_H */