1 /**********************************************************************
2 * plpgsql.h - Definitions for the PL/pgSQL
6 * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.48 2004/07/31 07:39:20 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 * Compilers 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
132 * GET DIAGNOSTICS system attrs
137 PLPGSQL_GETDIAG_ROW_COUNT,
138 PLPGSQL_GETDIAG_RESULT_OID
142 /**********************************************************************
143 * Node and structure definitions
144 **********************************************************************/
148 { /* Dynamic string control structure */
156 { /* Postgres data type */
157 char *typname; /* (simple) name of the type */
158 Oid typoid; /* OID of the data type */
159 int ttype; /* PLPGSQL_TTYPE_ code */
160 int16 typlen; /* stuff copied from its pg_type entry */
164 FmgrInfo typinput; /* lookup info for typinput function */
165 int32 atttypmod; /* typmod (taken from someplace else) */
170 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
171 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
175 { /* Generic datum array item */
181 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
185 { /* Scalar or composite variable */
192 typedef struct PLpgSQL_expr
193 { /* SQL Query to plan and execute */
199 /* fields for "simple expression" fast-path execution: */
200 Expr *expr_simple_expr; /* NULL means not a simple expr */
201 Oid expr_simple_type;
202 /* if expr is simple AND in use in current xact, these fields are set: */
203 ExprState *expr_simple_state;
204 struct PLpgSQL_expr *expr_simple_next;
205 /* params to pass to expr */
207 int params[1]; /* VARIABLE SIZE ARRAY ... must be last */
212 { /* Scalar variable */
218 PLpgSQL_type *datatype;
221 PLpgSQL_expr *default_val;
222 PLpgSQL_expr *cursor_explicit_expr;
223 int cursor_explicit_argrow;
238 TupleDesc rowtupdesc;
241 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
243 * Note: if the underlying rowtype contains a dropped column, the
244 * corresponding fieldnames[] entry will be NULL, and there is no
245 * corresponding var (varnos[] will be -1).
254 { /* Record variable (non-fixed structure) */
268 { /* Field in record */
272 int recparentno; /* dno of parent record */
277 { /* Element of array variable */
280 PLpgSQL_expr *subscript;
281 int arrayparentno; /* dno of parent array variable */
286 { /* Positional argument to trigger */
289 PLpgSQL_expr *argnum;
294 { /* Item in the compilers namestack */
301 typedef struct PLpgSQL_ns
302 { /* Compiler namestack level */
305 PLpgSQL_nsitem **items;
306 struct PLpgSQL_ns *upper;
311 { /* Generic execution node */
318 { /* List of execution nodes */
319 int stmts_alloc; /* XXX this oughta just be a List ... */
321 PLpgSQL_stmt **stmts;
326 { /* One EXCEPTION ... WHEN clause */
329 PLpgSQL_stmts *action;
334 { /* List of WHEN clauses */
335 int exceptions_alloc; /* XXX this oughta just be a List ... */
337 PLpgSQL_exception **exceptions;
338 } PLpgSQL_exceptions;
342 { /* Block of statements */
347 PLpgSQL_exceptions *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 item; /* id for diagnostic value desired */
371 int target; /* where to assign it */
375 { /* Get Diagnostics statement */
379 PLpgSQL_diag_item *dtitems;
380 } PLpgSQL_stmt_getdiag;
388 PLpgSQL_stmts *true_body;
389 PLpgSQL_stmts *false_body;
394 { /* Unconditional LOOP statement */
403 { /* WHILE cond LOOP statement */
409 } PLpgSQL_stmt_while;
413 { /* FOR statement with integer loopvar */
426 { /* FOR statement running over SELECT */
438 { /* FOR statement running over EXECUTE */
446 } PLpgSQL_stmt_dynfors;
450 { /* SELECT ... INTO statement */
456 } PLpgSQL_stmt_select;
460 { /* OPEN a curvar */
464 PLpgSQL_row *returntype;
465 PLpgSQL_expr *argquery;
467 PLpgSQL_expr *dynquery;
472 { /* FETCH curvar INTO statement */
478 } PLpgSQL_stmt_fetch;
486 } PLpgSQL_stmt_close;
490 { /* EXIT statement */
499 { /* RETURN statement */
505 } PLpgSQL_stmt_return;
508 { /* RETURN NEXT statement */
514 } PLpgSQL_stmt_return_next;
517 { /* RAISE statement */
524 } PLpgSQL_stmt_raise;
528 { /* Generic SQL statement to execute */
531 PLpgSQL_expr *sqlstmt;
532 } PLpgSQL_stmt_execsql;
536 { /* Dynamic SQL string to execute */
540 } PLpgSQL_stmt_dynexecute;
543 typedef struct PLpgSQL_func_hashkey
544 { /* Hash lookup key for functions */
548 * For a trigger function, the OID of the relation triggered on is part
549 * of the hashkey --- we want to compile the trigger separately for each
550 * relation it is used with, in case the rowtype is different. Zero if
551 * not called as a trigger.
556 * We include actual argument types in the hash key to support
557 * polymorphic PLpgSQL functions. Be careful that extra positions are
560 Oid argtypes[FUNC_MAX_ARGS];
561 } PLpgSQL_func_hashkey;
564 typedef struct PLpgSQL_function
565 { /* Complete compiled function */
568 TransactionId fn_xmin;
571 PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
576 FmgrInfo fn_retinput;
577 Oid fn_rettypioparam;
582 int fn_argvarnos[FUNC_MAX_ARGS];
591 int tg_relname_varno;
595 PLpgSQL_datum **datums;
596 PLpgSQL_stmt_block *action;
601 { /* Runtime execution data */
604 Oid rettype; /* type of current retval */
606 Oid fn_rettype; /* info about declared function rettype */
610 TupleDesc rettupdesc;
613 Tuplestorestate *tuple_store; /* SRFs accumulate results here */
614 MemoryContext tuple_store_cxt;
622 PLpgSQL_datum **datums;
624 /* temporary state for results from evaluation of query or expr */
625 SPITupleTable *eval_tuptable;
626 uint32 eval_processed;
628 ExprContext *eval_econtext;
630 /* status information for error context reporting */
631 PLpgSQL_function *err_func; /* current func */
632 PLpgSQL_stmt *err_stmt; /* current stmt */
633 const char *err_text; /* additional state info */
637 /**********************************************************************
638 * Global variable declarations
639 **********************************************************************/
641 extern int plpgsql_DumpExecTree;
642 extern int plpgsql_SpaceScanned;
643 extern int plpgsql_nDatums;
644 extern PLpgSQL_datum **plpgsql_Datums;
646 extern int plpgsql_error_lineno;
647 extern char *plpgsql_error_funcname;
649 /* linkage to the real yytext variable */
650 extern char *plpgsql_base_yytext;
652 #define plpgsql_yytext plpgsql_base_yytext
654 extern PLpgSQL_function *plpgsql_curr_compile;
656 /**********************************************************************
657 * Function declarations
658 **********************************************************************/
661 * Functions in pl_comp.c
664 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
666 extern int plpgsql_parse_word(char *word);
667 extern int plpgsql_parse_dblword(char *word);
668 extern int plpgsql_parse_tripword(char *word);
669 extern int plpgsql_parse_wordtype(char *word);
670 extern int plpgsql_parse_dblwordtype(char *word);
671 extern int plpgsql_parse_tripwordtype(char *word);
672 extern int plpgsql_parse_wordrowtype(char *word);
673 extern int plpgsql_parse_dblwordrowtype(char *word);
674 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
675 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
676 extern PLpgSQL_variable *plpgsql_build_variable(char *refname, int lineno,
679 extern void plpgsql_adddatum(PLpgSQL_datum *new);
680 extern int plpgsql_add_initdatums(int **varnos);
681 extern void plpgsql_HashTableInit(void);
684 * Functions in pl_handler.c
687 extern void plpgsql_init(void);
688 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
689 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
692 * Functions in pl_exec.c
695 extern Datum plpgsql_exec_function(PLpgSQL_function * func,
696 FunctionCallInfo fcinfo);
697 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function * func,
698 TriggerData *trigdata);
699 extern void plpgsql_eoxact(bool isCommit, void *arg);
702 * Functions for the dynamic string handling in pl_funcs.c
705 extern void plpgsql_dstring_init(PLpgSQL_dstring * ds);
706 extern void plpgsql_dstring_free(PLpgSQL_dstring * ds);
707 extern void plpgsql_dstring_append(PLpgSQL_dstring * ds, char *str);
708 extern char *plpgsql_dstring_get(PLpgSQL_dstring * ds);
711 * Functions for the namestack handling in pl_funcs.c
714 extern void plpgsql_ns_init(void);
715 extern bool plpgsql_ns_setlocal(bool flag);
716 extern void plpgsql_ns_push(char *label);
717 extern void plpgsql_ns_pop(void);
718 extern void plpgsql_ns_additem(int itemtype, int itemno, char *name);
719 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
720 extern void plpgsql_ns_rename(char *oldname, char *newname);
723 * Other functions in pl_funcs.c
726 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
727 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt * stmt);
728 extern void plpgsql_dumptree(PLpgSQL_function * func);
731 * Externs in gram.y and scan.l
734 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
735 extern int plpgsql_yyparse(void);
736 extern int plpgsql_base_yylex(void);
737 extern int plpgsql_yylex(void);
738 extern void plpgsql_push_back_token(int token);
739 extern void plpgsql_yyerror(const char *message);
740 extern int plpgsql_scanner_lineno(void);
741 extern void plpgsql_scanner_init(const char *str, int functype);
742 extern void plpgsql_scanner_finish(void);
743 extern char *plpgsql_get_string_value(void);
745 #endif /* PLPGSQL_H */