OSDN Git Service

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