Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wHou6-007XT2-2w for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Apr 2026 20:19:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wHou5-000w04-08 for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Apr 2026 20:19:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wHou4-000vzw-27 for pgsql-bugs@lists.postgresql.org; Tue, 28 Apr 2026 20:19:32 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wHou1-00000003Ewg-3hHU for pgsql-bugs@lists.postgresql.org; Tue, 28 Apr 2026 20:19:31 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-ba52aec0fdeso164871366b.0 for ; Tue, 28 Apr 2026 13:19:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1777407567; cv=none; d=google.com; s=arc-20240605; b=F2pb2UgFbZFUh+HthSZPE4HTmC/WVbvo0vh1lY8NJ8u8gSyzu92inxHQcV1LDInCZw eymDP79JCpaoUDusD1dretUROlJncsIPt1HwxzD6FD+FVw4yt1Zeo1Y0xIC3Uq433M/a 8H20zFN3DqC5B2dmuXHtQX4X1Yj2nf6w/dtwuTDOizJfW0OLltyQJMpFu3bQlPZpv8Jr nwj3Mbwj1lPgEKN7PtTB6VHJpPJABNiZ1qYk+MjzBl7tvHWTNFYez+Q9oTkURlnLXfI3 +Dc+fiJTS5zLViZ1j8pXr+vjLZ2mATlrmcY9DyP5dJewhrIEzzfYvTE6ompEj0p+qqqr a6Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=KEVGX4aRrbI25YpnlaPqbsQCtDvtNBeI5zcQoG6x+0Y=; fh=fkSi2HHOqN++I7Y6qdDlHQqq6xmVtCEiKLYA6zHMXn8=; b=TDqY3AYgx1yGfKC9WRb6dV8FO6tUqmToIuNvyQX29NS9ft13k1lwlSeXj7fM1yYawS 0gRtYYm3rjcQkk3buFrwIFbGgD6fd2X0LdDpM1VOv+Yp1UILZkzm09e36QFIN6EjTyh/ m9pkRa6oZFe+htIOyvbvXC5N/WZ709RBKBKwURDNNm5slLpZzEHQCvp6tvTFaxZftTgO qz7eWCKvb2Qpw7VIjAYxgcfZ7RSVFculm031HE88tCgt67LQjLHOR5wzK1BfBNUyYioI jECde00OenrJs73R9L3zoQks8PCapiH5vVv1frteA55gbAG2zJl42ZlBBZ+V7TIYlpr5 xXZQ==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777407567; x=1778012367; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=KEVGX4aRrbI25YpnlaPqbsQCtDvtNBeI5zcQoG6x+0Y=; b=QFLWzOjhl+uj0QkE1NkKi5CEankTIm0pCrMLiuWa4213A8Nz/jWDmsPdYi24T65DzU 259PT6Zg6panPAntMzvRbp4rY+MtiZUp4cmcWCtdf4yzaCpszhzFxURMDkKiI3tEhOEZ /RhkJyPJydxl1exti7e7WZWY9mOyIqq3DcOKEPQtmfSeG7tO5znEY3dx2P83J4Vc7MxI BEVY+3yEKakeG4UTeQhb24BAnU4C/bSedJCn9+FVuXEO1QZBeg/0Avh6Bf+KeMbonsS8 vKSZ9hNIsn+1WNqNV388buUafansJzGGmJYN5lyGS0imDX3VsYUY1Ix5HVFm3O40B4y5 lp5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777407567; x=1778012367; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KEVGX4aRrbI25YpnlaPqbsQCtDvtNBeI5zcQoG6x+0Y=; b=Z9sFD0Edd0LCXN4aI+bBk2ih7RJRaEMgn4xG77XCFH7/zIaF98nVukU2FuuXl6GY6C FQofL745LhbXo7OXBBczuohFoQU5G4JInCrcFF0pLkTxNlzdLRmjb95CAuZGmFvvpgIS Ng1kubeKbB3FGETL8rxzFTYmiTDEq0tBzvWVGG1z7YFhq6dm4mXOef5i3G6Dmceo7MYg ydmOelOoNz7SondaZ1d/TpXGPwKQOOClh4lK1Y+JiMH3TMMnWUEzYanSiBxsktyIOsjM cVrXI64JBgRXYjA//wLIWNokQQ+bwJtZxBSXUtOM+9VNQttD5r7bkUR3269kJImxSQ3+ 2QHg== X-Forwarded-Encrypted: i=1; AFNElJ/Jj1vMWskkmu8SexUlfO9DSUlBA1uItT3z7vVUkYdX2YtPVdN0KJ36PjUF08D6YuI6+uPu3NUHjcAJ@lists.postgresql.org X-Gm-Message-State: AOJu0YxXQgceATFQ2vHlqFnRs7jmJ1gcfmvS90Ib1S+nelBY2qYt+HHZ uh8XRfJI5AfggFO4NS1DNswS/bPW0HGSj3tZJ7b7BEfciz1bRxadozy+3pFOv5E4LxR9IQjPDGN OGiRefjCANdaNRJGhXNsmkmmIV+u9E80= X-Gm-Gg: AeBDiesrrK5ulII3b0I3SHdSoDTQzw72NhY+mn0kTm+iKAXF44EjNQfqpZsoiTm9PVV 6jrNBD2dQ/oojcY4I3ltxQt/teQP9bdSI6Yp2NxCpwV0VAEHUjytXDD67LBPbLB6WyCXO79h6M+ dzj+bQnZD6qnRGpbzF3rbK4CPs4p4+mVU8VJTB15x6EFwJmXZ2d07w4+JDoyW/cwhsHVO5jxM6w TUCemBkehWXQxj6/+ChHAfYjXHFpRy0K+UwKjGNmTGUocDK9zQdCCAPfcGjF8LN1Ju+z9w9asRI 71wV4utfhDQaXJU= X-Received: by 2002:a17:907:7243:b0:ba6:a05c:ac25 with SMTP id a640c23a62f3a-bb7f9eb8922mr113071666b.2.1777407566681; Tue, 28 Apr 2026 13:19:26 -0700 (PDT) MIME-Version: 1.0 References: <19458-a69c98bc498333ba@postgresql.org> In-Reply-To: From: Nikita Malakhov Date: Tue, 28 Apr 2026 23:19:15 +0300 X-Gm-Features: AVHnY4KhhVoWl142ZyW4dv4f0sKSMgZlWtU2SWLvWNxBb1VUKpUC0nDXbjBnVkw Message-ID: Subject: Re: BUG #19458: OOM killer in jsonb_path_exists_opr (@?) with malformed JSONPath containing non-existent variables To: Andrey Borodin , Amit Langote Cc: Andrey Rachitskiy , pgsql-bugs@lists.postgresql.org, dhyan@nataraj.su Content-Type: multipart/alternative; boundary="000000000000a21bdd06508af2f7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a21bdd06508af2f7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi! According to the Jsonpath standard, malformed expression should return an error, but not all cases of malformation are thoroughly described. When this functionality was developed (Jsonpath and SQL/JSON) the absence of the variable was considered as malformation and was decided to throw an error in threads long time ago. In case this behavior to be a subject for change it surely should not be backported, but the error-throwing code has to. I agree that sometimes Json (-path) functionality provides very little info on errors, and it could be extended. Changing this behavior is subject for Hackers and should be approved by Tom Lane, Adres Freund and other people responsible for including Jsonpath and SQL/JSON into Postgres. On Tue, Apr 28, 2026 at 7:51=E2=80=AFPM Andrey Borodin wrote: > > > > On 20 Apr 2026, at 18:38, Andrey Rachitskiy wrote: > > > > I propose a targeted backpatch for REL_14/15/16 in jsonpath_exec.c to > align missing variable handling with newer branches and prevent > pathological memory growth on malformed/hostile jsonpath expressions. > > Hi! Thank you for the report and proposed fix. I've took a look into the > patch. > > So we can use vars like this: > > # SELECT jsonb_path_exists( > '{"x": 42}'::jsonb, > '$ ? ($"threshold" < 50)'::jsonpath, > '{"threshold": 10}'::jsonb -- HERE go vars > ); > > Operator @? is doing the same, but without supplied vars. And this thread > essentially points to buggy handling of vars: > > # SELECT j @? '$"no_such_var"' > FROM (VALUES > ('{"important": "data"}'::jsonb), > ('42'::jsonb), > ('null'::jsonb), > ('false'::jsonb) > ) AS t(j); > ?column? > ---------- > t > t > t > t > (4 rows) > > It basically says that path with value of var "no_such_var" exists > everywhere. > > I think it's a bug, but we would need a JSON Path expert here. > > 17+ throws an error, which seems suspicious to me too. @? is expected to > operate in silent mode. Perhaps, we should just return NULL instead of t. > By using RETURN_ERROR macro. But it might sound overly invasive for back > branches. > > Even if we are going to throw an error, we can give mode details. I'd > suggest > instead of "could not find jsonpath variable \"%s\"" throwing something > like > "no variables supplied to reference by variable \"%s\"" or something alon= g > those lines. > > > Besides this, the direction of the fix looks good to me. Thank you! > > > Best regards, Andrey Borodin. > > > > --=20 Regards, Nikita Malakhov Postgres Professional The Russian Postgres Company https://postgrespro.ru/ --000000000000a21bdd06508af2f7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi!

