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