okbob/plpgsql_check

pgplsql_check gives cryptic error if an 'empty' exception handler is executed

dgtrapeze opened this issue · 1 comments

We use pgplsql_check as supplied from yum.postgresql.org on RHEL8 PG15. We are quite fond of it and use the checker, profiler and tracer features.

On our development environments we load it via shared_preload_libraries as per https://github.com/okbob/plpgsql_check#profiler so we can use the profiler and tracer.

Recently the RPM package updated from
plpgsql_check_15-2.3.4-1.rhel8.x86_64.rpm
to
plpgsql_check_15-2.5.1-1PGDG.rhel8.x86_64.rpm
When this happened, a piece of our SQL that had worked fine for years suddenly gave a cryptic error.

We use the following in a test script to create required roles. As create role does not have an 'if not exists' option we wrap it in an exception handler as follows

DO $$
BEGIN
    begin
        create role test_role;
    exception
        when duplicate_object then
          -- Role already exists
    end;
END;
$$;

Now if this code falls into the exception handler (ie test_role does already exist), we get the following error

ERROR:  pldbgapi2 statement call stack is broken
CONTEXT:  PL/pgSQL function inline_code_block line 5 at SQL statement

This confused us as we do also use the pldebugger extension which has the name 'pldbgapi' thus we originally assumed this error was coming from the debugger extension. After some trial an error, we worked out that removing the plpgsql_check from shared_preload_libraries made this SQL 'work' again.

We then looked in the code base for your extension and found the error we were hitting in pldbgapi2.c which appears to be new for V2.5 of the extension.

We tried using other 'no-op' statements in the exception handler such as NULL; but got the same pldbgapi2 error

We had to add something that actually did something into the exception handler to get it to work again

DO $$
BEGIN
    begin
        create role test_role;
    exception
        when duplicate_object then
            raise notice 'test_role already exists';
    end;
END;
$$;
NOTICE:  test_role already exists

Seems like something in V2.5 means the empty exception handler cannot be handled by plpgsql_check.

okbob commented

should be fixed not in 2.5.3, please check