2
votes

A performance-critical plpgsql function in PostgreSQL 9.2 has a "RAISE DEBUG" statement that calls an expensive function for one of the arguments, ie.

RAISE DEBUG 'Details: %', expensive_function(...);

It appears that the function is called even when DEBUG logging is disabled. Is there a way I can check what the current logging level is (both log_min_messages and client_min_messages) in an IF statement or some other way I can call the function only if needed?

1
Check the exception clause. postgresql.org/docs/current/static/…Clodoaldo Neto
I've never read any docs to confirm this, but my observation has been that the raise statement will always fire and you can only set the configuration to either actually write the log to the client or server or not to. So there's no way without putting an if check before your raise to check if you want to log or not. That's how I have handled it. Not perfect, but tolerable.Kuberchaun
I'm happy to put in an IF check, I just don't know what the condition would be - that's what this question is about.EM0
@Clodoaldo Neto, I don't see the relevance of the EXCEPTION clause. Could you elaborate?EM0

1 Answers

3
votes

You can use SHOW to retrieve the debug level into a variable and then test it as a piece of text.
Example in plpgsql:

DECLARE
  dbg_level text;
BEGIN
  SHOW client_min_messages INTO dbg_level;
  IF (dbg_level ilike 'debug%') THEN
    RAISE DEBUG 'details: %', expensive_function();
  END IF;
END;