1 /*-------------------------------------------------------------------------
3 * plpgsql.h - Definitions for the PL/pgSQL
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
13 *-------------------------------------------------------------------------
21 #include "access/xact.h"
23 #include "miscadmin.h"
24 #include "commands/trigger.h"
25 #include "executor/spi.h"
26 #include "utils/tuplestore.h"
28 /**********************************************************************
30 **********************************************************************/
32 /* define our text domain for translations */
34 #define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
37 #define _(x) dgettext(TEXTDOMAIN, x)
40 * Compiler's namestack item types
52 * Datum array node types
60 PLPGSQL_DTYPE_RECFIELD
,
61 PLPGSQL_DTYPE_ARRAYELEM
,
67 * Variants distinguished in PLpgSQL_type structs
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 */
79 * Execution tree node types
82 enum PLpgSQL_stmt_types
95 PLPGSQL_STMT_RETURN_NEXT
,
96 PLPGSQL_STMT_RETURN_QUERY
,
99 PLPGSQL_STMT_DYNEXECUTE
,
100 PLPGSQL_STMT_DYNFORS
,
101 PLPGSQL_STMT_GETDIAG
,
110 * Execution node return codes
123 * GET DIAGNOSTICS system attrs
128 PLPGSQL_GETDIAG_ROW_COUNT
,
129 PLPGSQL_GETDIAG_RESULT_OID
133 * RAISE statement options
138 PLPGSQL_RAISEOPTION_ERRCODE
,
139 PLPGSQL_RAISEOPTION_MESSAGE
,
140 PLPGSQL_RAISEOPTION_DETAIL
,
141 PLPGSQL_RAISEOPTION_HINT
145 /**********************************************************************
146 * Node and structure definitions
147 **********************************************************************/
151 { /* Dynamic string control structure */
153 int used
; /* Including NUL terminator */
159 { /* Postgres data type */
160 char *typname
; /* (simple) name of the type */
161 Oid typoid
; /* OID of the data type */
162 int ttype
; /* PLPGSQL_TTYPE_ code */
163 int16 typlen
; /* stuff copied from its pg_type entry */
167 FmgrInfo typinput
; /* lookup info for typinput function */
168 int32 atttypmod
; /* typmod (taken from someplace else) */
173 * PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
174 * PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, PLpgSQL_arrayelem, and
178 { /* Generic datum array item */
184 * The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
188 { /* Scalar or composite variable */
195 typedef struct PLpgSQL_expr
196 { /* SQL Query to plan and execute */
202 /* fields for "simple expression" fast-path execution: */
203 Expr
*expr_simple_expr
; /* NULL means not a simple expr */
204 int expr_simple_generation
; /* plancache generation we checked */
205 Oid expr_simple_type
; /* result type Oid, if simple */
208 * if expr is simple AND prepared in current eval_estate,
209 * expr_simple_state is valid. Test validity by seeing if expr_simple_id
210 * matches eval_estate_simple_id.
212 ExprState
*expr_simple_state
;
213 long int expr_simple_id
;
215 /* params to pass to expr */
217 int params
[1]; /* VARIABLE SIZE ARRAY ... must be last */
222 { /* Scalar variable */
228 PLpgSQL_type
*datatype
;
231 PLpgSQL_expr
*default_val
;
232 PLpgSQL_expr
*cursor_explicit_expr
;
233 int cursor_explicit_argrow
;
249 TupleDesc rowtupdesc
;
252 * Note: TupleDesc is only set up for named rowtypes, else it is NULL.
254 * Note: if the underlying rowtype contains a dropped column, the
255 * corresponding fieldnames[] entry will be NULL, and there is no
256 * corresponding var (varnos[] will be -1).
265 { /* Record variable (non-fixed structure) */
279 { /* Field in record */
283 int recparentno
; /* dno of parent record */
288 { /* Element of array variable */
291 PLpgSQL_expr
*subscript
;
292 int arrayparentno
; /* dno of parent array variable */
297 { /* Positional argument to trigger */
300 PLpgSQL_expr
*argnum
;
305 { /* Item in the compilers namestack */
312 /* XXX: consider adapting this to use List */
313 typedef struct PLpgSQL_ns
314 { /* Compiler namestack level */
317 PLpgSQL_nsitem
**items
;
318 struct PLpgSQL_ns
*upper
;
323 { /* Generic execution node */
329 typedef struct PLpgSQL_condition
330 { /* One EXCEPTION condition name */
331 int sqlerrstate
; /* SQLSTATE code */
332 char *condname
; /* condition name (for debugging) */
333 struct PLpgSQL_condition
*next
;
340 List
*exc_list
; /* List of WHEN clauses */
341 } PLpgSQL_exception_block
;
344 { /* One EXCEPTION ... WHEN clause */
346 PLpgSQL_condition
*conditions
;
347 List
*action
; /* List of statements */
352 { /* Block of statements */
356 List
*body
; /* List of statements */
359 PLpgSQL_exception_block
*exceptions
;
360 } PLpgSQL_stmt_block
;
364 { /* Assign statement */
369 } PLpgSQL_stmt_assign
;
372 { /* PERFORM statement */
376 } PLpgSQL_stmt_perform
;
379 { /* Get Diagnostics item */
380 int kind
; /* id for diagnostic value desired */
381 int target
; /* where to assign it */
385 { /* Get Diagnostics statement */
388 List
*diag_items
; /* List of PLpgSQL_diag_item */
389 } PLpgSQL_stmt_getdiag
;
397 List
*true_body
; /* List of statements */
398 List
*false_body
; /* List of statements */
402 typedef struct /* CASE statement */
406 PLpgSQL_expr
*t_expr
; /* test expression, or NULL if none */
407 int t_varno
; /* var to store test expression value into */
408 List
*case_when_list
; /* List of PLpgSQL_case_when structs */
409 bool have_else
; /* flag needed because list could be empty */
410 List
*else_stmts
; /* List of statements */
413 typedef struct /* one arm of CASE statement */
416 PLpgSQL_expr
*expr
; /* boolean expression for this case */
417 List
*stmts
; /* List of statements */
422 { /* Unconditional LOOP statement */
426 List
*body
; /* List of statements */
431 { /* WHILE cond LOOP statement */
436 List
*body
; /* List of statements */
437 } PLpgSQL_stmt_while
;
441 { /* FOR statement with integer loopvar */
448 PLpgSQL_expr
*step
; /* NULL means default (ie, BY 1) */
450 List
*body
; /* List of statements */
455 * PLpgSQL_stmt_forq represents a FOR statement running over a SQL query.
456 * It is the common supertype of PLpgSQL_stmt_fors, PLpgSQL_stmt_forc
457 * and PLpgSQL_dynfors.
466 List
*body
; /* List of statements */
470 { /* FOR statement running over SELECT */
476 List
*body
; /* List of statements */
477 /* end of fields that must match PLpgSQL_stmt_forq */
482 { /* FOR statement running over cursor */
488 List
*body
; /* List of statements */
489 /* end of fields that must match PLpgSQL_stmt_forq */
491 PLpgSQL_expr
*argquery
; /* cursor arguments if any */
495 { /* FOR statement running over EXECUTE */
501 List
*body
; /* List of statements */
502 /* end of fields that must match PLpgSQL_stmt_forq */
504 List
*params
; /* USING expressions */
505 } PLpgSQL_stmt_dynfors
;
509 { /* OPEN a curvar */
514 PLpgSQL_row
*returntype
;
515 PLpgSQL_expr
*argquery
;
517 PLpgSQL_expr
*dynquery
;
522 { /* FETCH or MOVE statement */
525 PLpgSQL_rec
*rec
; /* target, as record or row */
527 int curvar
; /* cursor variable to fetch from */
528 FetchDirection direction
; /* fetch direction */
529 int how_many
; /* count, if constant (expr is NULL) */
530 PLpgSQL_expr
*expr
; /* count, if expression */
531 bool is_move
; /* is this a fetch or move? */
532 } PLpgSQL_stmt_fetch
;
540 } PLpgSQL_stmt_close
;
544 { /* EXIT or CONTINUE statement */
547 bool is_exit
; /* Is this an exit or a continue? */
548 char *label
; /* NULL if it's an unlabelled EXIT/CONTINUE */
554 { /* RETURN statement */
559 } PLpgSQL_stmt_return
;
562 { /* RETURN NEXT statement */
567 } PLpgSQL_stmt_return_next
;
570 { /* RETURN QUERY statement */
573 PLpgSQL_expr
*query
; /* if static query */
574 PLpgSQL_expr
*dynquery
; /* if dynamic query (RETURN QUERY EXECUTE) */
575 List
*params
; /* USING arguments for dynamic query */
576 } PLpgSQL_stmt_return_query
;
579 { /* RAISE statement */
583 char *condname
; /* condition name, SQLSTATE, or NULL */
584 char *message
; /* old-style message format literal, or NULL */
585 List
*params
; /* list of expressions for old-style message */
586 List
*options
; /* list of PLpgSQL_raise_option */
587 } PLpgSQL_stmt_raise
;
590 { /* RAISE statement option */
593 } PLpgSQL_raise_option
;
597 { /* Generic SQL statement to execute */
600 PLpgSQL_expr
*sqlstmt
;
601 bool mod_stmt
; /* is the stmt INSERT/UPDATE/DELETE? */
602 /* note: mod_stmt is set when we plan the query */
603 bool into
; /* INTO supplied? */
604 bool strict
; /* INTO STRICT flag */
605 PLpgSQL_rec
*rec
; /* INTO target, if record */
606 PLpgSQL_row
*row
; /* INTO target, if row */
607 } PLpgSQL_stmt_execsql
;
611 { /* Dynamic SQL string to execute */
614 PLpgSQL_expr
*query
; /* string expression */
615 bool into
; /* INTO supplied? */
616 bool strict
; /* INTO STRICT flag */
617 PLpgSQL_rec
*rec
; /* INTO target, if record */
618 PLpgSQL_row
*row
; /* INTO target, if row */
619 List
*params
; /* USING expressions */
620 } PLpgSQL_stmt_dynexecute
;
623 typedef struct PLpgSQL_func_hashkey
624 { /* Hash lookup key for functions */
627 bool isTrigger
; /* true if called as a trigger */
629 /* be careful that pad bytes in this struct get zeroed! */
632 * For a trigger function, the OID of the relation triggered on is part of
633 * the hashkey --- we want to compile the trigger separately for each
634 * relation it is used with, in case the rowtype is different. Zero if
635 * not called as a trigger.
640 * We include actual argument types in the hash key to support polymorphic
641 * PLpgSQL functions. Be careful that extra positions are zeroed!
643 Oid argtypes
[FUNC_MAX_ARGS
];
644 } PLpgSQL_func_hashkey
;
647 typedef struct PLpgSQL_function
648 { /* Complete compiled function */
651 TransactionId fn_xmin
;
652 ItemPointerData fn_tid
;
654 PLpgSQL_func_hashkey
*fn_hashkey
; /* back-link to hashtable key */
655 MemoryContext fn_cxt
;
660 FmgrInfo fn_retinput
;
661 Oid fn_rettypioparam
;
667 int fn_argvarnos
[FUNC_MAX_ARGS
];
677 int tg_relname_varno
;
678 int tg_table_name_varno
;
679 int tg_table_schema_varno
;
683 PLpgSQL_datum
**datums
;
684 PLpgSQL_stmt_block
*action
;
686 unsigned long use_count
;
691 { /* Runtime execution data */
694 Oid rettype
; /* type of current retval */
696 Oid fn_rettype
; /* info about declared function rettype */
702 TupleDesc rettupdesc
;
703 char *exitlabel
; /* the "target" label of the current EXIT or
704 * CONTINUE stmt, if any */
706 Tuplestorestate
*tuple_store
; /* SRFs accumulate results here */
707 MemoryContext tuple_store_cxt
;
715 PLpgSQL_datum
**datums
;
717 /* temporary state for results from evaluation of query or expr */
718 SPITupleTable
*eval_tuptable
;
719 uint32 eval_processed
;
721 ExprContext
*eval_econtext
; /* for executing simple expressions */
722 EState
*eval_estate
; /* EState containing eval_econtext */
723 long int eval_estate_simple_id
; /* ID for eval_estate */
725 /* status information for error context reporting */
726 PLpgSQL_function
*err_func
; /* current func */
727 PLpgSQL_stmt
*err_stmt
; /* current stmt */
728 const char *err_text
; /* additional state info */
729 void *plugin_info
; /* reserved for use by optional plugin */
734 * A PLpgSQL_plugin structure represents an instrumentation plugin.
735 * To instrument PL/pgSQL, a plugin library must access the rendezvous
736 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
737 * Typically the struct could just be static data in the plugin library.
738 * We expect that a plugin would do this at library load time (_PG_init()).
739 * It must also be careful to set the rendezvous variable back to NULL
740 * if it is unloaded (_PG_fini()).
742 * This structure is basically a collection of function pointers --- at
743 * various interesting points in pl_exec.c, we call these functions
744 * (if the pointers are non-NULL) to give the plugin a chance to watch
747 * func_setup is called when we start a function, before we've initialized
748 * the local variables defined by the function.
750 * func_beg is called when we start a function, after we've initialized
751 * the local variables.
753 * func_end is called at the end of a function.
755 * stmt_beg and stmt_end are called before and after (respectively) each
758 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
759 * error_callback and assign_expr fields with pointers to its own
760 * plpgsql_exec_error_callback and exec_assign_expr functions. This is
761 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
766 /* Function pointers set up by the plugin */
767 void (*func_setup
) (PLpgSQL_execstate
*estate
, PLpgSQL_function
*func
);
768 void (*func_beg
) (PLpgSQL_execstate
*estate
, PLpgSQL_function
*func
);
769 void (*func_end
) (PLpgSQL_execstate
*estate
, PLpgSQL_function
*func
);
770 void (*stmt_beg
) (PLpgSQL_execstate
*estate
, PLpgSQL_stmt
*stmt
);
771 void (*stmt_end
) (PLpgSQL_execstate
*estate
, PLpgSQL_stmt
*stmt
);
773 /* Function pointers set by PL/pgSQL itself */
774 void (*error_callback
) (void *arg
);
775 void (*assign_expr
) (PLpgSQL_execstate
*estate
, PLpgSQL_datum
*target
,
780 /**********************************************************************
781 * Global variable declarations
782 **********************************************************************/
784 extern bool plpgsql_DumpExecTree
;
785 extern bool plpgsql_SpaceScanned
;
786 extern int plpgsql_nDatums
;
787 extern PLpgSQL_datum
**plpgsql_Datums
;
789 extern int plpgsql_error_lineno
;
790 extern char *plpgsql_error_funcname
;
792 /* linkage to the real yytext variable */
793 extern char *plpgsql_base_yytext
;
795 #define yytext plpgsql_base_yytext
797 extern PLpgSQL_function
*plpgsql_curr_compile
;
798 extern bool plpgsql_check_syntax
;
799 extern MemoryContext compile_tmp_cxt
;
801 extern PLpgSQL_plugin
**plugin_ptr
;
803 /**********************************************************************
804 * Function declarations
805 **********************************************************************/
808 * Functions in pl_comp.c
811 extern PLpgSQL_function
*plpgsql_compile(FunctionCallInfo fcinfo
,
813 extern int plpgsql_parse_word(const char *word
);
814 extern int plpgsql_parse_dblword(const char *word
);
815 extern int plpgsql_parse_tripword(const char *word
);
816 extern int plpgsql_parse_wordtype(char *word
);
817 extern int plpgsql_parse_dblwordtype(char *word
);
818 extern int plpgsql_parse_tripwordtype(char *word
);
819 extern int plpgsql_parse_wordrowtype(char *word
);
820 extern int plpgsql_parse_dblwordrowtype(char *word
);
821 extern PLpgSQL_type
*plpgsql_parse_datatype(const char *string
);
822 extern PLpgSQL_type
*plpgsql_build_datatype(Oid typeOid
, int32 typmod
);
823 extern PLpgSQL_variable
*plpgsql_build_variable(const char *refname
, int lineno
,
826 extern PLpgSQL_rec
*plpgsql_build_record(const char *refname
, int lineno
,
828 extern int plpgsql_recognize_err_condition(const char *condname
,
829 bool allow_sqlstate
);
830 extern PLpgSQL_condition
*plpgsql_parse_err_condition(char *condname
);
831 extern void plpgsql_adddatum(PLpgSQL_datum
*new);
832 extern int plpgsql_add_initdatums(int **varnos
);
833 extern void plpgsql_HashTableInit(void);
834 extern void plpgsql_compile_error_callback(void *arg
);
837 * Functions in pl_handler.c
840 extern void _PG_init(void);
841 extern Datum
plpgsql_call_handler(PG_FUNCTION_ARGS
);
842 extern Datum
plpgsql_validator(PG_FUNCTION_ARGS
);
845 * Functions in pl_exec.c
848 extern Datum
plpgsql_exec_function(PLpgSQL_function
*func
,
849 FunctionCallInfo fcinfo
);
850 extern HeapTuple
plpgsql_exec_trigger(PLpgSQL_function
*func
,
851 TriggerData
*trigdata
);
852 extern void plpgsql_xact_cb(XactEvent event
, void *arg
);
853 extern void plpgsql_subxact_cb(SubXactEvent event
, SubTransactionId mySubid
,
854 SubTransactionId parentSubid
, void *arg
);
857 * Functions for the dynamic string handling in pl_funcs.c
860 extern void plpgsql_dstring_init(PLpgSQL_dstring
*ds
);
861 extern void plpgsql_dstring_free(PLpgSQL_dstring
*ds
);
862 extern void plpgsql_dstring_append(PLpgSQL_dstring
*ds
, const char *str
);
863 extern void plpgsql_dstring_append_char(PLpgSQL_dstring
*ds
, char c
);
864 extern char *plpgsql_dstring_get(PLpgSQL_dstring
*ds
);
867 * Functions for namestack handling in pl_funcs.c
870 extern void plpgsql_ns_init(void);
871 extern bool plpgsql_ns_setlocal(bool flag
);
872 extern void plpgsql_ns_push(const char *label
);
873 extern void plpgsql_ns_pop(void);
874 extern void plpgsql_ns_additem(int itemtype
, int itemno
, const char *name
);
875 extern PLpgSQL_nsitem
*plpgsql_ns_lookup(const char *name1
, const char *name2
,
876 const char *name3
, int *names_used
);
877 extern PLpgSQL_nsitem
*plpgsql_ns_lookup_label(const char *name
);
878 extern void plpgsql_ns_rename(char *oldname
, char *newname
);
881 * Other functions in pl_funcs.c
884 extern void plpgsql_convert_ident(const char *s
, char **output
, int numidents
);
885 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt
*stmt
);
886 extern void plpgsql_dumptree(PLpgSQL_function
*func
);
889 * Externs in gram.y and scan.l
892 extern PLpgSQL_expr
*plpgsql_read_expression(int until
, const char *expected
);
893 extern int plpgsql_yyparse(void);
894 extern int plpgsql_base_yylex(void);
895 extern int plpgsql_yylex(void);
896 extern void plpgsql_push_back_token(int token
);
897 extern void plpgsql_yyerror(const char *message
);
898 extern int plpgsql_scanner_lineno(void);
899 extern void plpgsql_scanner_init(const char *str
, int functype
);
900 extern void plpgsql_scanner_finish(void);
901 extern char *plpgsql_get_string_value(void);
903 #endif /* PLPGSQL_H */