1
/*-------------------------------------------------------------------------
3
* plpgsql.h - Definitions for the PL/pgSQL
6
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
11
* src/pl/plpgsql/src/plpgsql.h
13
*-------------------------------------------------------------------------
21
#include "access/xact.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"
29
/**********************************************************************
31
**********************************************************************/
33
/* define our text domain for translations */
35
#define TEXTDOMAIN PG_TEXTDOMAIN("plpgsql")
38
#define _(x) dgettext(TEXTDOMAIN, x)
41
* Compiler's namespace item types
53
* Datum array node types
61
PLPGSQL_DTYPE_RECFIELD,
62
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
93
PLPGSQL_STMT_FOREACH_A,
96
PLPGSQL_STMT_RETURN_NEXT,
97
PLPGSQL_STMT_RETURN_QUERY,
100
PLPGSQL_STMT_DYNEXECUTE,
101
PLPGSQL_STMT_DYNFORS,
102
PLPGSQL_STMT_GETDIAG,
111
* 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
* Behavioral modes for plpgsql variable resolution
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;
156
/**********************************************************************
157
* Node and structure definitions
158
**********************************************************************/
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 */
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) */
177
* PLpgSQL_datum is the common supertype for PLpgSQL_expr, PLpgSQL_var,
178
* PLpgSQL_row, PLpgSQL_rec, PLpgSQL_recfield, and PLpgSQL_arrayelem
181
{ /* Generic datum array item */
187
* The variants PLpgSQL_var, PLpgSQL_row, and PLpgSQL_rec share these
191
{ /* Scalar or composite variable */
198
typedef struct PLpgSQL_expr
199
{ /* SQL Query to plan and execute */
204
Bitmapset *paramnos; /* all dnos referenced by this query */
206
/* function containing this expr (not set until we first parse query) */
207
struct PLpgSQL_function *func;
209
/* namespace chain visible to this expr */
210
struct PLpgSQL_nsitem *ns;
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 */
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!)
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;
230
{ /* Scalar variable */
236
PLpgSQL_type *datatype;
239
PLpgSQL_expr *default_val;
240
PLpgSQL_expr *cursor_explicit_expr;
241
int cursor_explicit_argrow;
257
TupleDesc rowtupdesc;
260
* Note: TupleDesc is only set up for named rowtypes, else it is NULL.
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).
273
{ /* Record variable (non-fixed structure) */
287
{ /* Field in record */
291
int recparentno; /* dno of parent record */
296
{ /* Element of array variable */
299
PLpgSQL_expr *subscript;
300
int arrayparentno; /* dno of parent array variable */
304
typedef struct PLpgSQL_nsitem
305
{ /* Item in the compilers namespace tree */
308
struct PLpgSQL_nsitem *prev;
309
char name[1]; /* actually, as long as needed */
314
{ /* Generic execution node */
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;
331
List *exc_list; /* List of WHEN clauses */
332
} PLpgSQL_exception_block;
335
{ /* One EXCEPTION ... WHEN clause */
337
PLpgSQL_condition *conditions;
338
List *action; /* List of statements */
343
{ /* Block of statements */
347
List *body; /* List of statements */
350
PLpgSQL_exception_block *exceptions;
351
} PLpgSQL_stmt_block;
355
{ /* Assign statement */
360
} PLpgSQL_stmt_assign;
363
{ /* PERFORM statement */
367
} PLpgSQL_stmt_perform;
370
{ /* Get Diagnostics item */
371
int kind; /* id for diagnostic value desired */
372
int target; /* where to assign it */
376
{ /* Get Diagnostics statement */
379
List *diag_items; /* List of PLpgSQL_diag_item */
380
} PLpgSQL_stmt_getdiag;
388
List *true_body; /* List of statements */
389
List *false_body; /* List of statements */
393
typedef struct /* CASE statement */
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 */
404
typedef struct /* one arm of CASE statement */
407
PLpgSQL_expr *expr; /* boolean expression for this case */
408
List *stmts; /* List of statements */
413
{ /* Unconditional LOOP statement */
417
List *body; /* List of statements */
422
{ /* WHILE cond LOOP statement */
427
List *body; /* List of statements */
428
} PLpgSQL_stmt_while;
432
{ /* FOR statement with integer loopvar */
439
PLpgSQL_expr *step; /* NULL means default (ie, BY 1) */
441
List *body; /* List of statements */
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.
457
List *body; /* List of statements */
461
{ /* FOR statement running over SELECT */
467
List *body; /* List of statements */
468
/* end of fields that must match PLpgSQL_stmt_forq */
473
{ /* FOR statement running over cursor */
479
List *body; /* List of statements */
480
/* end of fields that must match PLpgSQL_stmt_forq */
482
PLpgSQL_expr *argquery; /* cursor arguments if any */
486
{ /* FOR statement running over EXECUTE */
492
List *body; /* List of statements */
493
/* end of fields that must match PLpgSQL_stmt_forq */
495
List *params; /* USING expressions */
496
} PLpgSQL_stmt_dynfors;
500
{ /* FOREACH item in array loop */
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;
512
{ /* OPEN a curvar */
517
PLpgSQL_row *returntype;
518
PLpgSQL_expr *argquery;
520
PLpgSQL_expr *dynquery;
521
List *params; /* USING expressions */
526
{ /* FETCH or MOVE statement */
529
PLpgSQL_rec *rec; /* target, as record or 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;
545
} PLpgSQL_stmt_close;
549
{ /* EXIT or CONTINUE statement */
552
bool is_exit; /* Is this an exit or a continue? */
553
char *label; /* NULL if it's an unlabelled EXIT/CONTINUE */
559
{ /* RETURN statement */
564
} PLpgSQL_stmt_return;
567
{ /* RETURN NEXT statement */
572
} PLpgSQL_stmt_return_next;
575
{ /* RETURN QUERY statement */
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;
584
{ /* RAISE statement */
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;
595
{ /* RAISE statement option */
598
} PLpgSQL_raise_option;
602
{ /* Generic SQL statement to execute */
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;
616
{ /* Dynamic SQL string to execute */
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;
628
typedef struct PLpgSQL_func_hashkey
629
{ /* Hash lookup key for functions */
632
bool isTrigger; /* true if called as a trigger */
634
/* be careful that pad bytes in this struct get zeroed! */
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.
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.
652
* We include actual argument types in the hash key to support polymorphic
653
* PLpgSQL functions. Be careful that extra positions are zeroed!
655
Oid argtypes[FUNC_MAX_ARGS];
656
} PLpgSQL_func_hashkey;
659
typedef struct PLpgSQL_function
660
{ /* Complete compiled function */
663
TransactionId fn_xmin;
664
ItemPointerData fn_tid;
666
Oid fn_input_collation;
667
PLpgSQL_func_hashkey *fn_hashkey; /* back-link to hashtable key */
668
MemoryContext fn_cxt;
673
FmgrInfo fn_retinput;
674
Oid fn_rettypioparam;
680
int fn_argvarnos[FUNC_MAX_ARGS];
690
int tg_relname_varno;
691
int tg_table_name_varno;
692
int tg_table_schema_varno;
696
PLpgSQL_resolve_option resolve_option;
699
PLpgSQL_datum **datums;
700
PLpgSQL_stmt_block *action;
702
/* these fields change when the function is used */
703
struct PLpgSQL_execstate *cur_estate;
704
unsigned long use_count;
708
typedef struct PLpgSQL_execstate
709
{ /* Runtime execution data */
710
PLpgSQL_function *func; /* function being executed */
714
Oid rettype; /* type of current retval */
716
Oid fn_rettype; /* info about declared function rettype */
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 */
727
Tuplestorestate *tuple_store; /* SRFs accumulate results here */
728
MemoryContext tuple_store_cxt;
729
ResourceOwner tuple_store_owner;
734
PLpgSQL_datum **datums;
736
/* temporary state for results from evaluation of query or expr */
737
SPITupleTable *eval_tuptable;
738
uint32 eval_processed;
740
ExprContext *eval_econtext; /* for executing simple expressions */
741
PLpgSQL_expr *cur_expr; /* current query/expr being evaluated */
743
/* status information for error context reporting */
744
PLpgSQL_stmt *err_stmt; /* current stmt */
745
const char *err_text; /* additional state info */
747
void *plugin_info; /* reserved for use by optional plugin */
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()).
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
765
* func_setup is called when we start a function, before we've initialized
766
* the local variables defined by the function.
768
* func_beg is called when we start a function, after we've initialized
769
* the local variables.
771
* func_end is called at the end of a function.
773
* stmt_beg and stmt_end are called before and after (respectively) each
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.
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);
791
/* Function pointers set by PL/pgSQL itself */
792
void (*error_callback) (void *arg);
793
void (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
798
/* Struct types used during parsing */
802
char *ident; /* palloc'd converted identifier */
803
bool quoted; /* Was it double-quoted? */
808
List *idents; /* composite identifiers (list of String) */
813
PLpgSQL_datum *datum; /* referenced variable */
814
char *ident; /* valid if simple name */
816
List *idents; /* valid if composite name */
819
/**********************************************************************
820
* Global variable declarations
821
**********************************************************************/
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 */
830
extern IdentifierLookup plpgsql_IdentifierLookup;
832
extern int plpgsql_variable_conflict;
834
extern bool plpgsql_check_syntax;
835
extern bool plpgsql_DumpExecTree;
837
extern PLpgSQL_stmt_block *plpgsql_parse_result;
839
extern int plpgsql_nDatums;
840
extern PLpgSQL_datum **plpgsql_Datums;
842
extern char *plpgsql_error_funcname;
844
extern PLpgSQL_function *plpgsql_curr_compile;
845
extern MemoryContext compile_tmp_cxt;
847
extern PLpgSQL_plugin **plugin_ptr;
849
/**********************************************************************
850
* Function declarations
851
**********************************************************************/
854
* Functions in pl_comp.c
857
extern PLpgSQL_function *plpgsql_compile(FunctionCallInfo fcinfo,
859
extern PLpgSQL_function *plpgsql_compile_inline(char *proc_source);
860
extern void plpgsql_parser_setup(struct ParseState *pstate,
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,
874
extern PLpgSQL_variable *plpgsql_build_variable(const char *refname, int lineno,
877
extern PLpgSQL_rec *plpgsql_build_record(const char *refname, int lineno,
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);
887
* Functions in pl_handler.c
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);
896
* Functions in pl_exec.c
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 Oid exec_get_datum_collation(PLpgSQL_execstate *estate,
909
PLpgSQL_datum *datum);
912
* Functions for namespace handling in pl_funcs.c
915
extern void plpgsql_ns_init(void);
916
extern void plpgsql_ns_push(const char *label);
917
extern void plpgsql_ns_pop(void);
918
extern PLpgSQL_nsitem *plpgsql_ns_top(void);
919
extern void plpgsql_ns_additem(int itemtype, int itemno, const char *name);
920
extern PLpgSQL_nsitem *plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode,
921
const char *name1, const char *name2,
922
const char *name3, int *names_used);
923
extern PLpgSQL_nsitem *plpgsql_ns_lookup_label(PLpgSQL_nsitem *ns_cur,
927
* Other functions in pl_funcs.c
930
extern const char *plpgsql_stmt_typename(PLpgSQL_stmt *stmt);
931
extern void plpgsql_free_function_memory(PLpgSQL_function *func);
932
extern void plpgsql_dumptree(PLpgSQL_function *func);
935
* Scanner functions in pl_scanner.c
938
extern int plpgsql_base_yylex(void);
939
extern int plpgsql_yylex(void);
940
extern void plpgsql_push_back_token(int token);
941
extern void plpgsql_append_source_text(StringInfo buf,
942
int startlocation, int endlocation);
943
extern int plpgsql_scanner_errposition(int location);
944
extern void plpgsql_yyerror(const char *message);
945
extern int plpgsql_location_to_lineno(int location);
946
extern int plpgsql_latest_lineno(void);
947
extern void plpgsql_scanner_init(const char *str);
948
extern void plpgsql_scanner_finish(void);
954
extern int plpgsql_yyparse(void);
956
#endif /* PLPGSQL_H */