postgresqlplpgsqlsyntax-checking

How to re-check an SQL function created with check_function_bodies=false?


Following this answer I learnt that I can disable syntax checking for an SQL function by setting check_function_bodies=false.

In my case: Running an upgrade using Flyway where

  1. The order of function creation is not well defined
  2. Some functions use other functions yet to be created within the same upgrade.

My question is - is it possible to issue a "recheck" of such a function without having to actually call it - once all other dependencies are in place? Something similar to Oracle's alter function ... compile.

Ideally I would like to set check_function_bodies=false at the beginning of the upgrade and then recheck every SQL function at the end of it.

I want to avoid having to:

  1. control the order in which scripts are run.
  2. re-run the function creation scripts

Things I tried:


Solution

  • I can think of two ways:

    1. You can call the language validator function directly:

      SELECT lanname, lanvalidator::regprocedure FROM pg_language;
      
        lanname   |         lanvalidator         
      ------------+------------------------------
       internal   | fmgr_internal_validator(oid)
       c          | fmgr_c_validator(oid)
       sql        | fmgr_sql_validator(oid)
       plpgsql    | plpgsql_validator(oid)
       plpython3u | plpython3_validator(oid)
      (5 rows)
      

      For SQL functions, that would work like this:

      SET check_function_bodies = off;
      CREATE FUNCTION bad() RETURNS void LANGUAGE sql AS 'SELECT $1';
      
      SET check_function_bodies = on;
      SELECT fmgr_sql_validator('bad()'::regprocedure);
      
      ERROR:  there is no parameter $1
      LINE 1: SELECT $1
                     ^
      QUERY:  SELECT $1
      
    2. You can redefine the function and check if it throws an error:

      SET check_function_bodies = on;
      DO $$BEGIN
         EXECUTE pg_get_functiondef('bad()'::regprocedure);
      END;$$;