Subject: RESET, NULL and empty-string valued settings and transaction isolation

From: Marcelo Zabani <[email-masked]>
Date: Sat, 19 Oct 2024 11:07:06 -0300

Hi, according to the docs, RESET sets the value of a setting to
"The default value is defined as the value that the parameter would have
had, if no SET had ever been issued for it in the current session"

Which confuses me given that the value starts being NULL in the session and
then turns into an empty string:
$ psql
=> select current_setting('my.test', true) is null; -- true
=> set my.test = 'abc';
=> reset my.test;
=> select current_setting('my.test', true) is null; -- false
=> select current_setting('my.test', true)=''; -- true

A similar effect happens with transactions and SET LOCAL:
=> begin;
=*> set local my.test='abc';
=*> rollback; -- commit works too
=> select current_setting('my.test', true) = ''; -- true

Is this expected? I thought even if I misunderstand the docs, the effect
isn't very nice because SQL like
current_setting('my.some_boolean_setting')::boolean will fail after a
transaction with SET LOCAL sets it, a side-effect that can be particularly
confusing and basically requires usage of nullif(.., '') or other explicit
checks around every current_setting call-site in practice.

Thanks in advance,
Marcelo.

---

From: "David G. Johnston" <[email-masked]>
Date: Sat, 19 Oct 2024 08:03:33 -0700

On Saturday, October 19, 2024, Marcelo Zabani <[email-masked]> wrote:

> Which confuses me given that the value starts being NULL in the session
> and then turns into an empty string:
> $ psql
> isn't very nice because SQL like current_setting('my.some_boolean_setting')::boolean
> will fail after a transaction with SET LOCAL sets it, a side-effect that
> can be particularly confusing and basically requires usage of nullif(..,
> '') or other explicit checks around every current_setting call-site in
> practice.
>

The observed behavior is also the documented behavior.

A setting value can never actually be the NULL value.  NULL is only an
indicator that a setting doesn’t exist - i.e., when current_setting returns
null it is not giving you the value of the named setting but instead giving
you an alternative representation to an error message that the setting
doesn’t exist.

You can only reset a setting that does exist at which point it resets to
its default/initial value.  For custom settings this is always text and the
default/initial value for a text setting is the empty string.

There is a pending documentation patch about NULL values that enhances the
coverage of this in the documentation.

If you design a system that has to deal with a setting not existing in the
session then you at least have the ability to test for that case without
provoking an error.  But it’s better IMO to design the system to ensure the
setting always exists up front and let its absence be an error that
indicates a coding bug that needs to be fixed.

There is also an open patch to add proper session variables and formally
deprecate this hack-ish usage of the settings subsystem for this use case.

David J.