OSDN Git Service

Fix plpgsql's handling of "simple" expression evaluation.
[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 and expr_simple_in_use are valid. Test validity by
219          * seeing if expr_simple_lxid matches current LXID.  (If not,
220          * expr_simple_state probably points at garbage!)
221          */
222         ExprState  *expr_simple_state;          /* eval tree for expr_simple_expr */
223         bool            expr_simple_in_use;             /* true if eval tree is active */
224         LocalTransactionId expr_simple_lxid;
225 } PLpgSQL_expr;
226
227
228 typedef struct
229 {                                                               /* Scalar variable */
230         int                     dtype;
231         int                     dno;
232         char       *refname;
233         int                     lineno;
234
235         PLpgSQL_type *datatype;
236         int                     isconst;
237         int                     notnull;
238         PLpgSQL_expr *default_val;
239         PLpgSQL_expr *cursor_explicit_expr;
240         int                     cursor_explicit_argrow;
241         int                     cursor_options;
242
243         Datum           value;
244         bool            isnull;
245         bool            freeval;
246 } PLpgSQL_var;
247
248
249 typedef struct
250 {                                                               /* Row variable */
251         int                     dtype;
252         int                     dno;
253         char       *refname;
254         int                     lineno;
255
256         TupleDesc       rowtupdesc;
257
258         /*
259          * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
260          *
261          * Note: if the underlying rowtype contains a dropped column, the
262          * corresponding fieldnames[] entry will be NULL, and there is no
263          * corresponding var (varnos[] will be -1).
264          */
265         int                     nfields;
266         char      **fieldnames;
267         int                *varnos;
268 } PLpgSQL_row;
269
270
271 typedef struct
272 {                                                               /* Record variable (non-fixed structure) */
273         int                     dtype;
274         int                     dno;
275         char       *refname;
276         int                     lineno;
277
278         HeapTuple       tup;
279         TupleDesc       tupdesc;
280         bool            freetup;
281         bool            freetupdesc;
282 } PLpgSQL_rec;
283
284
285 typedef struct
286 {                                                               /* Field in record */
287         int                     dtype;
288         int                     dno;
289         char       *fieldname;
290         int                     recparentno;    /* dno of parent record */
291 } PLpgSQL_recfield;
292
293
294 typedef struct
295 {                                                               /* Element of array variable */
296         int                     dtype;
297         int                     dno;
298         PLpgSQL_expr *subscript;
299         int                     arrayparentno;  /* dno of parent array variable */
300 } PLpgSQL_arrayelem;
301
302
303 typedef struct PLpgSQL_nsitem
304 {                                                               /* Item in the compilers namespace tree */
305         int                     itemtype;
306         int                     itemno;
307         struct PLpgSQL_nsitem *prev;
308         char            name[1];                /* actually, as long as needed */
309 } PLpgSQL_nsitem;
310
311
312 typedef struct
313 {                                                               /* Generic execution node               */
314         int                     cmd_type;
315         int                     lineno;
316 } PLpgSQL_stmt;
317
318
319 typedef struct PLpgSQL_condition
320 {                                                               /* One EXCEPTION condition name */
321         int                     sqlerrstate;    /* SQLSTATE code */
322         char       *condname;           /* condition name (for debugging) */
323         struct PLpgSQL_condition *next;
324 } PLpgSQL_condition;
325
326 typedef struct
327 {
328         int                     sqlstate_varno;
329         int                     sqlerrm_varno;
330         List       *exc_list;           /* List of WHEN clauses */
331 } PLpgSQL_exception_block;
332
333 typedef struct
334 {                                                               /* One EXCEPTION ... WHEN clause */
335         int                     lineno;
336         PLpgSQL_condition *conditions;
337         List       *action;                     /* List of statements */
338 } PLpgSQL_exception;
339
340
341 typedef struct
342 {                                                               /* Block of statements                  */
343         int                     cmd_type;
344         int                     lineno;
345         char       *label;
346         List       *body;                       /* List of statements */
347         int                     n_initvars;
348         int                *initvarnos;
349         PLpgSQL_exception_block *exceptions;
350 } PLpgSQL_stmt_block;
351
352
353 typedef struct
354 {                                                               /* Assign statement                     */
355         int                     cmd_type;
356         int                     lineno;
357         int                     varno;
358         PLpgSQL_expr *expr;
359 } PLpgSQL_stmt_assign;
360
361 typedef struct
362 {                                                               /* PERFORM statement            */
363         int                     cmd_type;
364         int                     lineno;
365         PLpgSQL_expr *expr;
366 } PLpgSQL_stmt_perform;
367
368 typedef struct
369 {                                                               /* Get Diagnostics item         */
370         int                     kind;                   /* id for diagnostic value desired */
371         int                     target;                 /* where to assign it */
372 } PLpgSQL_diag_item;
373
374 typedef struct
375 {                                                               /* Get Diagnostics statement            */
376         int                     cmd_type;
377         int                     lineno;
378         List       *diag_items;         /* List of PLpgSQL_diag_item */
379 } PLpgSQL_stmt_getdiag;
380
381
382 typedef struct
383 {                                                               /* IF statement                         */
384         int                     cmd_type;
385         int                     lineno;
386         PLpgSQL_expr *cond;
387         List       *true_body;          /* List of statements */
388         List       *false_body;         /* List of statements */
389 } PLpgSQL_stmt_if;
390
391
392 typedef struct                                  /* CASE statement */
393 {
394         int                     cmd_type;
395         int                     lineno;
396         PLpgSQL_expr *t_expr;           /* test expression, or NULL if none */
397         int                     t_varno;                /* var to store test expression value into */
398         List       *case_when_list; /* List of PLpgSQL_case_when structs */
399         bool            have_else;              /* flag needed because list could be empty */
400         List       *else_stmts;         /* List of statements */
401 } PLpgSQL_stmt_case;
402
403 typedef struct                                  /* one arm of CASE statement */
404 {
405         int                     lineno;
406         PLpgSQL_expr *expr;                     /* boolean expression for this case */
407         List       *stmts;                      /* List of statements */
408 } PLpgSQL_case_when;
409
410
411 typedef struct
412 {                                                               /* Unconditional LOOP statement         */
413         int                     cmd_type;
414         int                     lineno;
415         char       *label;
416         List       *body;                       /* List of statements */
417 } PLpgSQL_stmt_loop;
418
419
420 typedef struct
421 {                                                               /* WHILE cond LOOP statement            */
422         int                     cmd_type;
423         int                     lineno;
424         char       *label;
425         PLpgSQL_expr *cond;
426         List       *body;                       /* List of statements */
427 } PLpgSQL_stmt_while;
428
429
430 typedef struct
431 {                                                               /* FOR statement with integer loopvar   */
432         int                     cmd_type;
433         int                     lineno;
434         char       *label;
435         PLpgSQL_var *var;
436         PLpgSQL_expr *lower;
437         PLpgSQL_expr *upper;
438         PLpgSQL_expr *step;                     /* NULL means default (ie, BY 1) */
439         int                     reverse;
440         List       *body;                       /* List of statements */
441 } PLpgSQL_stmt_fori;
442
443
444 /*
445  * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
446  * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
447  * and PLpgSQL_dynfors.
448  */
449 typedef struct
450 {
451         int                     cmd_type;
452         int                     lineno;
453         char       *label;
454         PLpgSQL_rec *rec;
455         PLpgSQL_row *row;
456         List       *body;                       /* List of statements */
457 } PLpgSQL_stmt_forq;
458
459 typedef struct
460 {                                                               /* FOR statement running over SELECT    */
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         /* end of fields that must match PLpgSQL_stmt_forq */
468         PLpgSQL_expr *query;
469 } PLpgSQL_stmt_fors;
470
471 typedef struct
472 {                                                               /* FOR statement running over cursor    */
473         int                     cmd_type;
474         int                     lineno;
475         char       *label;
476         PLpgSQL_rec *rec;
477         PLpgSQL_row *row;
478         List       *body;                       /* List of statements */
479         /* end of fields that must match PLpgSQL_stmt_forq */
480         int                     curvar;
481         PLpgSQL_expr *argquery;         /* cursor arguments if any */
482 } PLpgSQL_stmt_forc;
483
484 typedef struct
485 {                                                               /* FOR statement running over EXECUTE   */
486         int                     cmd_type;
487         int                     lineno;
488         char       *label;
489         PLpgSQL_rec *rec;
490         PLpgSQL_row *row;
491         List       *body;                       /* List of statements */
492         /* end of fields that must match PLpgSQL_stmt_forq */
493         PLpgSQL_expr *query;
494         List       *params;                     /* USING expressions */
495 } PLpgSQL_stmt_dynfors;
496
497
498 typedef struct
499 {                                                               /* OPEN a curvar                                        */
500         int                     cmd_type;
501         int                     lineno;
502         int                     curvar;
503         int                     cursor_options;
504         PLpgSQL_row *returntype;
505         PLpgSQL_expr *argquery;
506         PLpgSQL_expr *query;
507         PLpgSQL_expr *dynquery;
508         List       *params;                     /* USING expressions */
509 } PLpgSQL_stmt_open;
510
511
512 typedef struct
513 {                                                               /* FETCH or MOVE statement */
514         int                     cmd_type;
515         int                     lineno;
516         PLpgSQL_rec *rec;                       /* target, as record or row */
517         PLpgSQL_row *row;
518         int                     curvar;                 /* cursor variable to fetch from */
519         FetchDirection direction;       /* fetch direction */
520         long            how_many;               /* count, if constant (expr is NULL) */
521         PLpgSQL_expr *expr;                     /* count, if expression */
522         bool            is_move;                /* is this a fetch or move? */
523         bool            returns_multiple_rows;  /* can return more than one row? */
524 } PLpgSQL_stmt_fetch;
525
526
527 typedef struct
528 {                                                               /* CLOSE curvar                                         */
529         int                     cmd_type;
530         int                     lineno;
531         int                     curvar;
532 } PLpgSQL_stmt_close;
533
534
535 typedef struct
536 {                                                               /* EXIT or CONTINUE statement                   */
537         int                     cmd_type;
538         int                     lineno;
539         bool            is_exit;                /* Is this an exit or a continue? */
540         char       *label;                      /* NULL if it's an unlabelled EXIT/CONTINUE */
541         PLpgSQL_expr *cond;
542 } PLpgSQL_stmt_exit;
543
544
545 typedef struct
546 {                                                               /* RETURN statement                     */
547         int                     cmd_type;
548         int                     lineno;
549         PLpgSQL_expr *expr;
550         int                     retvarno;
551 } PLpgSQL_stmt_return;
552
553 typedef struct
554 {                                                               /* RETURN NEXT statement */
555         int                     cmd_type;
556         int                     lineno;
557         PLpgSQL_expr *expr;
558         int                     retvarno;
559 } PLpgSQL_stmt_return_next;
560
561 typedef struct
562 {                                                               /* RETURN QUERY statement */
563         int                     cmd_type;
564         int                     lineno;
565         PLpgSQL_expr *query;            /* if static query */
566         PLpgSQL_expr *dynquery;         /* if dynamic query (RETURN QUERY EXECUTE) */
567         List       *params;                     /* USING arguments for dynamic query */
568 } PLpgSQL_stmt_return_query;
569
570 typedef struct
571 {                                                               /* RAISE statement                      */
572         int                     cmd_type;
573         int                     lineno;
574         int                     elog_level;
575         char       *condname;           /* condition name, SQLSTATE, or NULL */
576         char       *message;            /* old-style message format literal, or NULL */
577         List       *params;                     /* list of expressions for old-style message */
578         List       *options;            /* list of PLpgSQL_raise_option */
579 } PLpgSQL_stmt_raise;
580
581 typedef struct
582 {                                                               /* RAISE statement option */
583         int                     opt_type;
584         PLpgSQL_expr *expr;
585 } PLpgSQL_raise_option;
586
587
588 typedef struct
589 {                                                               /* Generic SQL statement to execute */
590         int                     cmd_type;
591         int                     lineno;
592         PLpgSQL_expr *sqlstmt;
593         bool            mod_stmt;               /* is the stmt INSERT/UPDATE/DELETE? */
594         /* note: mod_stmt is set when we plan the query */
595         bool            into;                   /* INTO supplied? */
596         bool            strict;                 /* INTO STRICT flag */
597         PLpgSQL_rec *rec;                       /* INTO target, if record */
598         PLpgSQL_row *row;                       /* INTO target, if row */
599 } PLpgSQL_stmt_execsql;
600
601
602 typedef struct
603 {                                                               /* Dynamic SQL string to execute */
604         int                     cmd_type;
605         int                     lineno;
606         PLpgSQL_expr *query;            /* string expression */
607         bool            into;                   /* INTO supplied? */
608         bool            strict;                 /* INTO STRICT flag */
609         PLpgSQL_rec *rec;                       /* INTO target, if record */
610         PLpgSQL_row *row;                       /* INTO target, if row */
611         List       *params;                     /* USING expressions */
612 } PLpgSQL_stmt_dynexecute;
613
614
615 typedef struct PLpgSQL_func_hashkey
616 {                                                               /* Hash lookup key for functions */
617         Oid                     funcOid;
618
619         bool            isTrigger;              /* true if called as a trigger */
620
621         /* be careful that pad bytes in this struct get zeroed! */
622
623         /*
624          * For a trigger function, the OID of the relation triggered on is part of
625          * the hashkey --- we want to compile the trigger separately for each
626          * relation it is used with, in case the rowtype is different.  Zero if
627          * not called as a trigger.
628          */
629         Oid                     trigrelOid;
630
631         /*
632          * We include actual argument types in the hash key to support polymorphic
633          * PLpgSQL functions.  Be careful that extra positions are zeroed!
634          */
635         Oid                     argtypes[FUNC_MAX_ARGS];
636 } PLpgSQL_func_hashkey;
637
638
639 typedef struct PLpgSQL_function
640 {                                                               /* Complete compiled function     */
641         char       *fn_name;
642         Oid                     fn_oid;
643         TransactionId fn_xmin;
644         ItemPointerData fn_tid;
645         bool            fn_is_trigger;
646         PLpgSQL_func_hashkey *fn_hashkey;       /* back-link to hashtable key */
647         MemoryContext fn_cxt;
648
649         Oid                     fn_rettype;
650         int                     fn_rettyplen;
651         bool            fn_retbyval;
652         FmgrInfo        fn_retinput;
653         Oid                     fn_rettypioparam;
654         bool            fn_retistuple;
655         bool            fn_retset;
656         bool            fn_readonly;
657
658         int                     fn_nargs;
659         int                     fn_argvarnos[FUNC_MAX_ARGS];
660         int                     out_param_varno;
661         int                     found_varno;
662         int                     new_varno;
663         int                     old_varno;
664         int                     tg_name_varno;
665         int                     tg_when_varno;
666         int                     tg_level_varno;
667         int                     tg_op_varno;
668         int                     tg_relid_varno;
669         int                     tg_relname_varno;
670         int                     tg_table_name_varno;
671         int                     tg_table_schema_varno;
672         int                     tg_nargs_varno;
673         int                     tg_argv_varno;
674
675         PLpgSQL_resolve_option resolve_option;
676
677         int                     ndatums;
678         PLpgSQL_datum **datums;
679         PLpgSQL_stmt_block *action;
680
681         /* these fields change when the function is used */
682         struct PLpgSQL_execstate *cur_estate;
683         unsigned long use_count;
684 } PLpgSQL_function;
685
686
687 typedef struct PLpgSQL_execstate
688 {                                                               /* Runtime execution data       */
689         PLpgSQL_function *func;         /* function being executed */
690
691         Datum           retval;
692         bool            retisnull;
693         Oid                     rettype;                /* type of current retval */
694
695         Oid                     fn_rettype;             /* info about declared function rettype */
696         bool            retistuple;
697         bool            retisset;
698
699         bool            readonly_func;
700
701         TupleDesc       rettupdesc;
702         char       *exitlabel;          /* the "target" label of the current EXIT or
703                                                                  * CONTINUE stmt, if any */
704
705         Tuplestorestate *tuple_store;           /* SRFs accumulate results here */
706         MemoryContext tuple_store_cxt;
707         ResourceOwner tuple_store_owner;
708         ReturnSetInfo *rsi;
709
710         int                     found_varno;
711         int                     ndatums;
712         PLpgSQL_datum **datums;
713
714         /* temporary state for results from evaluation of query or expr */
715         SPITupleTable *eval_tuptable;
716         uint32          eval_processed;
717         Oid                     eval_lastoid;
718         ExprContext *eval_econtext; /* for executing simple expressions */
719         PLpgSQL_expr *cur_expr;         /* current query/expr being evaluated */
720
721         /* status information for error context reporting */
722         PLpgSQL_stmt *err_stmt;         /* current stmt */
723         const char *err_text;           /* additional state info */
724
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 /* Struct types used during parsing */
777
778 typedef struct
779 {
780         char       *ident;                      /* palloc'd converted identifier */
781         bool            quoted;                 /* Was it double-quoted? */
782 } PLword;
783
784 typedef struct
785 {
786         List       *idents;                     /* composite identifiers (list of String) */
787 } PLcword;
788
789 typedef struct
790 {
791         PLpgSQL_datum *datum;           /* referenced variable */
792         char       *ident;                      /* valid if simple name */
793         bool            quoted;
794         List       *idents;                     /* valid if composite name */
795 } PLwdatum;
796
797 /**********************************************************************
798  * Global variable declarations
799  **********************************************************************/
800
801 typedef enum
802 {
803         IDENTIFIER_LOOKUP_NORMAL,       /* normal processing of var names */
804         IDENTIFIER_LOOKUP_DECLARE,      /* In DECLARE --- don't look up names */
805         IDENTIFIER_LOOKUP_EXPR          /* In SQL expression --- special case */
806 } IdentifierLookup;
807
808 extern IdentifierLookup plpgsql_IdentifierLookup;
809
810 extern int      plpgsql_variable_conflict;
811
812 extern bool plpgsql_check_syntax;
813 extern bool plpgsql_DumpExecTree;
814
815 extern PLpgSQL_stmt_block *plpgsql_parse_result;
816
817 extern int      plpgsql_nDatums;
818 extern PLpgSQL_datum **plpgsql_Datums;
819
820 extern char *plpgsql_error_funcname;
821
822 extern PLpgSQL_function *plpgsql_curr_compile;
823 extern MemoryContext compile_tmp_cxt;
824
825 extern PLpgSQL_plugin **plugin_ptr;
826
827 /**********************************************************************
828  * Function declarations
829  **********************************************************************/
830
831 /* ----------
832  * Functions in pl_comp.c
833  * ----------
834  */
835 extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
836                                 bool forValidator);
837 extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
838 extern void plpgsql_parser_setup(struct ParseState *pstate,
839                                          PLpgSQL_expr *expr);
840 extern bool plpgsql_parse_word(char *word1, const char *yytxt,
841                                    PLwdatum *wdatum, PLword *word);
842 extern bool plpgsql_parse_dblword(char *word1, char *word2,
843                                           PLwdatum *wdatum, PLcword *cword);
844 extern bool plpgsql_parse_tripword(char *word1, char *word2, char *word3,
845                                            PLwdatum *wdatum, PLcword *cword);
846 extern PLpgSQL_type *plpgsql_parse_wordtype(char *ident);
847 extern PLpgSQL_type *plpgsql_parse_cwordtype(List *idents);
848 extern PLpgSQL_type *plpgsql_parse_wordrowtype(char *ident);
849 extern PLpgSQL_type *plpgsql_parse_cwordrowtype(List *idents);
850 extern PLpgSQL_type *plpgsql_build_datatype(Oid typeOid, int32 typmod);
851 extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
852                                            PLpgSQL_type *dtype,
853                                            bool add2namespace);
854 extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
855                                          bool add2namespace);
856 extern int plpgsql_recognize_err_condition(const char *condname,
857                                                                 bool allow_sqlstate);
858 extern PLpgSQL_condition *plpgsql_parse_err_condition(char *condname);
859 extern void plpgsql_adddatum(PLpgSQL_datum *new);
860 extern int      plpgsql_add_initdatums(int **varnos);
861 extern void plpgsql_HashTableInit(void);
862
863 /* ----------
864  * Functions in pl_handler.c
865  * ----------
866  */
867 extern void _PG_init(void);
868 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
869 extern Datum plpgsql_inline_handler(PG_FUNCTION_ARGS);
870 extern Datum plpgsql_validator(PG_FUNCTION_ARGS);
871
872 /* ----------
873  * Functions in pl_exec.c
874  * ----------
875  */
876 extern Datum plpgsql_exec_function(PLpgSQL_function *func,
877                                           FunctionCallInfo fcinfo);
878 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func,
879                                          TriggerData *trigdata);
880 extern void plpgsql_xact_cb(XactEvent event, void *arg);
881 extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
882                                    SubTransactionId parentSubid, void *arg);
883 extern Oid exec_get_datum_type(PLpgSQL_execstate *estate,
884                                         PLpgSQL_datum *datum);
885 extern Oid exec_get_rec_fieldtype(PLpgSQL_rec *rec, const char *fieldname,
886                                            int *fieldno);
887
888 /* ----------
889  * Functions for namespace handling in pl_funcs.c
890  * ----------
891  */
892 extern void plpgsql_ns_init(void);
893 extern void plpgsql_ns_push(const char *label);
894 extern void plpgsql_ns_pop(void);
895 extern PLpgSQL_nsitem *plpgsql_ns_top(void);
896 extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
897 extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
898                                   const char *name1, const char *name2,
899                                   const char *name3, int *names_used);
900 extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
901                                                 const char *name);
902
903 /* ----------
904  * Other functions in pl_funcs.c
905  * ----------
906  */
907 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
908 extern void plpgsql_dumptree(PLpgSQL_function *func);
909
910 /* ----------
911  * Scanner functions in pl_scanner.c
912  * ----------
913  */
914 extern int      plpgsql_base_yylex(void);
915 extern int      plpgsql_yylex(void);
916 extern void plpgsql_push_back_token(int token);
917 extern void plpgsql_append_source_text(StringInfo buf,
918                                                    int startlocation, int endlocation);
919 extern int      plpgsql_scanner_errposition(int location);
920 extern void plpgsql_yyerror(const char *message);
921 extern int      plpgsql_location_to_lineno(int location);
922 extern int      plpgsql_latest_lineno(void);
923 extern void plpgsql_scanner_init(const char *str);
924 extern void plpgsql_scanner_finish(void);
925
926 /* ----------
927  * Externs in gram.y
928  * ----------
929  */
930 extern int      plpgsql_yyparse(void);
931
932 #endif   /* PLPGSQL_H */