Prefer argument name over "$n" for the refname of a plpgsql argument.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 11 Sep 2017 20:24:34 +0000 (16:24 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 11 Sep 2017 20:24:43 +0000 (16:24 -0400)
If a function argument has a name, use that as the "refname" of the
PLpgSQL_datum representing the argument, instead of $n as before.
This allows better error messages in some cases.

Pavel Stehule, reviewed by Jeevan Chalke

Discussion: https://postgr.es/m/CAFj8pRB9GyU2U1Sb2ssgP26DZ_yq-FYDfpvUvGQ=k4R=yOPVjg@mail.gmail.com

src/pl/plpgsql/src/pl_comp.c
src/test/regress/expected/plpgsql.out
src/test/regress/sql/plpgsql.sql

index e9d7ef55e97618e06ca0b6eb5ce596bf80f27684..9931ee038fed14c6200e2052460a219fb51198a1 100644 (file)
@@ -433,9 +433,14 @@ do_compile(FunctionCallInfo fcinfo,
                             errmsg("PL/pgSQL functions cannot accept type %s",
                                    format_type_be(argtypeid))));
 
-               /* Build variable and add to datum list */
-               argvariable = plpgsql_build_variable(buf, 0,
-                                                    argdtype, false);
+               /*
+                * Build variable and add to datum list.  If there's a name
+                * for the argument, use that as refname, else use $n name.
+                */
+               argvariable = plpgsql_build_variable((argnames &&
+                                                     argnames[i][0] != '\0') ?
+                                                    argnames[i] : buf,
+                                                    0, argdtype, false);
 
                if (argvariable->dtype == PLPGSQL_DTYPE_VAR)
                {
index 71099969a47e8db16478db61f3d476ca4859e5d0..7d3e9225bb24df8cb8360069790849ce28b79bd6 100644 (file)
@@ -6029,3 +6029,14 @@ SELECT * FROM list_partitioned_table() AS t;
  2
 (2 rows)
 
+--
+-- Check argument name is used instead of $n in error message
+--
+CREATE FUNCTION fx(x WSlot) RETURNS void AS $$
+BEGIN
+  GET DIAGNOSTICS x = ROW_COUNT;
+  RETURN;
+END; $$ LANGUAGE plpgsql;
+ERROR:  "x" is not a scalar variable
+LINE 3:   GET DIAGNOSTICS x = ROW_COUNT;
+                          ^
index 771d68282eed8c744f77f9bcf955ce23202ee835..6c9399696bf796456470ba68d36252907b9fcee4 100644 (file)
@@ -4811,3 +4811,12 @@ BEGIN
 END; $$ LANGUAGE plpgsql;
 
 SELECT * FROM list_partitioned_table() AS t;
+
+--
+-- Check argument name is used instead of $n in error message
+--
+CREATE FUNCTION fx(x WSlot) RETURNS void AS $$
+BEGIN
+  GET DIAGNOSTICS x = ROW_COUNT;
+  RETURN;
+END; $$ LANGUAGE plpgsql;