Skip to content

Commit 0cbd5df

Browse files
committed
fix: request failures when work_mem is set on a role
1 parent fae5817 commit 0cbd5df

7 files changed

Lines changed: 47 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. From versio
2121
- Shutdown should wait for in flight requests by @mkleczek in #4702
2222
- Remove automatic transaction retries on `40001 (serialization_failure)` errors to prevent replication lag by @laurenceisla in #3673
2323
- Fix unexpected results when embedding and filtering the same table more than once by @laurenceisla in #4075
24+
- Fix request failures when `work_mem` is set on a role by @laurenceisla in #4955
2425

2526
### Changed
2627

src/PostgREST/Config/Database.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Control.Arrow ((***))
1616
import PostgREST.Config.PgVersion (PgVersion (..), pgVersion150)
1717

1818
import qualified Data.HashMap.Strict as HM
19+
import qualified Data.Text as T
1920

2021
import qualified Hasql.Decoders as HD
2122
import qualified Hasql.Encoders as HE
@@ -32,8 +33,8 @@ type RoleSettings = (HM.HashMap ByteString (HM.HashMap ByteString ByteString
3233
type RoleIsolationLvl = HM.HashMap ByteString SQL.IsolationLevel
3334
type TimezoneNames = Set Text -- cache timezone names for prefer timezone=
3435

35-
toIsolationLevel :: (Eq a, IsString a) => a -> SQL.IsolationLevel
36-
toIsolationLevel a = case a of
36+
toIsolationLevel :: Text -> SQL.IsolationLevel
37+
toIsolationLevel a = case T.toLower a of
3738
"repeatable read" -> SQL.RepeatableRead
3839
"serializable" -> SQL.Serializable
3940
_ -> SQL.ReadCommitted
@@ -148,7 +149,7 @@ queryRoleSettings pgVer =
148149
SELECT
149150
rolname,
150151
substr(setting, 1, strpos(setting, '=') - 1) as key,
151-
lower(substr(setting, strpos(setting, '=') + 1)) as value
152+
substr(setting, strpos(setting, '=') + 1) as value
152153
FROM role_setting
153154
),
154155
iso_setting AS (

src/PostgREST/SchemaCache.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ funcsSqlQuery = encodeUtf8 [trimming|
461461
bt.oid <> bt.base_type as rettype_is_composite_alias,
462462
p.provolatile,
463463
p.provariadic > 0 as hasvariadic,
464-
lower((regexp_split_to_array((regexp_split_to_array(iso_config, '='))[2], ','))[1]) AS transaction_isolation_level,
464+
(regexp_split_to_array((regexp_split_to_array(iso_config, '='))[2], ','))[1] AS transaction_isolation_level,
465465
coalesce(func_settings.kvs, '{}') as kvs
466466
FROM pg_proc p
467467
LEFT JOIN arguments a ON a.oid = p.oid

test/io/__snapshots__/test_cli/test_schema_cache_snapshot[dbRoutines].yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,23 @@
582582
pdSchema: public
583583
pdVolatility: Volatile
584584

585+
- - qiName: get_work_mem
586+
qiSchema: public
587+
- - pdDescription: null
588+
pdFuncSettings: []
589+
pdHasVariadic: false
590+
pdName: get_work_mem
591+
pdParams: []
592+
pdReturnType:
593+
contents:
594+
contents:
595+
qiName: text
596+
qiSchema: pg_catalog
597+
tag: Scalar
598+
tag: Single
599+
pdSchema: public
600+
pdVolatility: Volatile
601+
585602
- - qiName: notify_do_nothing
586603
qiSchema: public
587604
- - pdDescription: null

test/io/fixtures/roles.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ CREATE ROLE postgrest_test_author;
88
CREATE ROLE postgrest_test_serializable;
99
CREATE ROLE postgrest_test_repeatable_read;
1010
CREATE ROLE postgrest_test_w_superuser_settings;
11+
CREATE ROLE postgrest_test_work_mem;
1112

1213
GRANT
1314
postgrest_test_anonymous, postgrest_test_author,
1415
postgrest_test_serializable, postgrest_test_repeatable_read,
15-
postgrest_test_w_superuser_settings TO :"PGUSER";
16+
postgrest_test_w_superuser_settings, postgrest_test_work_mem TO :"PGUSER";
1617

1718
ALTER ROLE :"PGUSER" SET pgrst.db_anon_role = 'postgrest_test_anonymous';
1819
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';
2324

2425
ALTER ROLE postgrest_test_anonymous SET statement_timeout TO '5s';
2526
ALTER ROLE postgrest_test_author SET statement_timeout TO '10s';
27+
28+
ALTER ROLE postgrest_test_work_mem SET work_mem TO '3MB';

test/io/fixtures/schema.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,7 @@ create or replace function custom_vary_hdr() returns void as $$
265265
perform set_config('response.headers', '[{"Vary": "X-Test-Accept"}]', false);
266266
end
267267
$$ language plpgsql;
268+
269+
create or replace function get_work_mem() returns text as $$
270+
select current_setting('work_mem', true);
271+
$$ language sql;

test/io/test_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,3 +2178,19 @@ def test_positive_pool_metric(defaultenv):
21782178
).group(1)
21792179
)
21802180
assert metrics >= 0
2181+
2182+
2183+
def test_work_mem_in_role_settings(defaultenv):
2184+
"Should work when setting work_mem on a role. See https://github.com/PostgREST/postgrest/issues/4955"
2185+
2186+
env = {
2187+
**defaultenv,
2188+
"PGRST_JWT_SECRET": SECRET,
2189+
}
2190+
2191+
headers = jwtauthheader({"role": "postgrest_test_work_mem"}, SECRET)
2192+
2193+
with run(env=env) as postgrest:
2194+
response = postgrest.session.post("/rpc/get_work_mem", headers=headers)
2195+
assert response.status_code == 200
2196+
assert response.text == '"3MB"'

0 commit comments

Comments
 (0)