1 /**********************************************************************
2 * plpgsql.h - Definitions for the PL/pgSQL
6 * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.64 2005/06/22 01:35:02 neilc 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;
203 /* if expr is simple AND in use in current xact, these fields are set: */
204 ExprState *expr_simple_state;
205 struct PLpgSQL_expr *expr_simple_next;
206 /* params to pass to expr */
208 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
213 { /* Scalar variable */
219 PLpgSQL_type *datatype;
222 PLpgSQL_expr *default_val;
223 PLpgSQL_expr *cursor_explicit_expr;
224 int cursor_explicit_argrow;
239 TupleDesc rowtupdesc;
242 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
244 * Note: if the underlying rowtype contains a dropped column, the
245 * corresponding fieldnames[] entry will be NULL, and there is no
246 * corresponding var (varnos[] will be -1).
255 { /* Record variable (non-fixed structure) */
269 { /* Field in record */
273 int recparentno; /* dno of parent record */
278 { /* Element of array variable */
281 PLpgSQL_expr *subscript;
282 int arrayparentno; /* dno of parent array variable */
287 { /* Positional argument to trigger */
290 PLpgSQL_expr *argnum;
295 { /* Item in the compilers namestack */
302 /* XXX: consider adapting this to use List */
303 typedef struct PLpgSQL_ns
304 { /* Compiler namestack level */
307 PLpgSQL_nsitem **items;
308 struct PLpgSQL_ns *upper;
313 { /* Generic execution node */
319 typedef struct PLpgSQL_condition
320 { /* One EXCEPTION condition name */
321 int sqlerrstate; /* SQLSTATE code */
322 char *condname; /* condition name (for debugging) */
323 struct PLpgSQL_condition *next;
330 List *exc_list; /* List of WHEN clauses */
331 } PLpgSQL_exception_block;
334 { /* One EXCEPTION ... WHEN clause */
336 PLpgSQL_condition *conditions;
337 List *action; /* List of statements */
342 { /* Block of statements */
346 List *body; /* List of statements */
349 PLpgSQL_exception_block *exceptions;
350 } PLpgSQL_stmt_block;
354 { /* Assign statement */
359 } PLpgSQL_stmt_assign;
362 { /* PERFORM statement */
366 } PLpgSQL_stmt_perform;
369 { /* Get Diagnostics item */
370 int kind; /* id for diagnostic value desired */
371 int target; /* where to assign it */
375 { /* Get Diagnostics statement */
378 List *diag_items; /* List of PLpgSQL_diag_item */
379 } PLpgSQL_stmt_getdiag;
387 List *true_body; /* List of statements */
388 List *false_body; /* List of statements */
393 { /* Unconditional LOOP statement */
397 List *body; /* List of statements */
402 { /* WHILE cond LOOP statement */
407 List *body; /* List of statements */
408 } PLpgSQL_stmt_while;
412 { /* FOR statement with integer loopvar */
420 List *body; /* List of statements */
425 { /* FOR statement running over SELECT */
432 List *body; /* List of statements */
437 { /* FOR statement running over EXECUTE */
444 List *body; /* List of statements */
445 } PLpgSQL_stmt_dynfors;
449 { /* SELECT ... INTO statement */
455 } PLpgSQL_stmt_select;
459 { /* OPEN a curvar */
463 PLpgSQL_row *returntype;
464 PLpgSQL_expr *argquery;
466 PLpgSQL_expr *dynquery;
471 { /* FETCH curvar INTO statement */
477 } PLpgSQL_stmt_fetch;
485 } PLpgSQL_stmt_close;
489 { /* EXIT or CONTINUE statement */
492 bool is_exit; /* Is this an exit or a continue? */
499 { /* RETURN statement */
504 } PLpgSQL_stmt_return;
507 { /* RETURN NEXT statement */
512 } PLpgSQL_stmt_return_next;
515 { /* RAISE statement */
520 List *params; /* list of expressions */
521 } PLpgSQL_stmt_raise;
525 { /* Generic SQL statement to execute */
528 PLpgSQL_expr *sqlstmt;
529 } PLpgSQL_stmt_execsql;
533 { /* Dynamic SQL string to execute */
536 PLpgSQL_rec *rec; /* INTO record or row variable */
539 } PLpgSQL_stmt_dynexecute;
542 typedef struct PLpgSQL_func_hashkey
543 { /* Hash lookup key for functions */
547 * For a trigger function, the OID of the relation triggered on is
548 * part of the hashkey --- we want to compile the trigger separately
549 * for each relation it is used with, in case the rowtype is
550 * different. Zero if not called as a trigger.
555 * We include actual argument types in the hash key to support
556 * polymorphic PLpgSQL functions. Be careful that extra positions are
559 Oid argtypes[FUNC_MAX_ARGS];
560 } PLpgSQL_func_hashkey;
563 typedef struct PLpgSQL_function
564 { /* Complete compiled function */
567 TransactionId fn_xmin;
570 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
571 MemoryContext fn_cxt;
576 FmgrInfo fn_retinput;
577 Oid fn_rettypioparam;
583 int fn_argvarnos[FUNC_MAX_ARGS];
593 int tg_relname_varno;
597 PLpgSQL_datum **datums;
598 PLpgSQL_stmt_block *action;
603 { /* Runtime execution data */
606 Oid rettype; /* type of current retval */
608 Oid fn_rettype; /* info about declared function rettype */
614 TupleDesc rettupdesc;
615 char *exitlabel; /* the "target" label of the current
616 * EXIT or CONTINUE stmt, if any */
618 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
619 MemoryContext tuple_store_cxt;
627 PLpgSQL_datum **datums;
629 /* temporary state for results from evaluation of query or expr */
630 SPITupleTable *eval_tuptable;
631 uint32 eval_processed;
633 ExprContext *eval_econtext;
635 /* status information for error context reporting */
636 PLpgSQL_function *err_func; /* current func */
637 PLpgSQL_stmt *err_stmt; /* current stmt */
638 const char *err_text; /* additional state info */
642 /**********************************************************************
643 * Global variable declarations
644 **********************************************************************/
646 extern bool plpgsql_DumpExecTree;
647 extern bool plpgsql_SpaceScanned;
648 extern int plpgsql_nDatums;
649 extern PLpgSQL_datum **plpgsql_Datums;
651 extern int plpgsql_error_lineno;
652 extern char *plpgsql_error_funcname;
654 /* linkage to the real yytext variable */
655 extern char *plpgsql_base_yytext;
657 #define plpgsql_yytext plpgsql_base_yytext
659 extern PLpgSQL_function *plpgsql_curr_compile;
660 extern bool plpgsql_check_syntax;
661 extern MemoryContext compile_tmp_cxt;
663 /**********************************************************************
664 * Function declarations
665 **********************************************************************/
668 * Functions in pl_comp.c
671 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
673 extern int plpgsql_parse_word(char *word);
674 extern int plpgsql_parse_dblword(char *word);
675 extern int plpgsql_parse_tripword(char *word);
676 extern int plpgsql_parse_wordtype(char *word);
677 extern int plpgsql_parse_dblwordtype(char *word);
678 extern int plpgsql_parse_tripwordtype(char *word);
679 extern int plpgsql_parse_wordrowtype(char *word);
680 extern int plpgsql_parse_dblwordrowtype(char *word);
681 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
682 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
683 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
686 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
687 extern void plpgsql_adddatum(PLpgSQL_datum *new);
688 extern int plpgsql_add_initdatums(int **varnos);
689 extern void plpgsql_HashTableInit(void);
690 extern void plpgsql_compile_error_callback(void *arg);
693 * Functions in pl_handler.c
696 extern void plpgsql_init(void);
697 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
698 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
701 * Functions in pl_exec.c
704 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
705 FunctionCallInfo fcinfo);
706 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
707 TriggerData *trigdata);
708 extern void plpgsql_xact_cb(XactEvent event, void *arg);
711 * Functions for the dynamic string handling in pl_funcs.c
714 extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
715 extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
716 extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
717 extern void plpgsql_dstring_append_char(PLpgSQL_dstring *ds, char c);
718 extern char *plpgsql_dstring_get(PLpgSQL_dstring *ds);
721 * Functions for the namestack handling in pl_funcs.c
724 extern void plpgsql_ns_init(void);
725 extern bool plpgsql_ns_setlocal(bool flag);
726 extern void plpgsql_ns_push(char *label);
727 extern void plpgsql_ns_pop(void);
728 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
729 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
730 extern void plpgsql_ns_rename(char *oldname, char *newname);
733 * Other functions in pl_funcs.c
736 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
737 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
738 extern void plpgsql_dumptree(PLpgSQL_function *func);
741 * Externs in gram.y and scan.l
744 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
745 extern int plpgsql_yyparse(void);
746 extern int plpgsql_base_yylex(void);
747 extern int plpgsql_yylex(void);
748 extern void plpgsql_push_back_token(int token);
749 extern void plpgsql_yyerror(const char *message);
750 extern int plpgsql_scanner_lineno(void);
751 extern void plpgsql_scanner_init(const char *str, int functype);
752 extern void plpgsql_scanner_finish(void);
753 extern char *plpgsql_get_string_value(void);
755 #endif /* PLPGSQL_H */