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

@@ -210,5 +210,30 @@ describe('timing at the caps (§15)', () => {
for (const op of ['save', 'submit', 'publish'])
expect(Math.max(...samples[op]), `${op} at the caps`).toBeLessThan(BUDGET_MS)
}, 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)
})