I'd like to declare a variable on the psql command line, e.g. -v log_level=DEBUG, then be able to access this variable from within a utility function that is used by my code to log messages to a table. The logging level will determine whether or not to log the message. I've tried the various formats I've found on the internet, e.g.


CREATE OR REPLACE FUNCTION add_log_msg( v_proc_name varchar, v_msg_log_level varchar(16), v_log_msg varchar)
RETURNS void AS
$BODY$

DECLARE
v_log_level varchar;
BEGIN

v_log_level := :log_level;
v_log_level := :'log_level';

and some other variations using back slashes and double quotes, but all produce either syntax errors or the wrong results.

I'm calling my main function like this :

psql ..... -v log_level=DEBUG -c "select my_main_function()"

The function "my_main_function()" calls add_log_msg() at various points so I can debug my code.

I'd prefer to avoid creating any new postgresql.conf variables or a new debugging parameter table.

Any ideas ?