]> granicus.if.org Git - postgresql/blob - src/pl/plpgsql/src/plpgsql.h
Replace PLpgSQL_dstring by StringInfo.
[postgresql] / src / pl / plpgsql / src / plpgsql.h
1 /*-------------------------------------------------------------------------
2  *
3  * plpgsql.h            - Definitions for the PL/pgSQL
4  *                        procedural language
5  *
6  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.114 2009/07/22 02:31:38 joe Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #ifndef PLPGSQL_H
17 #define PLPGSQL_H
18
19 #include "postgres.h"
20
21 #include "access/xact.h"
22 #include "fmgr.h"
23 #include "miscadmin.h"
24 #include "commands/trigger.h"
25 #include "executor/spi.h"
26 #include "utils/tuplestore.h"
27
28 /**********************************************************************
29  * Definitions
30  **********************************************************************/
31
32 /* define our text domain for translations */
33 #undef TEXTDOMAIN
34 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
35
36 #undef _
37 #define _(x) dgettext(TEXTDOMAIN, x)
38
39 /* ----------
40  * Compiler's namestack item types
41  * ----------
42  */
43 enum
44 {
45         PLPGSQL_NSTYPE_LABEL,
46         PLPGSQL_NSTYPE_VAR,
47         PLPGSQL_NSTYPE_ROW,
48         PLPGSQL_NSTYPE_REC
49 };
50
51 /* ----------
52  * Datum array node types
53  * ----------
54  */
55 enum
56 {
57         PLPGSQL_DTYPE_VAR,
58         PLPGSQL_DTYPE_ROW,
59         PLPGSQL_DTYPE_REC,
60         PLPGSQL_DTYPE_RECFIELD,
61         PLPGSQL_DTYPE_ARRAYELEM,
62         PLPGSQL_DTYPE_EXPR,
63         PLPGSQL_DTYPE_TRIGARG
64 };
65
66 /* ----------
67  * Variants distinguished in PLpgSQL_type structs
68  * ----------
69  */
70 enum
71 {
72         PLPGSQL_TTYPE_SCALAR,           /* scalar types and domains */
73         PLPGSQL_TTYPE_ROW,                      /* composite types */
74         PLPGSQL_TTYPE_REC,                      /* RECORD pseudotype */
75         PLPGSQL_TTYPE_PSEUDO            /* other pseudotypes */
76 };
77
78 /* ----------
79  * Execution tree node types
80  * ----------
81  */
82 enum PLpgSQL_stmt_types
83 {
84         PLPGSQL_STMT_BLOCK,
85         PLPGSQL_STMT_ASSIGN,
86         PLPGSQL_STMT_IF,
87         PLPGSQL_STMT_CASE,
88         PLPGSQL_STMT_LOOP,
89         PLPGSQL_STMT_WHILE,
90         PLPGSQL_STMT_FORI,
91         PLPGSQL_STMT_FORS,
92         PLPGSQL_STMT_FORC,
93         PLPGSQL_STMT_EXIT,
94         PLPGSQL_STMT_RETURN,
95         PLPGSQL_STMT_RETURN_NEXT,
96         PLPGSQL_STMT_RETURN_QUERY,
97         PLPGSQL_STMT_RAISE,
98         PLPGSQL_STMT_EXECSQL,
99         PLPGSQL_STMT_DYNEXECUTE,
100         PLPGSQL_STMT_DYNFORS,
101         PLPGSQL_STMT_GETDIAG,
102         PLPGSQL_STMT_OPEN,
103         PLPGSQL_STMT_FETCH,
104         PLPGSQL_STMT_CLOSE,
105         PLPGSQL_STMT_PERFORM
106 };
107
108
109 /* ----------
110  * Execution node return codes
111  * ----------
112  */
113 enum
114 {
115         PLPGSQL_RC_OK,
116         PLPGSQL_RC_EXIT,
117         PLPGSQL_RC_RETURN,
118         PLPGSQL_RC_CONTINUE,
119         PLPGSQL_RC_RERAISE
120 };
121
122 /* ----------
123  * GET DIAGNOSTICS system attrs
124  * ----------
125  */
126 enum
127 {
128         PLPGSQL_GETDIAG_ROW_COUNT,
129         PLPGSQL_GETDIAG_RESULT_OID
130 };
131
132 /* --------
133  * RAISE statement options
134  * --------
135  */
136 enum
137 {
138         PLPGSQL_RAISEOPTION_ERRCODE,
139         PLPGSQL_RAISEOPTION_MESSAGE,
140         PLPGSQL_RAISEOPTION_DETAIL,
141         PLPGSQL_RAISEOPTION_HINT
142 };
143
144
145 /**********************************************************************
146  * Node and structure definitions
147  **********************************************************************/
148
149
150 typedef struct
151 {                                                               /* Postgres data type */
152         char       *typname;            /* (simple) name of the type */
153         Oid                     typoid;                 /* OID of the data type */
154         int                     ttype;                  /* PLPGSQL_TTYPE_ code */
155         int16           typlen;                 /* stuff copied from its pg_type entry */
156         bool            typbyval;
157         Oid                     typrelid;
158         Oid                     typioparam;
159         FmgrInfo        typinput;               /* lookup info for typinput function */
160         int32           atttypmod;              /* typmod (taken from someplace else) */
161 } PLpgSQL_type;
162
163
164 /*
165  * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
166  * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
167  * PLpgSQL_trigarg
168  */
169 typedef struct
170 {                                                               /* Generic datum array item             */
171         int                     dtype;
172         int                     dno;
173 } PLpgSQL_datum;
174
175 /*
176  * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
177  * fields
178  */
179 typedef struct
180 {                                                               /* Scalar or composite variable */
181         int                     dtype;
182         int                     dno;
183         char       *refname;
184         int                     lineno;
185 } PLpgSQL_variable;
186
187 typedef struct PLpgSQL_expr
188 {                                                               /* SQL Query to plan and execute        */
189         int                     dtype;
190         int                     dno;
191         char       *query;
192         SPIPlanPtr      plan;
193         Oid                *plan_argtypes;
194         /* fields for "simple expression" fast-path execution: */
195         Expr       *expr_simple_expr;           /* NULL means not a simple expr */
196         int                     expr_simple_generation; /* plancache generation we checked */
197         Oid                     expr_simple_type;               /* result type Oid, if simple */
198
199         /*
200          * if expr is simple AND prepared in current transaction,
201          * expr_simple_state is valid. Test validity by seeing if expr_simple_lxid
202          * matches current LXID.
203          */
204         ExprState  *expr_simple_state;
205         LocalTransactionId expr_simple_lxid;
206
207         /* params to pass to expr */
208         int                     nparams;
209         int                     params[1];              /* VARIABLE SIZE ARRAY ... must be last */
210 } PLpgSQL_expr;
211
212
213 typedef struct
214 {                                                               /* Scalar variable */
215         int                     dtype;
216         int                     dno;
217         char       *refname;
218         int                     lineno;
219
220         PLpgSQL_type *datatype;
221         int                     isconst;
222         int                     notnull;
223         PLpgSQL_expr *default_val;
224         PLpgSQL_expr *cursor_explicit_expr;
225         int                     cursor_explicit_argrow;
226         int                     cursor_options;
227
228         Datum           value;
229         bool            isnull;
230         bool            freeval;
231 } PLpgSQL_var;
232
233
234 typedef struct
235 {                                                               /* Row variable */
236         int                     dtype;
237         int                     dno;
238         char       *refname;
239         int                     lineno;
240
241         TupleDesc       rowtupdesc;
242
243         /*
244          * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
245          *
246          * Note: if the underlying rowtype contains a dropped column, the
247          * corresponding fieldnames[] entry will be NULL, and there is no
248          * corresponding var (varnos[] will be -1).
249          */
250         int                     nfields;
251         char      **fieldnames;
252         int                *varnos;
253 } PLpgSQL_row;
254
255
256 typedef struct
257 {                                                               /* Record variable (non-fixed structure) */
258         int                     dtype;
259         int                     dno;
260         char       *refname;
261         int                     lineno;
262
263         HeapTuple       tup;
264         TupleDesc       tupdesc;
265         bool            freetup;
266         bool            freetupdesc;
267 } PLpgSQL_rec;
268
269
270 typedef struct
271 {                                                               /* Field in record */
272         int                     dtype;
273         int                     dno;
274         char       *fieldname;
275         int                     recparentno;    /* dno of parent record */
276 } PLpgSQL_recfield;
277
278
279 typedef struct
280 {                                                               /* Element of array variable */
281         int                     dtype;
282         int                     dno;
283         PLpgSQL_expr *subscript;
284         int                     arrayparentno;  /* dno of parent array variable */
285 } PLpgSQL_arrayelem;
286
287
288 typedef struct
289 {                                                               /* Positional argument to trigger       */
290         int                     dtype;
291         int                     dno;
292         PLpgSQL_expr *argnum;
293 } PLpgSQL_trigarg;
294
295
296 typedef struct
297 {                                                               /* Item in the compilers namestack      */
298         int                     itemtype;
299         int                     itemno;
300         char            name[1];
301 } PLpgSQL_nsitem;
302
303
304 /* XXX: consider adapting this to use List */
305 typedef struct PLpgSQL_ns
306 {                                                               /* Compiler namestack level             */
307         int                     items_alloc;
308         int                     items_used;
309         PLpgSQL_nsitem **items;
310         struct PLpgSQL_ns *upper;
311 } PLpgSQL_ns;
312
313
314 typedef struct
315 {                                                               /* Generic execution node               */
316         int                     cmd_type;
317         int                     lineno;
318 } PLpgSQL_stmt;
319
320
321 typedef struct PLpgSQL_condition
322 {                                                               /* One EXCEPTION condition name */
323         int                     sqlerrstate;    /* SQLSTATE code */
324         char       *condname;           /* condition name (for debugging) */
325         struct PLpgSQL_condition *next;
326 } PLpgSQL_condition;
327
328 typedef struct
329 {
330         int                     sqlstate_varno;
331         int                     sqlerrm_varno;
332         List       *exc_list;           /* List of WHEN clauses */
333 } PLpgSQL_exception_block;
334
335 typedef struct
336 {                                                               /* One EXCEPTION ... WHEN clause */
337         int                     lineno;
338         PLpgSQL_condition *conditions;
339         List       *action;                     /* List of statements */
340 } PLpgSQL_exception;
341
342
343 typedef struct
344 {                                                               /* Block of statements                  */
345         int                     cmd_type;
346         int                     lineno;
347         char       *label;
348         List       *body;                       /* List of statements */
349         int                     n_initvars;
350         int                *initvarnos;
351         PLpgSQL_exception_block *exceptions;
352 } PLpgSQL_stmt_block;
353
354
355 typedef struct
356 {                                                               /* Assign statement                     */
357         int                     cmd_type;
358         int                     lineno;
359         int                     varno;
360         PLpgSQL_expr *expr;
361 } PLpgSQL_stmt_assign;
362
363 typedef struct
364 {                                                               /* PERFORM statement            */
365         int                     cmd_type;
366         int                     lineno;
367         PLpgSQL_expr *expr;
368 } PLpgSQL_stmt_perform;
369
370 typedef struct
371 {                                                               /* Get Diagnostics item         */
372         int                     kind;                   /* id for diagnostic value desired */
373         int                     target;                 /* where to assign it */
374 } PLpgSQL_diag_item;
375
376 typedef struct
377 {                                                               /* Get Diagnostics statement            */
378         int                     cmd_type;
379         int                     lineno;
380         List       *diag_items;         /* List of PLpgSQL_diag_item */
381 } PLpgSQL_stmt_getdiag;
382
383
384 typedef struct
385 {                                                               /* IF statement                         */
386         int                     cmd_type;
387         int                     lineno;
388         PLpgSQL_expr *cond;
389         List       *true_body;          /* List of statements */
390         List       *false_body;         /* List of statements */
391 } PLpgSQL_stmt_if;
392
393
394 typedef struct                                  /* CASE statement */
395 {
396         int                     cmd_type;
397         int                     lineno;
398         PLpgSQL_expr *t_expr;           /* test expression, or NULL if none */
399         int                     t_varno;                /* var to store test expression value into */
400         List       *case_when_list; /* List of PLpgSQL_case_when structs */
401         bool            have_else;              /* flag needed because list could be empty */
402         List       *else_stmts;         /* List of statements */
403 } PLpgSQL_stmt_case;
404
405 typedef struct                                  /* one arm of CASE statement */
406 {
407         int                     lineno;
408         PLpgSQL_expr *expr;                     /* boolean expression for this case */
409         List       *stmts;                      /* List of statements */
410 } PLpgSQL_case_when;
411
412
413 typedef struct
414 {                                                               /* Unconditional LOOP statement         */
415         int                     cmd_type;
416         int                     lineno;
417         char       *label;
418         List       *body;                       /* List of statements */
419 } PLpgSQL_stmt_loop;
420
421
422 typedef struct
423 {                                                               /* WHILE cond LOOP statement            */
424         int                     cmd_type;
425         int                     lineno;
426         char       *label;
427         PLpgSQL_expr *cond;
428         List       *body;                       /* List of statements */
429 } PLpgSQL_stmt_while;
430
431
432 typedef struct
433 {                                                               /* FOR statement with integer loopvar   */
434         int                     cmd_type;
435         int                     lineno;
436         char       *label;
437         PLpgSQL_var *var;
438         PLpgSQL_expr *lower;
439         PLpgSQL_expr *upper;
440         PLpgSQL_expr *step;                     /* NULL means default (ie, BY 1) */
441         int                     reverse;
442         List       *body;                       /* List of statements */
443 } PLpgSQL_stmt_fori;
444
445
446 /*
447  * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
448  * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
449  * and PLpgSQL_dynfors.
450  */
451 typedef struct
452 {
453         int                     cmd_type;
454         int                     lineno;
455         char       *label;
456         PLpgSQL_rec *rec;
457         PLpgSQL_row *row;
458         List       *body;                       /* List of statements */
459 } PLpgSQL_stmt_forq;
460
461 typedef struct
462 {                                                               /* FOR statement running over SELECT    */
463         int                     cmd_type;
464         int                     lineno;
465         char       *label;
466         PLpgSQL_rec *rec;
467         PLpgSQL_row *row;
468         List       *body;                       /* List of statements */
469         /* end of fields that must match PLpgSQL_stmt_forq */
470         PLpgSQL_expr *query;
471 } PLpgSQL_stmt_fors;
472
473 typedef struct
474 {                                                               /* FOR statement running over cursor    */
475         int                     cmd_type;
476         int                     lineno;
477         char       *label;
478         PLpgSQL_rec *rec;
479         PLpgSQL_row *row;
480         List       *body;                       /* List of statements */
481         /* end of fields that must match PLpgSQL_stmt_forq */
482         int                     curvar;
483         PLpgSQL_expr *argquery;         /* cursor arguments if any */
484 } PLpgSQL_stmt_forc;
485
486 typedef struct
487 {                                                               /* FOR statement running over EXECUTE   */
488         int                     cmd_type;
489         int                     lineno;
490         char       *label;
491         PLpgSQL_rec *rec;
492         PLpgSQL_row *row;
493         List       *body;                       /* List of statements */
494         /* end of fields that must match PLpgSQL_stmt_forq */
495         PLpgSQL_expr *query;
496         List       *params;                     /* USING expressions */
497 } PLpgSQL_stmt_dynfors;
498
499
500 typedef struct
501 {                                                               /* OPEN a curvar                                        */
502         int                     cmd_type;
503         int                     lineno;
504         int                     curvar;
505         int                     cursor_options;
506         PLpgSQL_row *returntype;
507         PLpgSQL_expr *argquery;
508         PLpgSQL_expr *query;
509         PLpgSQL_expr *dynquery;
510 } PLpgSQL_stmt_open;
511
512
513 typedef struct
514 {                                                               /* FETCH or MOVE statement */
515         int                     cmd_type;
516         int                     lineno;
517         PLpgSQL_rec *rec;                       /* target, as record or row */
518         PLpgSQL_row *row;
519         int                     curvar;                 /* cursor variable to fetch from */
520         FetchDirection direction;       /* fetch direction */
521         int                     how_many;               /* count, if constant (expr is NULL) */
522         PLpgSQL_expr *expr;                     /* count, if expression */
523         bool            is_move;                /* is this a fetch or move? */
524 } PLpgSQL_stmt_fetch;
525
526
527 typedef struct
528 {                                                               /* CLOSE curvar                                         */
529         int                     cmd_type;
530         int                     lineno;
531         int                     curvar;
532 } PLpgSQL_stmt_close;
533
534
535 typedef struct
536 {                                                               /* EXIT or CONTINUE statement                   */
537         int                     cmd_type;
538         int                     lineno;
539         bool            is_exit;                /* Is this an exit or a continue? */
540         char       *label;                      /* NULL if it's an unlabelled EXIT/CONTINUE */
541         PLpgSQL_expr *cond;
542 } PLpgSQL_stmt_exit;
543
544
545 typedef struct
546 {                                                               /* RETURN statement                     */
547         int                     cmd_type;
548         int                     lineno;
549         PLpgSQL_expr *expr;
550         int                     retvarno;
551 } PLpgSQL_stmt_return;
552
553 typedef struct
554 {                                                               /* RETURN NEXT statement */
555         int                     cmd_type;
556         int                     lineno;
557         PLpgSQL_expr *expr;
558         int                     retvarno;
559 } PLpgSQL_stmt_return_next;
560
561 typedef struct
562 {                                                               /* RETURN QUERY statement */
563         int                     cmd_type;
564         int                     lineno;
565         PLpgSQL_expr *query;            /* if static query */
566         PLpgSQL_expr *dynquery;         /* if dynamic query (RETURN QUERY EXECUTE) */
567         List       *params;                     /* USING arguments for dynamic query */
568 } PLpgSQL_stmt_return_query;
569
570 typedef struct
571 {                                                               /* RAISE statement                      */
572         int                     cmd_type;
573         int                     lineno;
574         int                     elog_level;
575         char       *condname;           /* condition name, SQLSTATE, or NULL */
576         char       *message;            /* old-style message format literal, or NULL */
577         List       *params;                     /* list of expressions for old-style message */
578         List       *options;            /* list of PLpgSQL_raise_option */
579 } PLpgSQL_stmt_raise;
580
581 typedef struct
582 {                                                               /* RAISE statement option */
583         int                     opt_type;
584         PLpgSQL_expr *expr;
585 } PLpgSQL_raise_option;
586
587
588 typedef struct
589 {                                                               /* Generic SQL statement to execute */
590         int                     cmd_type;
591         int                     lineno;
592         PLpgSQL_expr *sqlstmt;
593         bool            mod_stmt;               /* is the stmt INSERT/UPDATE/DELETE? */
594         /* note: mod_stmt is set when we plan the query */
595         bool            into;                   /* INTO supplied? */
596         bool            strict;                 /* INTO STRICT flag */
597         PLpgSQL_rec *rec;                       /* INTO target, if record */
598         PLpgSQL_row *row;                       /* INTO target, if row */
599 } PLpgSQL_stmt_execsql;
600
601
602 typedef struct
603 {                                                               /* Dynamic SQL string to execute */
604         int                     cmd_type;
605         int                     lineno;
606         PLpgSQL_expr *query;            /* string expression */
607         bool            into;                   /* INTO supplied? */
608         bool            strict;                 /* INTO STRICT flag */
609         PLpgSQL_rec *rec;                       /* INTO target, if record */
610         PLpgSQL_row *row;                       /* INTO target, if row */
611         List       *params;                     /* USING expressions */
612 } PLpgSQL_stmt_dynexecute;
613
614
615 typedef struct PLpgSQL_func_hashkey
616 {                                                               /* Hash lookup key for functions */
617         Oid                     funcOid;
618
619         bool            isTrigger;              /* true if called as a trigger */
620
621         /* be careful that pad bytes in this struct get zeroed! */
622
623         /*
624          * For a trigger function, the OID of the relation triggered on is part of
625          * the hashkey --- we want to compile the trigger separately for each
626          * relation it is used with, in case the rowtype is different.  Zero if
627          * not called as a trigger.
628          */
629         Oid                     trigrelOid;
630
631         /*
632          * We include actual argument types in the hash key to support polymorphic
633          * PLpgSQL functions.  Be careful that extra positions are zeroed!
634          */
635         Oid                     argtypes[FUNC_MAX_ARGS];
636 } PLpgSQL_func_hashkey;
637
638
639 typedef struct PLpgSQL_function
640 {                                                               /* Complete compiled function     */
641         char       *fn_name;
642         Oid                     fn_oid;
643         TransactionId fn_xmin;
644         ItemPointerData fn_tid;
645         bool            fn_is_trigger;
646         PLpgSQL_func_hashkey *fn_hashkey;       /* back-link to hashtable key */
647         MemoryContext fn_cxt;
648
649         Oid                     fn_rettype;
650         int                     fn_rettyplen;
651         bool            fn_retbyval;
652         FmgrInfo        fn_retinput;
653         Oid                     fn_rettypioparam;
654         bool            fn_retistuple;
655         bool            fn_retset;
656         bool            fn_readonly;
657
658         int                     fn_nargs;
659         int                     fn_argvarnos[FUNC_MAX_ARGS];
660         int                     out_param_varno;
661         int                     found_varno;
662         int                     new_varno;
663         int                     old_varno;
664         int                     tg_name_varno;
665         int                     tg_when_varno;
666         int                     tg_level_varno;
667         int                     tg_op_varno;
668         int                     tg_relid_varno;
669         int                     tg_relname_varno;
670         int                     tg_table_name_varno;
671         int                     tg_table_schema_varno;
672         int                     tg_nargs_varno;
673
674         int                     ndatums;
675         PLpgSQL_datum **datums;
676         PLpgSQL_stmt_block *action;
677
678         unsigned long use_count;
679 } PLpgSQL_function;
680
681
682 typedef struct
683 {                                                               /* Runtime execution data       */
684         Datum           retval;
685         bool            retisnull;
686         Oid                     rettype;                /* type of current retval */
687
688         Oid                     fn_rettype;             /* info about declared function rettype */
689         bool            retistuple;
690         bool            retisset;
691
692         bool            readonly_func;
693
694         TupleDesc       rettupdesc;
695         char       *exitlabel;          /* the "target" label of the current EXIT or
696                                                                  * CONTINUE stmt, if any */
697
698         Tuplestorestate *tuple_store;           /* SRFs accumulate results here */
699         MemoryContext tuple_store_cxt;
700         ReturnSetInfo *rsi;
701
702         int                     trig_nargs;
703         Datum      *trig_argv;
704
705         int                     found_varno;
706         int                     ndatums;
707         PLpgSQL_datum **datums;
708
709         /* temporary state for results from evaluation of query or expr */
710         SPITupleTable *eval_tuptable;
711         uint32          eval_processed;
712         Oid                     eval_lastoid;
713         ExprContext *eval_econtext; /* for executing simple expressions */
714
715         /* status information for error context reporting */
716         PLpgSQL_function *err_func; /* current func */
717         PLpgSQL_stmt *err_stmt;         /* current stmt */
718         const char *err_text;           /* additional state info */
719         void       *plugin_info;        /* reserved for use by optional plugin */
720 } PLpgSQL_execstate;
721
722
723 /*
724  * A PLpgSQL_plugin structure represents an instrumentation plugin.
725  * To instrument PL/pgSQL, a plugin library must access the rendezvous
726  * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
727  * Typically the struct could just be static data in the plugin library.
728  * We expect that a plugin would do this at library load time (_PG_init()).
729  * It must also be careful to set the rendezvous variable back to NULL
730  * if it is unloaded (_PG_fini()).
731  *
732  * This structure is basically a collection of function pointers --- at
733  * various interesting points in pl_exec.c, we call these functions
734  * (if the pointers are non-NULL) to give the plugin a chance to watch
735  * what we are doing.
736  *
737  *      func_setup is called when we start a function, before we've initialized
738  *      the local variables defined by the function.
739  *
740  *      func_beg is called when we start a function, after we've initialized
741  *      the local variables.
742  *
743  *      func_end is called at the end of a function.
744  *
745  *      stmt_beg and stmt_end are called before and after (respectively) each
746  *      statement.
747  *
748  * Also, immediately before any call to func_setup, PL/pgSQL fills in the
749  * error_callback and assign_expr fields with pointers to its own
750  * plpgsql_exec_error_callback and exec_assign_expr functions.  This is
751  * a somewhat ad-hoc expedient to simplify life for debugger plugins.
752  */
753
754 typedef struct
755 {
756         /* Function pointers set up by the plugin */
757         void            (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
758         void            (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
759         void            (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
760         void            (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
761         void            (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
762
763         /* Function pointers set by PL/pgSQL itself */
764         void            (*error_callback) (void *arg);
765         void            (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
766                                                                                         PLpgSQL_expr *expr);
767 } PLpgSQL_plugin;
768
769
770 /**********************************************************************
771  * Global variable declarations
772  **********************************************************************/
773
774 extern bool plpgsql_DumpExecTree;
775 extern bool plpgsql_SpaceScanned;
776 extern int      plpgsql_nDatums;
777 extern PLpgSQL_datum **plpgsql_Datums;
778
779 extern int      plpgsql_error_lineno;
780 extern char *plpgsql_error_funcname;
781
782 /* linkage to the real yytext variable */
783 extern char *plpgsql_base_yytext;
784
785 #define yytext plpgsql_base_yytext
786
787 extern PLpgSQL_function *plpgsql_curr_compile;
788 extern bool plpgsql_check_syntax;
789 extern MemoryContext compile_tmp_cxt;
790
791 extern PLpgSQL_plugin **plugin_ptr;
792
793 /**********************************************************************
794  * Function declarations
795  **********************************************************************/
796
797 /* ----------
798  * Functions in pl_comp.c
799  * ----------
800  */
801 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
802                                 bool forValidator);
803 extern int      plpgsql_parse_word(const char *word);
804 extern int      plpgsql_parse_dblword(const char *word);
805 extern int      plpgsql_parse_tripword(const char *word);
806 extern int      plpgsql_parse_wordtype(char *word);
807 extern int      plpgsql_parse_dblwordtype(char *word);
808 extern int      plpgsql_parse_tripwordtype(char *word);
809 extern int      plpgsql_parse_wordrowtype(char *word);
810 extern int      plpgsql_parse_dblwordrowtype(char *word);
811 extern PLpgSQL_type *plpgsql_parse_datatype(const char *string);
812 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
813 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
814                                            PLpgSQL_type *dtype,
815                                            bool add2namespace);
816 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
817                                          bool add2namespace);
818 extern int plpgsql_recognize_err_condition(const char *condname,
819                                                                 bool allow_sqlstate);
820 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
821 extern void plpgsql_adddatum(PLpgSQL_datum *new);
822 extern int      plpgsql_add_initdatums(int **varnos);
823 extern void plpgsql_HashTableInit(void);
824 extern void plpgsql_compile_error_callback(void *arg);
825
826 /* ----------
827  * Functions in pl_handler.c
828  * ----------
829  */
830 extern void _PG_init(void);
831 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
832 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
833
834 /* ----------
835  * Functions in pl_exec.c
836  * ----------
837  */
838 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
839                                           FunctionCallInfo fcinfo);
840 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
841                                          TriggerData *trigdata);
842 extern void plpgsql_xact_cb(XactEvent event, void *arg);
843 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
844                                    SubTransactionId parentSubid, void *arg);
845
846 /* ----------
847  * Functions for namestack handling in pl_funcs.c
848  * ----------
849  */
850 extern void plpgsql_ns_init(void);
851 extern bool plpgsql_ns_setlocal(bool flag);
852 extern void plpgsql_ns_push(const char *label);
853 extern void plpgsql_ns_pop(void);
854 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
855 extern PLpgSQL_nsitem *plpgsql_ns_lookup(const char *name1, const char *name2,
856                                   const char *name3, int *names_used);
857 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(const char *name);
858 extern void plpgsql_ns_rename(char *oldname, char *newname);
859
860 /* ----------
861  * Other functions in pl_funcs.c
862  * ----------
863  */
864 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
865 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
866 extern void plpgsql_dumptree(PLpgSQL_function *func);
867
868 /* ----------
869  * Externs in gram.y and scan.l
870  * ----------
871  */
872 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
873 extern int      plpgsql_yyparse(void);
874 extern int      plpgsql_base_yylex(void);
875 extern int      plpgsql_yylex(void);
876 extern void plpgsql_push_back_token(int token);
877 extern void plpgsql_yyerror(const char *message);
878 extern int      plpgsql_scanner_lineno(void);
879 extern void plpgsql_scanner_init(const char *str);
880 extern void plpgsql_scanner_finish(void);
881
882 #endif   /* PLPGSQL_H */