Discussion:
Testing truthiness of GUC variables?
Abelard Hoffman
2014-09-15 06:17:42 UTC
Permalink
If I set a custom GUC variable to a boolean value, such as:

SET myapp.audit = 'on';

is there a way to test it for truthiness in the same way the standard
built-in variables are? IOW, the docs say a boolean can be written as:

Boolean values can be written as on, off, true, false, yes, no, 1, 0 (all
case-insensitive) or any unambiguous prefix of these.
Abelard Hoffman
2014-09-15 06:20:41 UTC
Permalink
Post by Abelard Hoffman
SET myapp.audit = 'on';
is there a way to test it for truthiness in the same way the standard
Boolean values can be written as on, off, true, false, yes, no, 1, 0 (all
case-insensitive) or any unambiguous prefix of these.
Sorry, hit send too soon. I meant to ask, is there a built-in function I
can call, given the value from current_setting('myapp.audit'), that will
test it using the same logic?
Kevin Grittner
2014-09-15 12:39:31 UTC
Permalink
Post by Abelard Hoffman
Boolean values can be written as on, off, true, false, yes, no,
1, 0 (all case-insensitive) or any unambiguous prefix of these.
is there a built-in function I can call, given the value from
current_setting('myapp.audit'), that will test it using the same
logic?
You can *set* a boolean setting with any of those, but that doesn't
mean it will be stored as the string you used:

test=# set default_transaction_read_only = true;
SET
test=# show default_transaction_read_only;
default_transaction_read_only
-------------------------------
on
(1 row)

test=# set default_transaction_read_only = on;
SET
test=# show default_transaction_read_only;
default_transaction_read_only
-------------------------------
on
(1 row)

test=# set default_transaction_read_only = yes;
SET
test=# show default_transaction_read_only;
default_transaction_read_only
-------------------------------
on
(1 row)

test=# set default_transaction_read_only = 1;
SET
test=# show default_transaction_read_only;
default_transaction_read_only
-------------------------------
on
(1 row)

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Abelard Hoffman
2014-09-16 05:33:38 UTC
Permalink
Post by Kevin Grittner
Post by Abelard Hoffman
Boolean values can be written as on, off, true, false, yes, no,
1, 0 (all case-insensitive) or any unambiguous prefix of these.
is there a built-in function I can call, given the value from
current_setting('myapp.audit'), that will test it using the same
logic?
You can *set* a boolean setting with any of those, but that doesn't
Thanks, but I meant for user defined variables:

test=# set myapp.foo = true;
SET
test=# show myapp.foo;
myapp.foo
-----------
true
(1 row)

test=# set myapp.foo = on;
SET
test=# show myapp.foo;
myapp.foo
-----------
on
(1 row)


I realize now though that all I need to do is cast it to bool:

test=# set myapp.foo = FA;
SET
test=# select current_setting('myapp.foo')::bool;
current_setting
-----------------
f
(1 row)

Loading...