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

@@ -424,7 +424,11 @@ begin
if (p_doc -> 'schema_version') is distinct from '2'::jsonb then
raise exception 'document.schema_version' using errcode = '23514';
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);
if v_number_bytes > 2097152 then
raise exception 'document.too_large' using errcode = '23514';