From 5a1ed11924ceef403cbff11db40e3057e1077623 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Mon, 4 Sep 2023 18:12:10 -0300 Subject: [PATCH] note on current_setting inconsistency Closes #362. --- docs/references/transactions.rst | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/references/transactions.rst b/docs/references/transactions.rst index 1e732cd7a..ee81e9d1c 100644 --- a/docs/references/transactions.rst +++ b/docs/references/transactions.rst @@ -115,7 +115,6 @@ PostgREST stores the headers, cookies and headers as JSON. To get them: .. important:: - The headers names are lowercased. e.g. If the request sends ``User-Agent: x`` this will be obtainable as ``current_setting('request.headers', true)::json->>'user-agent'``. .. code-block:: postgresql @@ -131,9 +130,21 @@ PostgREST stores the headers, cookies and headers as JSON. To get them: -- value of the email claim in a jwt SELECT current_setting('request.jwt.claims', true)::json->>'email'; -.. note:: +.. important:: - The ``role`` in ``request.jwt.claims`` defaults to the value of :ref:`db-anon-role`. + - The headers names are lowercased. e.g. If the request sends ``User-Agent: x`` this will be obtainable as ``current_setting('request.headers', true)::json->>'user-agent'``. + - The ``role`` in ``request.jwt.claims`` defaults to the value of :ref:`db-anon-role`. + - Settings don't become NULL after the transaction is committed, instead they're set to a an empty string ``''``. + + + This is considered expected behavior by PostgreSQL. For more details, see `this discussion `_. + + To avoid this inconsistency, you can create a wrapper function like: + + .. code-block:: postgresql + + CREATE FUNCTION my_current_setting(text) RETURNS text + LANGUAGE SQL AS $$ + SELECT nullif(current_setting($1, true), ''); + $$; .. _guc_legacy_names: