A comment about control flow and variables: in branches that are not taken, variables are expanded nonetheless, in a way that can be surprising. Case in point:
To avoid that kind of trouble, would it make sense not to expand variables in untaken branches?
Hmmm. This case is somehow contrived (for a string, :'var' could be used, in which case escaping would avoid the hazard), but I think that what you suggest is a better behavior, if easy to implement.
-- Fabien.
Good question, Daniel. Variable expansion seems to be done via psql_get_variable which is invoked via callback, which means that I might have to move branch_block_active into PsqlSettings. That's slightly different because the existing boolean is scoped with MainLoop(), but there's no way to get to a new MainLoop unless you're in an executing branch, and no way to legally exit a MainLoop with an unbalanced if-endif state. In short, I think it's better behavior. It does prevent someone from setting a variable to '\endif' and expecting that to work, like this:
select case
when random() < 0.5 then '\endif'
else E'\else\n\echo fooled you\n\endif'
end as contrived_metaprogramming
\gset
\if false
:contrived_metaprogramming
I'm sure someone will argue that preventing that is a good thing. Unless someone sees a good reason not to move that PsqlSettings, I'll make that change.