1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
|
/*--------------------------------------------------------------------
* Symbols referenced in this file:
* - plpgsql_compile_inline
* - plpgsql_error_funcname
* - plpgsql_check_syntax
* - plpgsql_curr_compile
* - plpgsql_compile_tmp_cxt
* - plpgsql_DumpExecTree
* - plpgsql_start_datums
* - plpgsql_nDatums
* - plpgsql_Datums
* - datums_alloc
* - datums_last
* - plpgsql_build_variable
* - plpgsql_adddatum
* - plpgsql_build_record
* - plpgsql_build_datatype
* - plpgsql_parse_tripword
* - plpgsql_build_recfield
* - plpgsql_parse_dblword
* - plpgsql_parse_word
* - plpgsql_add_initdatums
* - plpgsql_recognize_err_condition
* - exception_label_map
* - plpgsql_parse_err_condition
* - plpgsql_parse_wordtype
* - plpgsql_parse_wordrowtype
* - plpgsql_parse_cwordtype
* - plpgsql_parse_cwordrowtype
* - plpgsql_parse_result
* - plpgsql_finish_datums
* - plpgsql_compile_error_callback
* - add_dummy_return
*--------------------------------------------------------------------
*/
/*-------------------------------------------------------------------------
*
* pl_comp.c - Compiler part of the PL/pgSQL
* procedural language
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/pl/plpgsql/src/pl_comp.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "plpgsql.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/regproc.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
/* ----------
* Our own local and global variables
* ----------
*/
__thread PLpgSQL_stmt_block *plpgsql_parse_result;
static __thread int datums_alloc;
__thread int plpgsql_nDatums;
__thread PLpgSQL_datum **plpgsql_Datums;
static __thread int datums_last;
__thread char *plpgsql_error_funcname;
__thread bool plpgsql_DumpExecTree = false;
__thread bool plpgsql_check_syntax = false;
__thread PLpgSQL_function *plpgsql_curr_compile;
/* A context appropriate for short-term allocs during compilation */
__thread MemoryContext plpgsql_compile_tmp_cxt;
/* ----------
* Hash table for compiled functions
* ----------
*/
typedef struct plpgsql_hashent
{
PLpgSQL_func_hashkey key;
PLpgSQL_function *function;
} plpgsql_HashEnt;
#define FUNCS_PER_USER 128 /* initial table size */
/* ----------
* Lookup table for EXCEPTION condition names
* ----------
*/
typedef struct
{
const char *label;
int sqlerrstate;
} ExceptionLabelMap;
static const ExceptionLabelMap exception_label_map[] = {
#include "plerrcodes.h" /* pgrminclude ignore */
{NULL, 0}
};
/* ----------
* static prototypes
* ----------
*/
static PLpgSQL_function *do_compile(FunctionCallInfo fcinfo,
HeapTuple procTup,
PLpgSQL_function *function,
PLpgSQL_func_hashkey *hashkey,
bool forValidator);
static void plpgsql_compile_error_callback(void *arg);
static void add_parameter_name(PLpgSQL_nsitem_type itemtype, int itemno, const char *name);
static void add_dummy_return(PLpgSQL_function *function);
static Node *plpgsql_pre_column_ref(ParseState *pstate, ColumnRef *cref);
static Node *plpgsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var);
static Node *plpgsql_param_ref(ParseState *pstate, ParamRef *pref);
static Node *resolve_column_ref(ParseState *pstate, PLpgSQL_expr *expr,
ColumnRef *cref, bool error_if_no_field);
static Node *make_datum_param(PLpgSQL_expr *expr, int dno, int location);
static PLpgSQL_row *build_row_from_vars(PLpgSQL_variable **vars, int numvars);
static PLpgSQL_type *build_datatype(HeapTuple typeTup, int32 typmod,
Oid collation, TypeName *origtypname);
static void compute_function_hashkey(FunctionCallInfo fcinfo,
Form_pg_proc procStruct,
PLpgSQL_func_hashkey *hashkey,
bool forValidator);
static void plpgsql_resolve_polymorphic_argtypes(int numargs,
Oid *argtypes, char *argmodes,
Node *call_expr, bool forValidator,
const char *proname);
static PLpgSQL_function *plpgsql_HashTableLookup(PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableInsert(PLpgSQL_function *function,
PLpgSQL_func_hashkey *func_key);
static void plpgsql_HashTableDelete(PLpgSQL_function *function);
static void delete_function(PLpgSQL_function *func);
/* ----------
* plpgsql_compile Make an execution tree for a PL/pgSQL function.
*
* If forValidator is true, we're only compiling for validation purposes,
* and so some checks are skipped.
*
* Note: it's important for this to fall through quickly if the function
* has already been compiled.
* ----------
*/
/*
* This is the slow part of plpgsql_compile().
*
* The passed-in "function" pointer is either NULL or an already-allocated
* function struct to overwrite.
*
* While compiling a function, the CurrentMemoryContext is the
* per-function memory context of the function we are compiling. That
* means a palloc() will allocate storage with the same lifetime as
* the function itself.
*
* Because palloc()'d storage will not be immediately freed, temporary
* allocations should either be performed in a short-lived memory
* context or explicitly pfree'd. Since not all backend functions are
* careful about pfree'ing their allocations, it is also wise to
* switch into a short-term context before calling into the
* backend. An appropriate context for performing short-term
* allocations is the plpgsql_compile_tmp_cxt.
*
* NB: this code is not re-entrant. We assume that nothing we do here could
* result in the invocation of another plpgsql function.
*/
/* ----------
* plpgsql_compile_inline Make an execution tree for an anonymous code block.
*
* Note: this is generally parallel to do_compile(); is it worth trying to
* merge the two?
*
* Note: we assume the block will be thrown away so there is no need to build
* persistent data structures.
* ----------
*/
PLpgSQL_function *
plpgsql_compile_inline(char *proc_source)
{
char *func_name = "inline_code_block";
PLpgSQL_function *function;
ErrorContextCallback plerrcontext;
PLpgSQL_variable *var;
int parse_rc;
MemoryContext func_cxt;
/*
* Setup the scanner input and error info. We assume that this function
* cannot be invoked recursively, so there's no need to save and restore
* the static variables used here.
*/
plpgsql_scanner_init(proc_source);
plpgsql_error_funcname = func_name;
/*
* Setup error traceback support for ereport()
*/
plerrcontext.callback = plpgsql_compile_error_callback;
plerrcontext.arg = proc_source;
plerrcontext.previous = error_context_stack;
error_context_stack = &plerrcontext;
/* Do extra syntax checking if check_function_bodies is on */
plpgsql_check_syntax = check_function_bodies;
/* Function struct does not live past current statement */
function = (PLpgSQL_function *) palloc0(sizeof(PLpgSQL_function));
plpgsql_curr_compile = function;
/*
* All the rest of the compile-time storage (e.g. parse tree) is kept in
* its own memory context, so it can be reclaimed easily.
*/
func_cxt = AllocSetContextCreate(CurrentMemoryContext,
"PL/pgSQL inline code context",
ALLOCSET_DEFAULT_SIZES);
plpgsql_compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
function->fn_signature = pstrdup(func_name);
function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
function->fn_input_collation = InvalidOid;
function->fn_cxt = func_cxt;
function->out_param_varno = -1; /* set up for no OUT param */
function->resolve_option = plpgsql_variable_conflict;
function->print_strict_params = plpgsql_print_strict_params;
/*
* don't do extra validation for inline code as we don't want to add spam
* at runtime
*/
function->extra_warnings = 0;
function->extra_errors = 0;
function->nstatements = 0;
function->requires_procedure_resowner = false;
plpgsql_ns_init();
plpgsql_ns_push(func_name, PLPGSQL_LABEL_BLOCK);
plpgsql_DumpExecTree = false;
plpgsql_start_datums();
/* Set up as though in a function returning VOID */
function->fn_rettype = VOIDOID;
function->fn_retset = false;
function->fn_retistuple = false;
function->fn_retisdomain = false;
function->fn_prokind = PROKIND_FUNCTION;
/* a bit of hardwired knowledge about type VOID here */
function->fn_retbyval = true;
function->fn_rettyplen = sizeof(int32);
/*
* Remember if function is STABLE/IMMUTABLE. XXX would it be better to
* set this true inside a read-only transaction? Not clear.
*/
function->fn_readonly = false;
/*
* Create the magic FOUND variable.
*/
var = plpgsql_build_variable("found", 0,
plpgsql_build_datatype(BOOLOID,
-1,
InvalidOid,
NULL),
true);
function->found_varno = var->dno;
/*
* Now parse the function's text
*/
parse_rc = plpgsql_yyparse();
if (parse_rc != 0)
elog(ERROR, "plpgsql parser returned %d", parse_rc);
function->action = plpgsql_parse_result;
plpgsql_scanner_finish();
/*
* If it returns VOID (always true at the moment), we allow control to
* fall off the end without an explicit RETURN statement.
*/
if (function->fn_rettype == VOIDOID)
add_dummy_return(function);
/*
* Complete the function's info
*/
function->fn_nargs = 0;
plpgsql_finish_datums(function);
/*
* Pop the error context stack
*/
error_context_stack = plerrcontext.previous;
plpgsql_error_funcname = NULL;
plpgsql_check_syntax = false;
MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
plpgsql_compile_tmp_cxt = NULL;
return function;
}
/*
* error context callback to let us supply a call-stack traceback.
* If we are validating or executing an anonymous code block, the function
* source text is passed as an argument.
*/
static void
plpgsql_compile_error_callback(void *arg)
{
if (arg)
{
/*
* Try to convert syntax error position to reference text of original
* CREATE FUNCTION or DO command.
*/
if (function_parse_error_transpose((const char *) arg))
return;
/*
* Done if a syntax error position was reported; otherwise we have to
* fall back to a "near line N" report.
*/
}
if (plpgsql_error_funcname)
errcontext("compilation of PL/pgSQL function \"%s\" near line %d",
plpgsql_error_funcname, plpgsql_latest_lineno());
}
/*
* Add a name for a function parameter to the function's namespace
*/
/*
* Add a dummy RETURN statement to the given function's body
*/
static void
add_dummy_return(PLpgSQL_function *function)
{
/*
* If the outer block has an EXCEPTION clause, we need to make a new outer
* block, since the added RETURN shouldn't act like it is inside the
* EXCEPTION clause. Likewise, if it has a label, wrap it in a new outer
* block so that EXIT doesn't skip the RETURN.
*/
if (function->action->exceptions != NULL ||
function->action->label != NULL)
{
PLpgSQL_stmt_block *new;
new = palloc0(sizeof(PLpgSQL_stmt_block));
new->cmd_type = PLPGSQL_STMT_BLOCK;
new->stmtid = ++function->nstatements;
new->body = list_make1(function->action);
function->action = new;
}
if (function->action->body == NIL ||
((PLpgSQL_stmt *) llast(function->action->body))->cmd_type != PLPGSQL_STMT_RETURN)
{
PLpgSQL_stmt_return *new;
new = palloc0(sizeof(PLpgSQL_stmt_return));
new->cmd_type = PLPGSQL_STMT_RETURN;
new->stmtid = ++function->nstatements;
new->expr = NULL;
new->retvarno = function->out_param_varno;
function->action->body = lappend(function->action->body, new);
}
}
/*
* plpgsql_parser_setup set up parser hooks for dynamic parameters
*
* Note: this routine, and the hook functions it prepares for, are logically
* part of plpgsql parsing. But they actually run during function execution,
* when we are ready to evaluate a SQL query or expression that has not
* previously been parsed and planned.
*/
/*
* plpgsql_pre_column_ref parser callback before parsing a ColumnRef
*/
/*
* plpgsql_post_column_ref parser callback after parsing a ColumnRef
*/
/*
* plpgsql_param_ref parser callback for ParamRefs ($n symbols)
*/
/*
* resolve_column_ref attempt to resolve a ColumnRef as a plpgsql var
*
* Returns the translated node structure, or NULL if name not found
*
* error_if_no_field tells whether to throw error or quietly return NULL if
* we are able to match a record/row name but don't find a field name match.
*/
/*
* Helper for columnref parsing: build a Param referencing a plpgsql datum,
* and make sure that that datum is listed in the expression's paramnos.
*/
/* ----------
* plpgsql_parse_word The scanner calls this to postparse
* any single word that is not a reserved keyword.
*
* word1 is the downcased/dequoted identifier; it must be palloc'd in the
* function's long-term memory context.
*
* yytxt is the original token text; we need this to check for quoting,
* so that later checks for unreserved keywords work properly.
*
* We attempt to recognize the token as a variable only if lookup is true
* and the plpgsql_IdentifierLookup context permits it.
*
* If recognized as a variable, fill in *wdatum and return true;
* if not recognized, fill in *word and return false.
* (Note: those two pointers actually point to members of the same union,
* but for notational reasons we pass them separately.)
* ----------
*/
bool
plpgsql_parse_word(char *word1, const char *yytxt, bool lookup,
PLwdatum *wdatum, PLword *word)
{
PLpgSQL_nsitem *ns;
/*
* We should not lookup variables in DECLARE sections. In SQL
* expressions, there's no need to do so either --- lookup will happen
* when the expression is compiled.
*/
if (lookup && plpgsql_IdentifierLookup == IDENTIFIER_LOOKUP_NORMAL)
{
/*
* Do a lookup in the current namespace stack
*/
ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
word1, NULL, NULL,
NULL);
if (ns != NULL)
{
switch (ns->itemtype)
{
case PLPGSQL_NSTYPE_VAR:
case PLPGSQL_NSTYPE_REC:
wdatum->datum = plpgsql_Datums[ns->itemno];
wdatum->ident = word1;
wdatum->quoted = (yytxt[0] == '"');
wdatum->idents = NIL;
return true;
default:
/* plpgsql_ns_lookup should never return anything else */
elog(ERROR, "unrecognized plpgsql itemtype: %d",
ns->itemtype);
}
}
}
/*
* Nothing found - up to now it's a word without any special meaning for
* us.
*/
word->ident = word1;
word->quoted = (yytxt[0] == '"');
return false;
}
/* ----------
* plpgsql_parse_dblword Same lookup for two words
* separated by a dot.
* ----------
*/
bool
plpgsql_parse_dblword(char *word1, char *word2,
PLwdatum *wdatum, PLcword *cword)
{
PLpgSQL_nsitem *ns;
List *idents;
int nnames;
idents = list_make2(makeString(word1),
makeString(word2));
/*
* We should do nothing in DECLARE sections. In SQL expressions, we
* really only need to make sure that RECFIELD datums are created when
* needed. In all the cases handled by this function, returning a T_DATUM
* with a two-word idents string is the right thing.
*/
if (plpgsql_IdentifierLookup != IDENTIFIER_LOOKUP_DECLARE)
{
/*
* Do a lookup in the current namespace stack
*/
ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
word1, word2, NULL,
&nnames);
if (ns != NULL)
{
switch (ns->itemtype)
{
case PLPGSQL_NSTYPE_VAR:
/* Block-qualified reference to scalar variable. */
wdatum->datum = plpgsql_Datums[ns->itemno];
wdatum->ident = NULL;
wdatum->quoted = false; /* not used */
wdatum->idents = idents;
return true;
case PLPGSQL_NSTYPE_REC:
if (nnames == 1)
{
/*
* First word is a record name, so second word could
* be a field in this record. We build a RECFIELD
* datum whether it is or not --- any error will be
* detected later.
*/
PLpgSQL_rec *rec;
PLpgSQL_recfield *new;
rec = (PLpgSQL_rec *) (plpgsql_Datums[ns->itemno]);
new = plpgsql_build_recfield(rec, word2);
wdatum->datum = (PLpgSQL_datum *) new;
}
else
{
/* Block-qualified reference to record variable. */
wdatum->datum = plpgsql_Datums[ns->itemno];
}
wdatum->ident = NULL;
wdatum->quoted = false; /* not used */
wdatum->idents = idents;
return true;
default:
break;
}
}
}
/* Nothing found */
cword->idents = idents;
return false;
}
/* ----------
* plpgsql_parse_tripword Same lookup for three words
* separated by dots.
* ----------
*/
bool
plpgsql_parse_tripword(char *word1, char *word2, char *word3,
PLwdatum *wdatum, PLcword *cword)
{
PLpgSQL_nsitem *ns;
List *idents;
int nnames;
/*
* We should do nothing in DECLARE sections. In SQL expressions, we need
* to make sure that RECFIELD datums are created when needed, and we need
* to be careful about how many names are reported as belonging to the
* T_DATUM: the third word could be a sub-field reference, which we don't
* care about here.
*/
if (plpgsql_IdentifierLookup != IDENTIFIER_LOOKUP_DECLARE)
{
/*
* Do a lookup in the current namespace stack. Must find a record
* reference, else ignore.
*/
ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
word1, word2, word3,
&nnames);
if (ns != NULL)
{
switch (ns->itemtype)
{
case PLPGSQL_NSTYPE_REC:
{
PLpgSQL_rec *rec;
PLpgSQL_recfield *new;
rec = (PLpgSQL_rec *) (plpgsql_Datums[ns->itemno]);
if (nnames == 1)
{
/*
* First word is a record name, so second word
* could be a field in this record (and the third,
* a sub-field). We build a RECFIELD datum
* whether it is or not --- any error will be
* detected later.
*/
new = plpgsql_build_recfield(rec, word2);
idents = list_make2(makeString(word1),
makeString(word2));
}
else
{
/* Block-qualified reference to record variable. */
new = plpgsql_build_recfield(rec, word3);
idents = list_make3(makeString(word1),
makeString(word2),
makeString(word3));
}
wdatum->datum = (PLpgSQL_datum *) new;
wdatum->ident = NULL;
wdatum->quoted = false; /* not used */
wdatum->idents = idents;
return true;
}
default:
break;
}
}
}
/* Nothing found */
idents = list_make3(makeString(word1),
makeString(word2),
makeString(word3));
cword->idents = idents;
return false;
}
/* ----------
* plpgsql_parse_wordtype The scanner found word%TYPE. word can be
* a variable name or a basetype.
*
* Returns datatype struct, or NULL if no match found for word.
* ----------
*/
PLpgSQL_type * plpgsql_parse_wordtype(char *ident) { return NULL; }
/* ----------
* plpgsql_parse_cwordtype Same lookup for compositeword%TYPE
* ----------
*/
PLpgSQL_type * plpgsql_parse_cwordtype(List *idents) { return NULL; }
/* ----------
* plpgsql_parse_wordrowtype Scanner found word%ROWTYPE.
* So word must be a table name.
* ----------
*/
PLpgSQL_type * plpgsql_parse_wordrowtype(char *ident) { return NULL; }
/* ----------
* plpgsql_parse_cwordrowtype Scanner found compositeword%ROWTYPE.
* So word must be a namespace qualified table name.
* ----------
*/
PLpgSQL_type * plpgsql_parse_cwordrowtype(List *idents) { return NULL; }
/*
* plpgsql_build_variable - build a datum-array entry of a given
* datatype
*
* The returned struct may be a PLpgSQL_var or PLpgSQL_rec
* depending on the given datatype, and is allocated via
* palloc. The struct is automatically added to the current datum
* array, and optionally to the current namespace.
*/
PLpgSQL_variable *
plpgsql_build_variable(const char *refname, int lineno, PLpgSQL_type *dtype,
bool add2namespace)
{
PLpgSQL_variable *result;
switch (dtype->ttype)
{
case PLPGSQL_TTYPE_SCALAR:
{
/* Ordinary scalar datatype */
PLpgSQL_var *var;
var = palloc0(sizeof(PLpgSQL_var));
var->dtype = PLPGSQL_DTYPE_VAR;
var->refname = pstrdup(refname);
var->lineno = lineno;
var->datatype = dtype;
/* other fields are left as 0, might be changed by caller */
/* preset to NULL */
var->value = 0;
var->isnull = true;
var->freeval = false;
plpgsql_adddatum((PLpgSQL_datum *) var);
if (add2namespace)
plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR,
var->dno,
refname);
result = (PLpgSQL_variable *) var;
break;
}
case PLPGSQL_TTYPE_REC:
{
/* Composite type -- build a record variable */
PLpgSQL_rec *rec;
rec = plpgsql_build_record(refname, lineno,
dtype, dtype->typoid,
add2namespace);
result = (PLpgSQL_variable *) rec;
break;
}
case PLPGSQL_TTYPE_PSEUDO:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("variable \"%s\" has pseudo-type %s",
refname, format_type_be(dtype->typoid))));
result = NULL; /* keep compiler quiet */
break;
default:
elog(ERROR, "unrecognized ttype: %d", dtype->ttype);
result = NULL; /* keep compiler quiet */
break;
}
return result;
}
/*
* Build empty named record variable, and optionally add it to namespace
*/
PLpgSQL_rec *
plpgsql_build_record(const char *refname, int lineno,
PLpgSQL_type *dtype, Oid rectypeid,
bool add2namespace)
{
PLpgSQL_rec *rec;
rec = palloc0(sizeof(PLpgSQL_rec));
rec->dtype = PLPGSQL_DTYPE_REC;
rec->refname = pstrdup(refname);
rec->lineno = lineno;
/* other fields are left as 0, might be changed by caller */
rec->datatype = dtype;
rec->rectypeid = rectypeid;
rec->firstfield = -1;
rec->erh = NULL;
plpgsql_adddatum((PLpgSQL_datum *) rec);
if (add2namespace)
plpgsql_ns_additem(PLPGSQL_NSTYPE_REC, rec->dno, rec->refname);
return rec;
}
/*
* Build a row-variable data structure given the component variables.
* Include a rowtupdesc, since we will need to materialize the row result.
*/
/*
* Build a RECFIELD datum for the named field of the specified record variable
*
* If there's already such a datum, just return it; we don't need duplicates.
*/
PLpgSQL_recfield *
plpgsql_build_recfield(PLpgSQL_rec *rec, const char *fldname)
{
PLpgSQL_recfield *recfield;
int i;
/* search for an existing datum referencing this field */
i = rec->firstfield;
while (i >= 0)
{
PLpgSQL_recfield *fld = (PLpgSQL_recfield *) plpgsql_Datums[i];
Assert(fld->dtype == PLPGSQL_DTYPE_RECFIELD &&
fld->recparentno == rec->dno);
if (strcmp(fld->fieldname, fldname) == 0)
return fld;
i = fld->nextfield;
}
/* nope, so make a new one */
recfield = palloc0(sizeof(PLpgSQL_recfield));
recfield->dtype = PLPGSQL_DTYPE_RECFIELD;
recfield->fieldname = pstrdup(fldname);
recfield->recparentno = rec->dno;
recfield->rectupledescid = INVALID_TUPLEDESC_IDENTIFIER;
plpgsql_adddatum((PLpgSQL_datum *) recfield);
/* now we can link it into the parent's chain */
recfield->nextfield = rec->firstfield;
rec->firstfield = recfield->dno;
return recfield;
}
/*
* plpgsql_build_datatype
* Build PLpgSQL_type struct given type OID, typmod, collation,
* and type's parsed name.
*
* If collation is not InvalidOid then it overrides the type's default
* collation. But collation is ignored if the datatype is non-collatable.
*
* origtypname is the parsed form of what the user wrote as the type name.
* It can be NULL if the type could not be a composite type, or if it was
* identified by OID to begin with (e.g., it's a function argument type).
*/
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname) { PLpgSQL_type *typ; typ = (PLpgSQL_type *) palloc0(sizeof(PLpgSQL_type)); typ->typname = pstrdup("UNKNOWN"); typ->ttype = PLPGSQL_TTYPE_SCALAR; return typ; }
/*
* Utility subroutine to make a PLpgSQL_type struct given a pg_type entry
* and additional details (see comments for plpgsql_build_datatype).
*/
/*
* plpgsql_recognize_err_condition
* Check condition name and translate it to SQLSTATE.
*
* Note: there are some cases where the same condition name has multiple
* entries in the table. We arbitrarily return the first match.
*/
int
plpgsql_recognize_err_condition(const char *condname, bool allow_sqlstate)
{
int i;
if (allow_sqlstate)
{
if (strlen(condname) == 5 &&
strspn(condname, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
return MAKE_SQLSTATE(condname[0],
condname[1],
condname[2],
condname[3],
condname[4]);
}
for (i = 0; exception_label_map[i].label != NULL; i++)
{
if (strcmp(condname, exception_label_map[i].label) == 0)
return exception_label_map[i].sqlerrstate;
}
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("unrecognized exception condition \"%s\"",
condname)));
return 0; /* keep compiler quiet */
}
/*
* plpgsql_parse_err_condition
* Generate PLpgSQL_condition entry(s) for an exception condition name
*
* This has to be able to return a list because there are some duplicate
* names in the table of error code names.
*/
PLpgSQL_condition *
plpgsql_parse_err_condition(char *condname)
{
int i;
PLpgSQL_condition *new;
PLpgSQL_condition *prev;
/*
* XXX Eventually we will want to look for user-defined exception names
* here.
*/
/*
* OTHERS is represented as code 0 (which would map to '00000', but we
* have no need to represent that as an exception condition).
*/
if (strcmp(condname, "others") == 0)
{
new = palloc(sizeof(PLpgSQL_condition));
new->sqlerrstate = 0;
new->condname = condname;
new->next = NULL;
return new;
}
prev = NULL;
for (i = 0; exception_label_map[i].label != NULL; i++)
{
if (strcmp(condname, exception_label_map[i].label) == 0)
{
new = palloc(sizeof(PLpgSQL_condition));
new->sqlerrstate = exception_label_map[i].sqlerrstate;
new->condname = condname;
new->next = prev;
prev = new;
}
}
if (!prev)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("unrecognized exception condition \"%s\"",
condname)));
return prev;
}
/* ----------
* plpgsql_start_datums Initialize datum list at compile startup.
* ----------
*/
void
plpgsql_start_datums(void)
{
datums_alloc = 128;
plpgsql_nDatums = 0;
/* This is short-lived, so needn't allocate in function's cxt */
plpgsql_Datums = MemoryContextAlloc(plpgsql_compile_tmp_cxt,
sizeof(PLpgSQL_datum *) * datums_alloc);
/* datums_last tracks what's been seen by plpgsql_add_initdatums() */
datums_last = 0;
}
/* ----------
* plpgsql_adddatum Add a variable, record or row
* to the compiler's datum list.
* ----------
*/
void
plpgsql_adddatum(PLpgSQL_datum *newdatum)
{
if (plpgsql_nDatums == datums_alloc)
{
datums_alloc *= 2;
plpgsql_Datums = repalloc(plpgsql_Datums, sizeof(PLpgSQL_datum *) * datums_alloc);
}
newdatum->dno = plpgsql_nDatums;
plpgsql_Datums[plpgsql_nDatums++] = newdatum;
}
/* ----------
* plpgsql_finish_datums Copy completed datum info into function struct.
* ----------
*/
void
plpgsql_finish_datums(PLpgSQL_function *function)
{
Size copiable_size = 0;
int i;
function->ndatums = plpgsql_nDatums;
function->datums = palloc(sizeof(PLpgSQL_datum *) * plpgsql_nDatums);
for (i = 0; i < plpgsql_nDatums; i++)
{
function->datums[i] = plpgsql_Datums[i];
/* This must agree with copy_plpgsql_datums on what is copiable */
switch (function->datums[i]->dtype)
{
case PLPGSQL_DTYPE_VAR:
case PLPGSQL_DTYPE_PROMISE:
copiable_size += MAXALIGN(sizeof(PLpgSQL_var));
break;
case PLPGSQL_DTYPE_REC:
copiable_size += MAXALIGN(sizeof(PLpgSQL_rec));
break;
default:
break;
}
}
function->copiable_size = copiable_size;
}
/* ----------
* plpgsql_add_initdatums Make an array of the datum numbers of
* all the initializable datums created since the last call
* to this function.
*
* If varnos is NULL, we just forget any datum entries created since the
* last call.
*
* This is used around a DECLARE section to create a list of the datums
* that have to be initialized at block entry. Note that datums can also
* be created elsewhere than DECLARE, eg by a FOR-loop, but it is then
* the responsibility of special-purpose code to initialize them.
* ----------
*/
int
plpgsql_add_initdatums(int **varnos)
{
int i;
int n = 0;
/*
* The set of dtypes recognized here must match what exec_stmt_block()
* cares about (re)initializing at block entry.
*/
for (i = datums_last; i < plpgsql_nDatums; i++)
{
switch (plpgsql_Datums[i]->dtype)
{
case PLPGSQL_DTYPE_VAR:
case PLPGSQL_DTYPE_REC:
n++;
break;
default:
break;
}
}
if (varnos != NULL)
{
if (n > 0)
{
*varnos = (int *) palloc(sizeof(int) * n);
n = 0;
for (i = datums_last; i < plpgsql_nDatums; i++)
{
switch (plpgsql_Datums[i]->dtype)
{
case PLPGSQL_DTYPE_VAR:
case PLPGSQL_DTYPE_REC:
(*varnos)[n++] = plpgsql_Datums[i]->dno;
default:
break;
}
}
}
else
*varnos = NULL;
}
datums_last = plpgsql_nDatums;
return n;
}
/*
* Compute the hashkey for a given function invocation
*
* The hashkey is returned into the caller-provided storage at *hashkey.
*/
/*
* This is the same as the standard resolve_polymorphic_argtypes() function,
* except that:
* 1. We go ahead and report the error if we can't resolve the types.
* 2. We treat RECORD-type input arguments (not output arguments) as if
* they were polymorphic, replacing their types with the actual input
* types if we can determine those. This allows us to create a separate
* function cache entry for each named composite type passed to such an
* argument.
* 3. In validation mode, we have no inputs to look at, so assume that
* polymorphic arguments are integer, integer-array or integer-range.
*/
/*
* delete_function - clean up as much as possible of a stale function cache
*
* We can't release the PLpgSQL_function struct itself, because of the
* possibility that there are fn_extra pointers to it. We can release
* the subsidiary storage, but only if there are no active evaluations
* in progress. Otherwise we'll just leak that storage. Since the
* case would only occur if a pg_proc update is detected during a nested
* recursive call on the function, a leak seems acceptable.
*
* Note that this can be called more than once if there are multiple fn_extra
* pointers to the same function cache. Hence be careful not to do things
* twice.
*/
/* exported so we can call it from _PG_init() */
|