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