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:
1 parent
7c4bf124f4
commit
538d10369f
4 files changed
+39
-4
No files matched your search
@@ -210,5 +210,30 @@ describe('timing at the caps (§15)', () => {
|
|||||||
for (const op of ['save', 'submit', 'publish'])
|
for (const op of ['save', 'submit', 'publish'])
|
||||||
expect(Math.max(...samples[op]), `${op} at the caps`).toBeLessThan(BUDGET_MS)
|
expect(Math.max(...samples[op]), `${op} at the caps`).toBeLessThan(BUDGET_MS)
|
||||||
}, 600_000)
|
}, 600_000)
|
||||||
|
|
||||||
|
// The size guard's number walk at the caps (security gate on 7c4bf12): a
|
||||||
|
// document of many small numbers within the cap saves within the budget, one
|
||||||
|
// just over the cap answers the closed code within it, never a statement timeout.
|
||||||
|
it('measures a document of many small numbers within the budget', async () => {
|
||||||
|
const base = capsDocument([null, null, null, null] as unknown as string[], 8, 0)
|
||||||
|
const sheet = ok(await rpc('create_official_sheet', { slug: `perf-n-${Date.now().toString(36)}`, document: base }, E.aal2)) as string
|
||||||
|
const draft = await sql(`select id from public.anatomical_sheet_versions where sheet_id = '${sheet}' and status = 'draft'`)
|
||||||
|
const revision = Number(await sql(`select revision from public.anatomical_sheet_versions where id = '${draft}'`))
|
||||||
|
const withNumbers = (count: number) => ({ ...base, pad: new Array<number>(count).fill(0) })
|
||||||
|
const within = await pgSizes(withNumbers(600_000))
|
||||||
|
expect(within.text).toBeLessThanOrEqual(DOC_CAP)
|
||||||
|
const saveMs = await timed(() => rpc('save_sheet_draft',
|
||||||
|
{ version_id: draft, expected_revision: revision, document: withNumbers(600_000), change_note: 'numbers' }, E.aal2))
|
||||||
|
const start = performance.now()
|
||||||
|
const over = await rpc('save_sheet_draft',
|
||||||
|
{ version_id: draft, expected_revision: revision + 1, document: withNumbers(1_000_000), change_note: 'numbers' }, E.aal2)
|
||||||
|
const overMs = performance.now() - start
|
||||||
|
console.info(`NUMBER WALK ${JSON.stringify({ within: { numbers: 600_000, textBytes: within.text, ms: Math.round(saveMs) },
|
||||||
|
over: { numbers: 1_000_000, status: over.status, ms: Math.round(overMs) } })}`)
|
||||||
|
expect(over.text).toContain('document.too_large')
|
||||||
|
expect(saveMs).toBeLessThan(BUDGET_MS)
|
||||||
|
expect(overMs).toBeLessThan(BUDGET_MS)
|
||||||
|
ok(await rpc('archive_anatomical_sheet', { sheet_id: sheet }, E.aal2))
|
||||||
|
}, 120_000)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -424,7 +424,11 @@ begin
|
|||||||
if (p_doc -> 'schema_version') is distinct from '2'::jsonb then
|
if (p_doc -> 'schema_version') is distinct from '2'::jsonb then
|
||||||
raise exception 'document.schema_version' using errcode = '23514';
|
raise exception 'document.schema_version' using errcode = '23514';
|
||||||
end if;
|
end if;
|
||||||
for v_number in select jsonb_path_query(p_doc, 'strict $.** ? (@.type() == "number")') loop
|
-- One jsonb_path_query_array call, then its elements: linear in the number
|
||||||
|
-- count (jsonb_path_query row by row is quadratic on PostgreSQL 17).
|
||||||
|
for v_number in
|
||||||
|
select jsonb_array_elements(jsonb_path_query_array(p_doc, 'strict $.** ? (@.type() == "number")'))
|
||||||
|
loop
|
||||||
v_number_bytes := v_number_bytes + octet_length(v_number::text);
|
v_number_bytes := v_number_bytes + octet_length(v_number::text);
|
||||||
if v_number_bytes > 2097152 then
|
if v_number_bytes > 2097152 then
|
||||||
raise exception 'document.too_large' using errcode = '23514';
|
raise exception 'document.too_large' using errcode = '23514';
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
-- ids created here, so the file also runs on a stack that already holds
|
-- ids created here, so the file also runs on a stack that already holds
|
||||||
-- sheets, rounds or a working copy.
|
-- sheets, rounds or a working copy.
|
||||||
begin;
|
begin;
|
||||||
select plan(110);
|
select plan(111);
|
||||||
|
|
||||||
-- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin +
|
-- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin +
|
||||||
-- editor + reviewer (an author of a later round), V3 reviewer, R reader.
|
-- 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 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)))) $$,
|
(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');
|
'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;
|
reset role;
|
||||||
select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'),
|
select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'),
|
||||||
'CHECK ((octet_length((content)::text) <= 2097152))',
|
'CHECK ((octet_length((content)::text) <= 2097152))',
|
||||||
|
|||||||
@@ -93,8 +93,9 @@ end $$;
|
|||||||
grant execute on all functions in schema pg_temp to authenticated;
|
grant execute on all functions in schema pg_temp to authenticated;
|
||||||
grant select on pg_temp.who 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
|
-- 6 callers x 2 levels x 2 operations x 4 roles x 2 targets = 192 cases: 12
|
||||||
-- allowed (one assertion), 176 refused (two assertions).
|
-- 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)
|
select pg_temp.role_case(c, l, o, r, s)
|
||||||
from unnest(array['N', 'O', 'A', 'M', 'S', 'D']) with ordinality c(c, ci),
|
from unnest(array['N', 'O', 'A', 'M', 'S', 'D']) with ordinality c(c, ci),
|
||||||
unnest(array['aal1', 'aal2']) with ordinality l(l, li),
|
unnest(array['aal1', 'aal2']) with ordinality l(l, li),
|
||||||
|
|||||||
Reference in new issue
Block a user