Thursday, August 13, 2020

New release of plpgsql_check - now with possibility to enable, disable checks, warnings, tracing inside functions

ADA language (ancestor of PL/SQL and PL/pgSQL languages) has PRAGMA clause for entering some compiler directives. Unfortunately PL/pgSQL has not this possibility, and it is very low possibility to have a PRAGMA in future. It is not a fundamental feature, but sometimes compiler directives can be practical.

When I use plpgsql_check, I usually find a parts of code with false alarms. The static analyse used by plpgsql_check cannot to work with dynamic SQL, it cannot to work with local temp tables, etc. Can be nice to have a possibility to disable check for these parts.

Now it is possible. I wrote special function plpgsql_check_pragma, that is identified by plpgsql_check, and plpgsql_check can read own directives from an arguments of this function. A implementation of pragma in plpgsql_check has static block or statement scope:

postgres=# create or replace function test_pragma()
 returns void
 language plpgsql
as $$
declare r record;
begin
  if false then
    -- check is disabled just for if body
    perform plpgsql_check_pragma('disable:check');
    raise notice '%', r.y;
  end if;
  select 10 as a, 20 as b into r;
  raise notice '%', r.a;
  raise notice '%', r.x;
end;
$$;
CREATE FUNCTION
postgres=# select * from plpgsql_check_function('test_pragma');
┌──────────────────────────────────────────────────┐
│              plpgsql_check_function              │
╞══════════════════════════════════════════════════╡
│ error:42703:11:RAISE:record "r" has no field "x" │
│ Context: SQL statement "SELECT r.x"              │
└──────────────────────────────────────────────────┘
(2 rows)

On systems without plpgsql_check you can write own empty implementation of plpgsql_check_pragma function (because probably you don't want to change already checked source code):

create or replace function plpgsql_check_pragma(variadic text[])
returns int as $$
  select 1
$$ language sql immutable;

This functionality is available from 1.13.0 release

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home