fix(supabase): V1-2.1 walk the document's numbers in linear time

jsonb_path_query row by row is quadratic on PostgreSQL 17: a document of
200k numbers under the cap ran into the statement timeout. One
jsonb_path_query_array call, then its elements. pgTAP 017 saves 600 000
numbers; the perf suite times 600k (saved) and 1M (closed code) over HTTP.
This commit is contained in:
bchanot committed 2026-10-28 20:34:35 +01:00
1 parent 7c4bf124f4
commit 538d10369f
4 files changed
+39 -4

No files matched your search

+6 -1
View File
@@ -8,7 +8,7 @@
-- ids created here, so the file also runs on a stack that already holds
-- sheets, rounds or a working copy.
begin;
select plan(110);
select plan(111);
-- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin +
-- editor + reviewer (an author of a later round), V3 reviewer, R reader.
@@ -683,6 +683,11 @@ select throws_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_exp)
select throws_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}',
(select jsonb_agg(1e131071) from generate_series(1, 9000)))) $$,
'23514', 'document.too_large', 'size: numbers that print as gigabytes of digits get the closed code, not an out-of-memory error');
-- 600 000 small numbers: within the cap as text (1.8 MB); the number walk is
-- linear, so the save completes (a quadratic walk would run for minutes here).
select lives_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}',
(select jsonb_agg(0) from generate_series(1, 600000)))) $$,
'size: a document of 600 000 small numbers within the cap is saved');
reset role;
select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'),
'CHECK ((octet_length((content)::text) <= 2097152))',
+3 -2
View File
@@ -93,8 +93,9 @@ end $$;
grant execute on all functions in schema pg_temp to authenticated;
grant select on pg_temp.who to authenticated;
-- 6 callers x 2 levels x 2 operations x 4 roles x 2 targets = 192 cases: 16
-- allowed (one assertion), 176 refused (two assertions).
-- 6 callers x 2 levels x 2 operations x 4 roles x 2 targets = 192 cases: 12
-- allowed (one assertion: owner 4 roles, admin 2, each for grant and revoke),
-- 180 refused (two assertions).
select pg_temp.role_case(c, l, o, r, s)
from unnest(array['N', 'O', 'A', 'M', 'S', 'D']) with ordinality c(c, ci),
unnest(array['aal1', 'aal2']) with ordinality l(l, li),