agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Alexander Korotkov <akorotkov@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid redundant re-evaluation of JSON_TABLE nested paths
Date: Tue, 14 Jul 2026 22:31:12 +0000
Message-ID: <E1wjlei-000Ced-0J@gemulon.postgresql.org> (raw)

Avoid redundant re-evaluation of JSON_TABLE nested paths

86ab7f4c721d makes JSON_TABLE with a NESTED PATH re-ran the nested path's
jsonpath expression several times for each parent row.  That makes such
queries significantly slower as the nested arrays grew, even without a PLAN
clause.

Two leftovers from the plan/join executor rework were responsible.
JsonTablePlanScanNextRow() still reset and advanced the nested plan
itself, although JsonTablePlanNextRow() now does that; and
JsonTableResetNestedPlan() eagerly called JsonTableResetRowPattern()
(which evaluates the path) in addition to setting the reset flag that
makes JsonTablePlanNextRow() evaluate it again.  Together these caused
the nested path to be evaluated multiple times per parent row.

Reduce JsonTablePlanScanNextRow() to advancing its own row pattern
iterator, and have JsonTableResetNestedPlan() only reset the transient
scan state (so a not-yet-advanced sibling still reads as NULL) while
deferring the actual path evaluation to the reset flag.  The nested path
is now evaluated exactly once per parent row, as before the PLAN clause
feature; results are unchanged and are covered by the existing tests.

Reported-by: Thom Brown <thom@linux.com>
Discussion: https://postgr.es/m/CAA-aLv5U94KD4C%2BLhAPYcCeGvs1xBMngcS5oEkZHN9YWwXUHsA%40mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6d8fd5a88183a65e77308aff374ebc239c44c6af

Modified Files
--------------
src/backend/utils/adt/jsonpath_exec.c | 48 ++++++++---------------------------
1 file changed, 11 insertions(+), 37 deletions(-)



Message-ID: <E1wjlei-000Ced-0J@gemulon.postgresql.org>
Permalink:  ../E1wjlei-000Ced-0J@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wjlei-000Ced-0J@gemulon.postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: akorotkov@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Avoid redundant re-evaluation of JSON_TABLE nested paths
  In-Reply-To: <E1wjlei-000Ced-0J@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox