OSDN Git Service

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