I was giving some thought to how psql handles undefined variables.

I would like an option where either psql can provide an alternate value
when an undefined variable is referenced, or a way to detect that a
specific variable is undefined and replace it with a defined variable.

My first thought thought was to have a


\set_if_undefined var_name 'default_value'


Another idea adding a \pset parameter that would return a specific value
when an undefined psql variable is referenced instead of raising an error.
Like this:

# select :'x' as value_of_x;
ERROR:  syntax error at or near ":"
LINE 1: select :'x' as value_of_x;
               ^

# \pset variable_default ''
analytics=# select :'x' as value_of_x;
 value_of_x
------------

(1 row)

# \pset variable_default ''
# select :'x' as value_of_x;
ERROR:  syntax error at or near ":"
LINE 1: select :'x' as value_of_x;
               ^

This would end up having behavior somewhat similar to +e/-e in bash, where
a paranoid script could do something like this:

\pset variable_default 'default1'
select :'required_var1' as required_var1 \gset
\pset variable_default 'default2'
select :'required_var2' as required_var2 \gset
-- reset to default behavior
\pset variable_default error


Thus setting sane defaults to vars that weren't assigned at invocation time.

Thoughts?

Reply via email to