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