According to the J= sonpath standard, malformed expression should return an error,
bu= t not all cases of malformation are thoroughly described.

When this functionality was developed (Jsonpath and SQL/JSON) the a= bsence
of the variable was considered as malformation and was dec= ided to throw an error
in threads long time ago. In case this beh= avior to be=C2=A0a=C2=A0subject for change it=C2=A0surely
should = not be backported,=C2=A0but the error-throwing code has to.
I agr= ee that sometimes Json (-path) functionality provides very little info on e= rrors,
and it could be extended.

Changin= g this behavior is subject for Hackers and should be approved by Tom Lane,<= /div>
Adres Freund and other people responsible for including Jsonpath = and SQL/JSON
into Postgres.

On Tue, Apr 28, 2026 at 7:51=E2=80=AFPM Andrey Borodin <x4mmm@yandex-team.ru> wrote:


> On 20 Apr 2026, at 18:38, Andrey Rachitskiy <pl0h0yp1@gmail.com> wrote:
>
> I propose a targeted backpatch for REL_14/15/16 in jsonpath_exec.c to = align missing variable handling with newer branches and prevent pathologica= l memory growth on malformed/hostile jsonpath expressions.

Hi! Thank you for the report and proposed fix. I've took a look into th= e
patch.

So we can use vars like this:

