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

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Marcelo Zabani <mzabani(at)gmail(dot)com>
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: RESET, NULL and empty-string valued settings and transaction isolation
Date: 2024-10-19 15:03:33
Message-ID: CAKFQuwYN72-k=OQ9FxL0BawvOTfp7si++m4dvMLzE3Xbf0sg+A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Saturday, October 19, 2024, Marcelo Zabani <mzabani(at)gmail(dot)com> wrote:

> 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.
>

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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2024-10-19 15:07:46 Re: RESET, NULL and empty-string valued settings and transaction isolation
Previous Message Marcelo Zabani 2024-10-19 14:07:06 RESET, NULL and empty-string valued settings and transaction isolation