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