fix: request failures when work_mem is set on a role

This commit is contained in:
Laurence Isla
2026-06-04 21:25:50 +05:00
committed by Taimoor Zaeem
parent 627e8c07ab
commit 4dc7d84ff1
7 changed files with 47 additions and 5 deletions
+1
View File
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. From versio
- Fix connection retrying message in `PGRST000` error by @netqo in #4980 - Fix connection retrying message in `PGRST000` error by @netqo in #4980
+ Remove redundant "Retrying the connection." from message because it is logged separately + Remove redundant "Retrying the connection." from message because it is logged separately
- Fix request failures when `work_mem` is set on a role by @laurenceisla in #4955
## [14.12] - 2026-05-20 ## [14.12] - 2026-05-20
+4 -3
View File
@@ -16,6 +16,7 @@ import Control.Arrow ((***))
import PostgREST.Config.PgVersion (PgVersion (..), pgVersion150) import PostgREST.Config.PgVersion (PgVersion (..), pgVersion150)
import qualified Data.HashMap.Strict as HM import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T
import qualified Hasql.Decoders as HD import qualified Hasql.Decoders as HD
import qualified Hasql.Encoders as HE import qualified Hasql.Encoders as HE
@@ -32,8 +33,8 @@ type RoleSettings = (HM.HashMap ByteString (HM.HashMap ByteString ByteString
type RoleIsolationLvl = HM.HashMap ByteString SQL.IsolationLevel type RoleIsolationLvl = HM.HashMap ByteString SQL.IsolationLevel
type TimezoneNames = Set Text -- cache timezone names for prefer timezone= type TimezoneNames = Set Text -- cache timezone names for prefer timezone=
toIsolationLevel :: (Eq a, IsString a) => a -> SQL.IsolationLevel toIsolationLevel :: Text -> SQL.IsolationLevel
toIsolationLevel a = case a of toIsolationLevel a = case T.toLower a of
"repeatable read" -> SQL.RepeatableRead "repeatable read" -> SQL.RepeatableRead
"serializable" -> SQL.Serializable "serializable" -> SQL.Serializable
_ -> SQL.ReadCommitted _ -> SQL.ReadCommitted
@@ -148,7 +149,7 @@ queryRoleSettings pgVer prepared =
SELECT SELECT
rolname, rolname,
substr(setting, 1, strpos(setting, '=') - 1) as key, substr(setting, 1, strpos(setting, '=') - 1) as key,
lower(substr(setting, strpos(setting, '=') + 1)) as value substr(setting, strpos(setting, '=') + 1) as value
FROM role_setting FROM role_setting
), ),
iso_setting AS ( iso_setting AS (
+1 -1
View File
@@ -448,7 +448,7 @@ funcsSqlQuery = encodeUtf8 [trimming|
bt.oid <> bt.base_type as rettype_is_composite_alias, bt.oid <> bt.base_type as rettype_is_composite_alias,
p.provolatile, p.provolatile,
p.provariadic > 0 as hasvariadic, p.provariadic > 0 as hasvariadic,
lower((regexp_split_to_array((regexp_split_to_array(iso_config, '='))[2], ','))[1]) AS transaction_isolation_level, (regexp_split_to_array((regexp_split_to_array(iso_config, '='))[2], ','))[1] AS transaction_isolation_level,
coalesce(func_settings.kvs, '{}') as kvs coalesce(func_settings.kvs, '{}') as kvs
FROM pg_proc p FROM pg_proc p
LEFT JOIN arguments a ON a.oid = p.oid LEFT JOIN arguments a ON a.oid = p.oid
@@ -565,6 +565,23 @@
pdSchema: public pdSchema: public
pdVolatility: Volatile pdVolatility: Volatile
- - qiName: get_work_mem
qiSchema: public
- - pdDescription: null
pdFuncSettings: []
pdHasVariadic: false
pdName: get_work_mem
pdParams: []
pdReturnType:
contents:
contents:
qiName: text
qiSchema: pg_catalog
tag: Scalar
tag: Single
pdSchema: public
pdVolatility: Volatile
- - qiName: notify_do_nothing - - qiName: notify_do_nothing
qiSchema: public qiSchema: public
- - pdDescription: null - - pdDescription: null
+4 -1
View File
@@ -8,11 +8,12 @@ CREATE ROLE postgrest_test_author;
CREATE ROLE postgrest_test_serializable; CREATE ROLE postgrest_test_serializable;
CREATE ROLE postgrest_test_repeatable_read; CREATE ROLE postgrest_test_repeatable_read;
CREATE ROLE postgrest_test_w_superuser_settings; CREATE ROLE postgrest_test_w_superuser_settings;
CREATE ROLE postgrest_test_work_mem;
GRANT GRANT
postgrest_test_anonymous, postgrest_test_author, postgrest_test_anonymous, postgrest_test_author,
postgrest_test_serializable, postgrest_test_repeatable_read, postgrest_test_serializable, postgrest_test_repeatable_read,
postgrest_test_w_superuser_settings TO :"PGUSER"; postgrest_test_w_superuser_settings, postgrest_test_work_mem TO :"PGUSER";
ALTER ROLE :"PGUSER" SET pgrst.db_anon_role = 'postgrest_test_anonymous'; ALTER ROLE :"PGUSER" SET pgrst.db_anon_role = 'postgrest_test_anonymous';
ALTER ROLE postgrest_test_serializable SET default_transaction_isolation = 'serializable'; ALTER ROLE postgrest_test_serializable SET default_transaction_isolation = 'serializable';
@@ -23,3 +24,5 @@ ALTER ROLE postgrest_test_w_superuser_settings SET log_min_messages = 'fatal';
ALTER ROLE postgrest_test_anonymous SET statement_timeout TO '2s'; ALTER ROLE postgrest_test_anonymous SET statement_timeout TO '2s';
ALTER ROLE postgrest_test_author SET statement_timeout TO '10s'; ALTER ROLE postgrest_test_author SET statement_timeout TO '10s';
ALTER ROLE postgrest_test_work_mem SET work_mem TO '3MB';
+4
View File
@@ -258,3 +258,7 @@ $_$ language sql;
create or replace function notify_pgrst() returns void as $$ create or replace function notify_pgrst() returns void as $$
notify pgrst; notify pgrst;
$$ language sql; $$ language sql;
create or replace function get_work_mem() returns text as $$
select current_setting('work_mem', true);
$$ language sql;
+16
View File
@@ -1833,3 +1833,19 @@ def test_positive_pool_metric(defaultenv):
).group(1) ).group(1)
) )
assert metrics >= 0 assert metrics >= 0
def test_work_mem_in_role_settings(defaultenv):
"Should work when setting work_mem on a role. See https://github.com/PostgREST/postgrest/issues/4955"
env = {
**defaultenv,
"PGRST_JWT_SECRET": SECRET,
}
headers = jwtauthheader({"role": "postgrest_test_work_mem"}, SECRET)
with run(env=env) as postgrest:
response = postgrest.session.post("/rpc/get_work_mem", headers=headers)
assert response.status_code == 200
assert response.text == '"3MB"'