# SELECT jsonb_path_exists(
'{"x": 42}'::jsonb,
'$ ? ($"threshold" < 50)'::jsonpath,
'{"threshold": 10}'::jsonb -- HERE go vars
);

Operator @? is doing the same, but without supplied vars. And this thread essentially points to buggy handling of vars:

# SELECT j @? '$"no_such_var"'
FROM (VALUES
=C2=A0 ('{"important": "data"}'::jsonb),
=C2=A0 ('42'::jsonb),
=C2=A0 ('null'::jsonb),
=C2=A0 ('false'::jsonb)
) AS t(j);
=C2=A0?column?=C2=A0
----------
=C2=A0t
=C2=A0t
=C2=A0t
=C2=A0t
(4 rows)

It basically says that path with value of var "no_such_var" exist= s everywhere.

I think it's a bug, but we would need a JSON Path expert here.

17+ throws an error, which seems suspicious to me too. @? is expected to operate in silent mode. Perhaps, we should just return NULL instead of t. By using RETURN_ERROR macro. But it might sound overly invasive for back branches.

Even if we are going to throw an error, we can give mode details. I'd s= uggest
instead of "could not find jsonpath variable \"%s\"" th= rowing something like
"no variables supplied to reference by variable \"%s\""= or something along
those lines.


Besides this, the direction of the fix looks good to me. Thank you!


Best regards, Andrey Borodin.





--
Regards,
Nikita Malakhov
Postgres Professional
The Russian Postgres Company
--000000000000a21bdd06508af2f7--