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