Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgWAt-0000FH-D0 for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Oct 2021 18:00:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mgWAs-0005j6-C8 for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Oct 2021 18:00:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgWAr-0005ix-Ro for pgsql-hackers@lists.postgresql.org; Fri, 29 Oct 2021 18:00:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgWAp-0005bW-4p for pgsql-hackers@lists.postgresql.org; Fri, 29 Oct 2021 18:00:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 19TI0Eee713976 for ; Fri, 29 Oct 2021 14:00:14 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Better localization of errors in plpgsql variable initialization MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <713882.1635530331.0@sss.pgh.pa.us> Date: Fri, 29 Oct 2021 14:00:14 -0400 Message-ID: <713975.1635530414@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <713882.1635530331.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable While preparing commit a2a731d6c, I chanced to notice what seemed like an off-by-one error in the line number reported for an error occurring while we initialize a plpgsql variable. For instance regression=3D# do $$ declare x int :=3D 1/0; regression$# begin end $$; ERROR: division by zero CONTEXT: SQL expression "1/0" PL/pgSQL function inline_code_block line 2 during statement block local va= riable initialization After tracking it down, it's not that. It's that we're pointing at the BEGIN keyword, because that's what determines the lineno associated with the PLpgSQL_stmt_block struct. This seems like it could be quite confusing in a block with many variables, so here's a proposed patch that improves the reported line number to match the specific variable. The only downside that I can see here is the one additional store per variable to set err_var; but it's pretty hard to believe that that'd be measurable in context. Any objections? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="blame-init-errors-on-specific-variable-1.patch"; charset="us-ascii" Content-ID: <713882.1635530331.2@sss.pgh.pa.us> Content-Description: blame-init-errors-on-specific-variable-1.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/pl/plpgsql/src/expected/plpgsql_domain.out b/src/pl/plpgs= ql/src/expected/plpgsql_domain.out index efc877cdd1..516c2b9e08 100644 --- a/src/pl/plpgsql/src/expected/plpgsql_domain.out +++ b/src/pl/plpgsql/src/expected/plpgsql_domain.out @@ -33,7 +33,7 @@ SELECT * FROM test_assign_booltrue(true, true); = SELECT * FROM test_assign_booltrue(false, true); ERROR: value for domain booltrue violates check constraint "booltrue_che= ck" -CONTEXT: PL/pgSQL function test_assign_booltrue(boolean,boolean) line 3 = during statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_booltrue(boolean,boolean) line 2 = during statement block local variable initialization SELECT * FROM test_assign_booltrue(true, false); ERROR: value for domain booltrue violates check constraint "booltrue_che= ck" CONTEXT: PL/pgSQL function test_assign_booltrue(boolean,boolean) line 4 = at assignment @@ -76,7 +76,7 @@ ERROR: value for domain uint2 violates check constraint= "uint2_check" CONTEXT: PL/pgSQL function test_assign_uint2(integer,integer) line 4 at = assignment SELECT * FROM test_assign_uint2(-100, 50); ERROR: value for domain uint2 violates check constraint "uint2_check" -CONTEXT: PL/pgSQL function test_assign_uint2(integer,integer) line 3 dur= ing statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_uint2(integer,integer) line 2 dur= ing statement block local variable initialization SELECT * FROM test_assign_uint2(null, 1); test_assign_uint2 = ------------------- @@ -115,7 +115,7 @@ SELECT * FROM test_assign_nnint(10, 20); = SELECT * FROM test_assign_nnint(null, 20); ERROR: domain nnint does not allow null values -CONTEXT: PL/pgSQL function test_assign_nnint(integer,integer) line 3 dur= ing statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_nnint(integer,integer) line 2 dur= ing statement block local variable initialization SELECT * FROM test_assign_nnint(10, null); ERROR: domain nnint does not allow null values CONTEXT: PL/pgSQL function test_assign_nnint(integer,integer) line 4 at = assignment @@ -168,7 +168,7 @@ ERROR: value for domain ordered_pair_domain violates = check constraint "ordered_ CONTEXT: PL/pgSQL function test_assign_ordered_pair_domain(integer,integ= er,integer) line 4 at assignment SELECT * FROM test_assign_ordered_pair_domain(2,1,3); ERROR: value for domain ordered_pair_domain violates check constraint "o= rdered_pair_domain_check" -CONTEXT: PL/pgSQL function test_assign_ordered_pair_domain(integer,integ= er,integer) line 3 during statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_ordered_pair_domain(integer,integ= er,integer) line 2 during statement block local variable initialization -- -- Arrays of domains -- @@ -276,7 +276,7 @@ ERROR: domain nnint does not allow null values CONTEXT: PL/pgSQL function test_assign_nnint_container(integer,integer,i= nteger) line 4 at assignment SELECT * FROM test_assign_nnint_container(1,null,3); ERROR: domain nnint does not allow null values -CONTEXT: PL/pgSQL function test_assign_nnint_container(integer,integer,i= nteger) line 3 during statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_nnint_container(integer,integer,i= nteger) line 2 during statement block local variable initialization -- Since core system allows this: SELECT null::nnint_container; nnint_container = @@ -356,7 +356,7 @@ ERROR: value for domain ordered_named_pair violates c= heck constraint "ordered_n CONTEXT: PL/pgSQL function test_assign_ordered_named_pair(integer,intege= r,integer) line 4 at assignment SELECT * FROM test_assign_ordered_named_pair(2,1,3); ERROR: value for domain ordered_named_pair violates check constraint "or= dered_named_pair_check" -CONTEXT: PL/pgSQL function test_assign_ordered_named_pair(integer,intege= r,integer) line 3 during statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_ordered_named_pair(integer,intege= r,integer) line 2 during statement block local variable initialization CREATE FUNCTION build_ordered_named_pairs(i int, j int) RETURNS ordered_n= amed_pair[] AS $$ begin return array[row(i, j), row(i, j+1)]; @@ -388,7 +388,7 @@ SELECT * FROM test_assign_ordered_named_pairs(1,2,3); = SELECT * FROM test_assign_ordered_named_pairs(2,1,3); ERROR: value for domain ordered_named_pair violates check constraint "or= dered_named_pair_check" -CONTEXT: PL/pgSQL function test_assign_ordered_named_pairs(integer,integ= er,integer) line 3 during statement block local variable initialization +CONTEXT: PL/pgSQL function test_assign_ordered_named_pairs(integer,integ= er,integer) line 2 during statement block local variable initialization SELECT * FROM test_assign_ordered_named_pairs(1,2,0); -- should fail som= eday test_assign_ordered_named_pairs = --------------------------------- diff --git a/src/pl/plpgsql/src/expected/plpgsql_varprops.out b/src/pl/plp= gsql/src/expected/plpgsql_varprops.out index 3801dccc95..25115a02bd 100644 --- a/src/pl/plpgsql/src/expected/plpgsql_varprops.out +++ b/src/pl/plpgsql/src/expected/plpgsql_varprops.out @@ -77,7 +77,7 @@ begin end$$; ERROR: division by zero CONTEXT: SQL expression "1/0" -PL/pgSQL function inline_code_block line 3 during statement block local v= ariable initialization +PL/pgSQL function inline_code_block line 2 during statement block local v= ariable initialization do $$ declare x bigint[] :=3D array[1,3,5]; begin @@ -120,7 +120,7 @@ begin raise notice 'x =3D %', x; end$$; ERROR: null value cannot be assigned to variable "x" declared NOT NULL -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x record not null; -- fail begin @@ -147,7 +147,7 @@ begin raise notice 'x =3D %', x; end$$; ERROR: null value cannot be assigned to variable "x" declared NOT NULL -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record not null; -- fail begin @@ -174,7 +174,7 @@ begin raise notice 'x =3D %', x; end$$; ERROR: null value cannot be assigned to variable "x" declared NOT NULL -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization -- Check that variables are reinitialized on block re-entry. do $$ begin @@ -216,14 +216,14 @@ begin raise notice 'x =3D %', x; end$$; ERROR: domain int_nn does not allow null values -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x int_nn :=3D null; -- fail begin raise notice 'x =3D %', x; end$$; ERROR: domain int_nn does not allow null values -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x int_nn :=3D 42; begin @@ -239,14 +239,14 @@ begin raise notice 'x =3D %', x; end$$; ERROR: domain var_record_nn does not allow null values -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record_nn :=3D null; -- fail begin raise notice 'x =3D %', x; end$$; ERROR: domain var_record_nn does not allow null values -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record_nn :=3D row(1,2); begin @@ -263,21 +263,21 @@ begin raise notice 'x =3D %', x; end$$; ERROR: value for domain var_record_colnn violates check constraint "var_= record_colnn_check" -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record_colnn :=3D null; -- fail begin raise notice 'x =3D %', x; end$$; ERROR: value for domain var_record_colnn violates check constraint "var_= record_colnn_check" -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record_colnn :=3D row(1,null); -- fail begin raise notice 'x =3D %', x; end$$; ERROR: value for domain var_record_colnn violates check constraint "var_= record_colnn_check" -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x var_record_colnn :=3D row(1,2); begin diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 0e1cfa3df6..6dbfdb7be0 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -1214,6 +1214,20 @@ static void plpgsql_exec_error_callback(void *arg) { PLpgSQL_execstate *estate =3D (PLpgSQL_execstate *) arg; + int err_lineno; + + /* + * If err_var is set, report the variable's declaration line number. + * Otherwise, if err_stmt is set, report the err_stmt's line number. Wh= en + * err_stmt is not set, we're in function entry/exit, or some such place + * not attached to a specific line number. + */ + if (estate->err_var !=3D NULL) + err_lineno =3D estate->err_var->lineno; + else if (estate->err_stmt !=3D NULL) + err_lineno =3D estate->err_stmt->lineno; + else + err_lineno =3D 0; = if (estate->err_text !=3D NULL) { @@ -1222,13 +1236,8 @@ plpgsql_exec_error_callback(void *arg) * actually need it. Therefore, places that set up err_text should * use gettext_noop() to ensure the strings get recorded in the * message dictionary. - * - * If both err_text and err_stmt are set, use the err_text as - * description, but report the err_stmt's line number. When err_stmt - * is not set, we're in function entry/exit, or some such place not - * attached to a specific line number. */ - if (estate->err_stmt !=3D NULL) + if (err_lineno > 0) { /* * translator: last %s is a phrase such as "during statement block @@ -1236,7 +1245,7 @@ plpgsql_exec_error_callback(void *arg) */ errcontext("PL/pgSQL function %s line %d %s", estate->func->fn_signature, - estate->err_stmt->lineno, + err_lineno, _(estate->err_text)); } else @@ -1250,12 +1259,12 @@ plpgsql_exec_error_callback(void *arg) _(estate->err_text)); } } - else if (estate->err_stmt !=3D NULL) + else if (estate->err_stmt !=3D NULL && err_lineno > 0) { /* translator: last %s is a plpgsql statement type name */ errcontext("PL/pgSQL function %s line %d at %s", estate->func->fn_signature, - estate->err_stmt->lineno, + err_lineno, plpgsql_stmt_typename(estate->err_stmt)); } else @@ -1643,7 +1652,12 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_= stmt_block *block) * Note that we currently don't support promise datums within blocks, * only at a function's outermost scope, so we needn't handle those * here. + * + * Since RECFIELD isn't a supported case either, it's okay to cast the + * PLpgSQL_datum to PLpgSQL_variable. */ + estate->err_var =3D (PLpgSQL_variable *) datum; + switch (datum->dtype) { case PLPGSQL_DTYPE_VAR: @@ -1717,6 +1731,8 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_s= tmt_block *block) } } = + estate->err_var =3D NULL; + if (block->exceptions) { /* @@ -4041,6 +4057,7 @@ plpgsql_estate_setup(PLpgSQL_execstate *estate, estate->eval_econtext =3D NULL; = estate->err_stmt =3D NULL; + estate->err_var =3D NULL; estate->err_text =3D NULL; = estate->plugin_info =3D NULL; diff --git a/src/pl/plpgsql/src/plpgsql.h b/src/pl/plpgsql/src/plpgsql.h index ebd3a5d3c8..3936866bb7 100644 --- a/src/pl/plpgsql/src/plpgsql.h +++ b/src/pl/plpgsql/src/plpgsql.h @@ -1088,6 +1088,7 @@ typedef struct PLpgSQL_execstate = /* status information for error context reporting */ PLpgSQL_stmt *err_stmt; /* current stmt */ + PLpgSQL_variable *err_var; /* current variable, if in a DECLARE section = */ const char *err_text; /* additional state info */ = void *plugin_info; /* reserved for use by optional plugin */ diff --git a/src/test/regress/expected/domain.out b/src/test/regress/expec= ted/domain.out index a04bd00ac6..8faf46d404 100644 --- a/src/test/regress/expected/domain.out +++ b/src/test/regress/expected/domain.out @@ -847,7 +847,7 @@ begin end$$ language plpgsql; select doubledecrement(3); -- fail because of implicit null assignment ERROR: domain pos_int does not allow null values -CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 3 during statem= ent block local variable initialization +CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 2 during statem= ent block local variable initialization create or replace function doubledecrement(p1 pos_int) returns pos_int as= $$ declare v pos_int :=3D 0; begin @@ -855,7 +855,7 @@ begin end$$ language plpgsql; select doubledecrement(3); -- fail at initialization assignment ERROR: value for domain pos_int violates check constraint "pos_int_check= " -CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 3 during statem= ent block local variable initialization +CONTEXT: PL/pgSQL function doubledecrement(pos_int) line 2 during statem= ent block local variable initialization create or replace function doubledecrement(p1 pos_int) returns pos_int as= $$ declare v pos_int :=3D 1; begin diff --git a/src/test/regress/expected/plpgsql.out b/src/test/regress/expe= cted/plpgsql.out index ae6fc824b6..33eb5e54b9 100644 --- a/src/test/regress/expected/plpgsql.out +++ b/src/test/regress/expected/plpgsql.out @@ -4648,7 +4648,7 @@ ERROR: column "x" does not exist LINE 1: x + 1 ^ QUERY: x + 1 -CONTEXT: PL/pgSQL function inline_code_block line 3 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare y int :=3D x + 1; -- error x int :=3D 42; @@ -4660,7 +4660,7 @@ ERROR: column "x" does not exist LINE 1: x + 1 ^ QUERY: x + 1 -CONTEXT: PL/pgSQL function inline_code_block line 4 during statement blo= ck local variable initialization +CONTEXT: PL/pgSQL function inline_code_block line 2 during statement blo= ck local variable initialization do $$ declare x int :=3D 42; y int :=3D x + 1; ------- =_aaaaaaaaaa0--