From: Tom Lane Date: Thu, 8 Feb 2007 18:38:16 +0000 (+0000) Subject: Rearrange use of plpgsql_add_initdatums() so that only the parsing of a X-Git-Url: http://waps.l3s.uni-hannover.de/gitweb/?a=commitdiff_plain;h=394d8a4d87924123df9a8ab6a87d2478dc3dbb6e;p=users%2Fbernd%2Fpostgres.git Rearrange use of plpgsql_add_initdatums() so that only the parsing of a DECLARE section needs to know about it. Formerly, everyplace besides DECLARE that created variables needed to do "plpgsql_add_initdatums(NULL)" to prevent those variables from being sucked up as part of a subsequent DECLARE block. This is obviously error-prone, and in fact the SQLSTATE/SQLERRM patch had failed to do it for those two variables, leading to the bug recently exhibited by Asif Ali Rehman: a DECLARE within an exception handler tried to reinitialize SQLERRM. Although the SQLSTATE/SQLERRM patch isn't in any pre-8.1 branches, and so I can't point to a demonstrable failure there, it seems wise to back-patch this into the older branches anyway, just to keep the logic similar to HEAD. --- diff --git a/src/pl/plpgsql/src/gram.y b/src/pl/plpgsql/src/gram.y index e22f7de62e..a4c8f7fa12 100644 --- a/src/pl/plpgsql/src/gram.y +++ b/src/pl/plpgsql/src/gram.y @@ -263,7 +263,6 @@ decl_sect : opt_label $$.label = $1; $$.n_initvars = 0; $$.initvarnos = NULL; - plpgsql_add_initdatums(NULL); } | opt_label decl_start { @@ -271,7 +270,6 @@ decl_sect : opt_label $$.label = $1; $$.n_initvars = 0; $$.initvarnos = NULL; - plpgsql_add_initdatums(NULL); } | opt_label decl_start decl_stmts { @@ -280,12 +278,16 @@ decl_sect : opt_label $$.label = $3; else $$.label = $1; + /* Remember variables declared in decl_stmts */ $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos)); } ; decl_start : K_DECLARE { + /* Forget any variables created before block */ + plpgsql_add_initdatums(NULL); + /* Make variable names be local to block */ plpgsql_ns_setlocal(true); } ; @@ -1033,8 +1035,6 @@ fori_var : fori_varname plpgsql_ns_additem(PLPGSQL_NSTYPE_VAR, new->varno, $1.name); - plpgsql_add_initdatums(NULL); - $$ = new; } ; diff --git a/src/pl/plpgsql/src/pl_comp.c b/src/pl/plpgsql/src/pl_comp.c index 8864218b46..8de133316e 100644 --- a/src/pl/plpgsql/src/pl_comp.c +++ b/src/pl/plpgsql/src/pl_comp.c @@ -668,12 +668,7 @@ do_compile(FunctionCallInfo fcinfo, function->found_varno = var->varno; /* - * Forget about the above created variables - */ - plpgsql_add_initdatums(NULL); - - /* - * Now parse the functions text + * Now parse the function's text */ parse_rc = plpgsql_yyparse(); if (parse_rc != 0) @@ -1648,11 +1643,17 @@ plpgsql_adddatum(PLpgSQL_datum * new) /* ---------- - * plpgsql_add_initdatums Put all datum entries created - * since the last call into the - * finishing code block so the - * block knows which variables to - * reinitialize when entered. + * plpgsql_add_initdatums Make an array of the datum numbers of + * all the simple VAR 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 VARs + * that have to be initialized at block entry. Note that VARs 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