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