1
/**********************************************************************
2
* plpgsql.h - Definitions for the PL/pgSQL
6
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.56 2004-09-16 16:58:44 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;
325
typedef struct PLpgSQL_condition
326
{ /* One EXCEPTION condition name */
327
int sqlerrstate; /* SQLSTATE code */
328
char *condname; /* condition name (for debugging) */
329
struct PLpgSQL_condition *next;
333
{ /* One EXCEPTION ... WHEN clause */
335
PLpgSQL_condition *conditions;
336
PLpgSQL_stmts *action;
341
{ /* List of WHEN clauses */
342
int exceptions_alloc; /* XXX this oughta just be a List
345
PLpgSQL_exception **exceptions;
346
} PLpgSQL_exceptions;
350
{ /* Block of statements */
355
PLpgSQL_exceptions *exceptions;
358
} PLpgSQL_stmt_block;
362
{ /* Assign statement */
367
} PLpgSQL_stmt_assign;
370
{ /* PERFORM statement */
374
} PLpgSQL_stmt_perform;
377
{ /* Get Diagnostics item */
378
int item; /* id for diagnostic value desired */
379
int target; /* where to assign it */
383
{ /* Get Diagnostics statement */
387
PLpgSQL_diag_item *dtitems;
388
} PLpgSQL_stmt_getdiag;
396
PLpgSQL_stmts *true_body;
397
PLpgSQL_stmts *false_body;
402
{ /* Unconditional LOOP statement */
411
{ /* WHILE cond LOOP statement */
417
} PLpgSQL_stmt_while;
421
{ /* FOR statement with integer loopvar */
434
{ /* FOR statement running over SELECT */
446
{ /* FOR statement running over EXECUTE */
454
} PLpgSQL_stmt_dynfors;
458
{ /* SELECT ... INTO statement */
464
} PLpgSQL_stmt_select;
468
{ /* OPEN a curvar */
472
PLpgSQL_row *returntype;
473
PLpgSQL_expr *argquery;
475
PLpgSQL_expr *dynquery;
480
{ /* FETCH curvar INTO statement */
486
} PLpgSQL_stmt_fetch;
494
} PLpgSQL_stmt_close;
498
{ /* EXIT statement */
507
{ /* RETURN statement */
513
} PLpgSQL_stmt_return;
516
{ /* RETURN NEXT statement */
522
} PLpgSQL_stmt_return_next;
525
{ /* RAISE statement */
532
} PLpgSQL_stmt_raise;
536
{ /* Generic SQL statement to execute */
539
PLpgSQL_expr *sqlstmt;
540
} PLpgSQL_stmt_execsql;
544
{ /* Dynamic SQL string to execute */
548
} PLpgSQL_stmt_dynexecute;
551
typedef struct PLpgSQL_func_hashkey
552
{ /* Hash lookup key for functions */
556
* For a trigger function, the OID of the relation triggered on is
557
* part of the hashkey --- we want to compile the trigger separately
558
* for each relation it is used with, in case the rowtype is
559
* different. Zero if not called as a trigger.
564
* We include actual argument types in the hash key to support
565
* polymorphic PLpgSQL functions. Be careful that extra positions are
568
Oid argtypes[FUNC_MAX_ARGS];
569
} PLpgSQL_func_hashkey;
572
typedef struct PLpgSQL_function
573
{ /* Complete compiled function */
576
TransactionId fn_xmin;
579
PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
584
FmgrInfo fn_retinput;
585
Oid fn_rettypioparam;
591
int fn_argvarnos[FUNC_MAX_ARGS];
600
int tg_relname_varno;
604
PLpgSQL_datum **datums;
605
PLpgSQL_stmt_block *action;
610
{ /* Runtime execution data */
613
Oid rettype; /* type of current retval */
615
Oid fn_rettype; /* info about declared function rettype */
621
TupleDesc rettupdesc;
624
Tuplestorestate *tuple_store; /* SRFs accumulate results here */
625
MemoryContext tuple_store_cxt;
633
PLpgSQL_datum **datums;
635
/* temporary state for results from evaluation of query or expr */
636
SPITupleTable *eval_tuptable;
637
uint32 eval_processed;
639
ExprContext *eval_econtext;
641
/* status information for error context reporting */
642
PLpgSQL_function *err_func; /* current func */
643
PLpgSQL_stmt *err_stmt; /* current stmt */
644
const char *err_text; /* additional state info */
648
/**********************************************************************
649
* Global variable declarations
650
**********************************************************************/
652
extern int plpgsql_DumpExecTree;
653
extern int plpgsql_SpaceScanned;
654
extern int plpgsql_nDatums;
655
extern PLpgSQL_datum **plpgsql_Datums;
657
extern int plpgsql_error_lineno;
658
extern char *plpgsql_error_funcname;
660
/* linkage to the real yytext variable */
661
extern char *plpgsql_base_yytext;
663
#define plpgsql_yytext plpgsql_base_yytext
665
extern PLpgSQL_function *plpgsql_curr_compile;
667
/**********************************************************************
668
* Function declarations
669
**********************************************************************/
672
* Functions in pl_comp.c
675
extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
677
extern int plpgsql_parse_word(char *word);
678
extern int plpgsql_parse_dblword(char *word);
679
extern int plpgsql_parse_tripword(char *word);
680
extern int plpgsql_parse_wordtype(char *word);
681
extern int plpgsql_parse_dblwordtype(char *word);
682
extern int plpgsql_parse_tripwordtype(char *word);
683
extern int plpgsql_parse_wordrowtype(char *word);
684
extern int plpgsql_parse_dblwordrowtype(char *word);
685
extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
686
extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
687
extern PLpgSQL_variable *plpgsql_build_variable(char *refname, int lineno,
690
extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
691
extern void plpgsql_adddatum(PLpgSQL_datum *new);
692
extern int plpgsql_add_initdatums(int **varnos);
693
extern void plpgsql_HashTableInit(void);
696
* Functions in pl_handler.c
699
extern void plpgsql_init(void);
700
extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
701
extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
704
* Functions in pl_exec.c
707
extern Datum plpgsql_exec_function(PLpgSQL_function *func,
708
FunctionCallInfo fcinfo);
709
extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
710
TriggerData *trigdata);
711
extern void plpgsql_xact_cb(XactEvent event, void *arg);
714
* Functions for the dynamic string handling in pl_funcs.c
717
extern void plpgsql_dstring_init(PLpgSQL_dstring *ds);
718
extern void plpgsql_dstring_free(PLpgSQL_dstring *ds);
719
extern void plpgsql_dstring_append(PLpgSQL_dstring *ds, const char *str);
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, 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 */