Re: BUG #19362: Extremely log processing of jsonb_path_exists_opr

From: James Moore <james(dot)moore(at)g5labs(dot)ai>
To: dhyan(at)nataraj(dot)su, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19362: Extremely log processing of jsonb_path_exists_opr
Date: 2026-11-30 17:37:07
Message-ID: CAOYRsOS326-JRLai48dd5kdQ1mrOzZRQVFDYsa4sb=qVJA6n6g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello,

A data point for this report: with $.** over the same kind of deeply
nested array, the problem is not only time but memory, and it ends with
the backend being OOM-killed and the cluster reinitializing. Any user can
trigger it with a plain SELECT; no privileges or tables are needed.

SELECT jsonb_path_query(
(repeat('[', 10000) || repeat(']', 10000))::jsonb, '$.**');

Reproduced on 17.11, 18.6 and 19.0. With the server in a 1 GB container
(docker run --memory 1g) on 19.0:

depth 2000: returns (pg_column_size of the result is about 16 MB)
depth 4000: LOG: client backend (PID ...) was terminated by signal 9: Killed
LOG: all server processes terminated; reinitializing

Without a memory limit, depth 10000 does the same on a machine with
7.65 GB of RAM.

Why it grows: $.** returns every item at every depth, and the item found at
level k is itself an array of depth n-k. executeAnyItem() appends each of
these containers to the result list and wrapItemsInArray() rebuilds them
all, so the result needs memory quadratic in the depth (the serialized
result grows 4x per doubling of depth in our measurements). The stack-depth
checks work as intended; this is unbounded allocation, not recursion.

On master, 5a2043bf71 ("Fix transient memory leakage in jsonpath
evaluation") reduces the temporary memory, but the result itself is still
quadratic: depth 10000 allocated about 3 GB there and ended with
ERROR 53200 only because that build ran under a soft memory cap.

Erroring out once the accumulated result passes a size limit, rather than
growing until the kernel kills the process, would contain it.

Regards,
James Moore

On Mon, Nov 30, 2026 at 12:35 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 19362
> Logged by: Nikolay Shaplov
> Email address: dhyan(at)nataraj(dot)su
> PostgreSQL version: 18.1
> Operating system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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb
> @? '++$.**.**.**.**.*'::jsonpath;
>
> it will run for about twenty minutes. If you add one more *.* to the end of
> jsonpath, it will be working for many hours.
>
> This does not seems to be a good behavior.
>
> This bug have been found while fuzzing of postgres op-functions, using AFL++
> as fuzzer and LibBlobStamper as a tool for creating syntactically correct
> arguments.
>
> Andrey have also explored code related to this bug, and will shortly add
> some some ideas of it's causes.
>

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message James Moore 2026-11-30 17:37:56 Re: SIMILAR TO expressions translate wildcards where they shouldn't
Previous Message PG Bug reporting form 2026-11-30 17:33:49 BUG #19961: Deadlock detector misses a cycle between a READ ONLY DEFERRABLE safe-snapshot wait and a table lock