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