Re: SQL/JSON json_table plan clause

Поиск
Список
Период
Сортировка
Искать

Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Hi Amit!

No, JSON_TABLE is ok. I've meant that in the previous approach to PLAN clause
implementation there were some wrong tests.

On Tue, Feb 4, 2025 at 6:05 AM Amit Langote <amitlangote09@gmail.com> wrote:
Hi Nikita,

On Wed, Dec 18, 2024 at 12:11 AM Nikita Malakhov <hukutoc@gmail.com> wrote:
>
> Hi hackers!
>
> This thread is further work continued in [1], where Amit Langote
> suggested starting discussion on the remaining SQL/JSON feature
> 'PLAN clause for JSON_TABLE' anew.
>
> We'd like to help with merging SQL/JSON patches into vanilla,
> and have adapted PLAN clause to recent changes in JSON_TABLE
> function.

Thanks for working on this.

> While doing this we've found that some tests with the PLAN clause
> were incorrect, along with JSON_TABLE behavior with this clause.
> We've corrected this behavior, but these corrections required reverting
> some removed and heavily refactored code, so we'd be glad for review
> and feedback on this patch.

Sorry, I don't fully understand this paragraph.  Do you mean that
there might be bugs in the existing JSON_TABLE() functionality that
was committed into v17?

--
Thanks, Amit Langote


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

Re: SQL/JSON json_table plan clause

От:
Amit Langote <amitlangote09@gmail.com>
Дата:
Hi Nikita,

I was looking at the patch you posted on Feb 3. Two high-level comments:

* The documentation additions are missing.

* It looks like the patch mixes refactoring changes with new
functionality, which makes it harder to follow what's existing vs.
new. Could you separate these changes so the new additions are easier
to review?

More specifically on the 2nd point:

-/*
- * Check if the type is "composite" for the purpose of checking whether to use
- * JSON_VALUE() or JSON_QUERY() for a given JsonTableColumn.
- */
-static bool
-isCompositeType(Oid typid)
-{
-    char        typtype = get_typtype(typid);
-
-    return typid == JSONOID ||
-        typid == JSONBOID ||
-        typid == RECORDOID ||
-        type_is_array(typid) ||
-        typtype == TYPTYPE_COMPOSITE ||
-    /* domain over one of the above? */
-        (typtype == TYPTYPE_DOMAIN &&
-         isCompositeType(getBaseType(typid)));
 }
...

Diffs like this one make me wonder whether this code actually needs to
change for PLAN clause support -- but I suspect it does not.

+
+    /**/
+    bool        cross;
+    bool        outerJoin;
+    bool        advanceNested;
+    bool        advanceRight;
+    bool        reset;

Incomplete comment.

@@ -4124,6 +4130,7 @@ JsonTableInitOpaque(TableFuncScanState *state, int natts)
      * Evaluate JSON_TABLE() PASSING arguments to be passed to the jsonpath
      * executor via JsonPathVariables.
      */
+
     if (state->passingvalexprs)
@@ -4155,15 +4162,13 @@ JsonTableInitOpaque(TableFuncScanState *state,
int natts)
     }

     cxt->colplanstates = palloc(sizeof(JsonTablePlanState *) *
-                                list_length(tf->colvalexprs));
-
+                               list_length(tf->colvalexprs));
     /*
      * Initialize plan for the root path and, recursively, also any child
      * plans that compute the NESTED paths.
      */
     cxt->rootplanstate = JsonTableInitPlan(cxt, rootplan, NULL, args,
-                                           CurrentMemoryContext);
-
+                                       CurrentMemoryContext);
     state->opaque = cxt;
 }

@@ -4201,8 +4206,9 @@ JsonTableInitPlan(JsonTableExecContext *cxt,
JsonTablePlan *plan,
     if (IsA(plan, JsonTablePathScan))
     {
         JsonTablePathScan *scan = (JsonTablePathScan *) plan;
-        int         i;
+        int     i;

Unnecessary whitespace addition/removal.

 /*
- * Transform JSON_TABLE column definition into a JsonFuncExpr
- * This turns:
+ * Transform JSON_TABLE column

Not sure why the comment was rewritten.

-JsonTableResetRowPattern(JsonTablePlanState *planstate, Datum item)
+JsonTableResetContextItem(JsonTablePlanState *planstate, Datum item)

I remember renaming ContextItem with RowPattern, but it seems this is
switching it back.

It makes it look like you're reverting to code from the old patch that
implemented the PLAN clause together with JSON_TABLE(), instead of
building on the committed code and adding just what's needed to
support PLAN clause in JsonTablePlan.  Especially, the changes to
parse_jsontable.c and jsonpath_exec.c,


Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Hi Amit!

Thank you for your feedback, I'll check it out in a couple of days and reply.

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

Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Вложения:
Hi hackers!

Thanks to Amit for reviewing the patch. I've reworked it according to Amit's
recommendations and divided it into 2 parts: part 1 contains PLAN clause
code and part 2 tests. Reworked code is much more compact, so I hope
it would not be so painful to review.

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

Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Вложения:
Hi hackers!

Patch was tested with the current master. All is good with minor changes in messages.
The 1st part contains grammar changes with JSON_TABLE transformations according
to given PLAN strategy, the 2nd contains corrected tests for JSON_TABLE and new tests
for PLAN clause.

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

Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Вложения:
Hi hackers!

I've rebased patch set onto current master and have corrected
output according to recent changes in JSON array processing.
Awaiting review.

Thanks!

--
Nikita Malakhov
Postgres Professional
The Russian Postgres Company

Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
Hi Nikita!

On Fri, May 15, 2026 at 2:53 PM Nikita Malakhov  wrote:
> Again, Alexander and Amit, thanks for the review. I've rebased the patch and made
> some changes according to Alexander's notes. I've slightly rearranged files between
> patches for it to be easier to review, so now there are 3 patches:
> v23-0001-json-table-plan-clause.patch - main code changes
> v23-0002-json-table-plan-tests.patch - test cases and out file changes
> v23-0003-json-table-plan-docs.patch - docs package
>
> <...>
> >1. IsA(planstate, JsonTableSiblingJoin) is wrong.  planstate is not a
> >node, thus IsA() can't be applied to it.  You should instead do
> >IsA(planstate->plan, JsonTableSiblingJoin).  It wasn't catched,
> >because regression tests don't exercise this branch.  So, you also
> >need to improve the coverage.
> - done;
>
> >2. get_json_table() with patch uses JSON_BEHAVIOR_EMPTY as the default
> >value for deparsing, while parsing still uses
> >JSON_BEHAVIOR_EMPTY_ARRAY.  Looks plain wrong.  I'm not sure what is
> >intention here.
> - looks like leftover from older version, changed to default behavior, so unnecessary
> emission of ERROR is omitted;
>
> >3. PLAN clause is always emitted during deparsing even if user didn't
> >specify anything.  I would prefer to skip PLAN clause in this case
> >unless there is strong reason to do the opposite (this reason must be
> >pointed if any).
> - done, this made test cases much more readable;
>
> >4. Unused typedefs in src/tools/pgindent/typedefs.list:
> >JsonTableScanState, JsonPathSpec, JsonTablePlanStateType,
> >JsonTableJoinState.
> - done;
>
> >5. Empty comment in JsonTablePlanState definition.  Pointed by Amit,
> >but not fixed.
> - done;
>
> >6. Rename passingArgs to passing_Args for no reason in parse_jsontable.c.
> - done;
>
> >7. Patch lacks documentation (also pointed by Amit)
> - done, documentation is provided in separate patch;
>
> >8. Patch could use pgindent run.
> - not done yet but would provide a newer version with it.

Thank you.  I've made following additional changes.

 1. Actually, I don't see my previous item 3 fully addressed.  We
still had PLAN clause deparsed in the case user didn't specify it.  I
see this as an undesirable behavior.  And there are at least two ways
to fix this: don't output PLAN clause if it's not the default,
remember if user specified as a special flag or even original clause.
I found first way (don't output default) more appealing as it don't
require additional struct members and we already do this in other
similar cases (NULLS FIRST/LAST, ON EMPTY/ON ERROR).  So, I did this
for PLAN clause.
 2. Simplification for JsonTablePlanNextRow(): no reason to check
(planstate->advanceNested || planstate->nested) if we already issued
break for (planstate->nested == NULL).
 3. Similar simplification for transformJsonTableNestedColumns().

------
Regards,
Alexander Korotkov
Supabase

Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
Hi!

On Sun, Jul 5, 2026 at 12:02 AM Alexander Korotkov  wrote:
> On Fri, May 15, 2026 at 2:53 PM Nikita Malakhov  wrote:
> > Again, Alexander and Amit, thanks for the review. I've rebased the patch and made
> > some changes according to Alexander's notes. I've slightly rearranged files between
> > patches for it to be easier to review, so now there are 3 patches:
> > v23-0001-json-table-plan-clause.patch - main code changes
> > v23-0002-json-table-plan-tests.patch - test cases and out file changes
> > v23-0003-json-table-plan-docs.patch - docs package
> >
> > <...>
> > >1. IsA(planstate, JsonTableSiblingJoin) is wrong.  planstate is not a
> > >node, thus IsA() can't be applied to it.  You should instead do
> > >IsA(planstate->plan, JsonTableSiblingJoin).  It wasn't catched,
> > >because regression tests don't exercise this branch.  So, you also
> > >need to improve the coverage.
> > - done;
> >
> > >2. get_json_table() with patch uses JSON_BEHAVIOR_EMPTY as the default
> > >value for deparsing, while parsing still uses
> > >JSON_BEHAVIOR_EMPTY_ARRAY.  Looks plain wrong.  I'm not sure what is
> > >intention here.
> > - looks like leftover from older version, changed to default behavior, so unnecessary
> > emission of ERROR is omitted;
> >
> > >3. PLAN clause is always emitted during deparsing even if user didn't
> > >specify anything.  I would prefer to skip PLAN clause in this case
> > >unless there is strong reason to do the opposite (this reason must be
> > >pointed if any).
> > - done, this made test cases much more readable;
> >
> > >4. Unused typedefs in src/tools/pgindent/typedefs.list:
> > >JsonTableScanState, JsonPathSpec, JsonTablePlanStateType,
> > >JsonTableJoinState.
> > - done;
> >
> > >5. Empty comment in JsonTablePlanState definition.  Pointed by Amit,
> > >but not fixed.
> > - done;
> >
> > >6. Rename passingArgs to passing_Args for no reason in parse_jsontable.c.
> > - done;
> >
> > >7. Patch lacks documentation (also pointed by Amit)
> > - done, documentation is provided in separate patch;
> >
> > >8. Patch could use pgindent run.
> > - not done yet but would provide a newer version with it.
>
> Thank you.  I've made following additional changes.
>
>  1. Actually, I don't see my previous item 3 fully addressed.  We
> still had PLAN clause deparsed in the case user didn't specify it.  I
> see this as an undesirable behavior.  And there are at least two ways
> to fix this: don't output PLAN clause if it's not the default,
> remember if user specified as a special flag or even original clause.
> I found first way (don't output default) more appealing as it don't
> require additional struct members and we already do this in other
> similar cases (NULLS FIRST/LAST, ON EMPTY/ON ERROR).  So, I did this
> for PLAN clause.
>  2. Simplification for JsonTablePlanNextRow(): no reason to check
> (planstate->advanceNested || planstate->nested) if we already issued
> break for (planstate->nested == NULL).
>  3. Similar simplification for transformJsonTableNestedColumns().

I made some additional changes to the patch.  In the previous versions
PLAN qual required name for root path, but didn't required names for
nested paths.  I've rechecked with standard, and it appears that
standard claims the opposite: name is not required for the root path,
but required for nested paths.  So, I decided to allow skipping name
for both root path (as standard claims), and nested path (our
extension to standard).  Corresponding changes made to code, tests and
documentation.

Also, I merged 3 patches into 1, and edited sql_features.txt.

I'm going to push this if no objections.

------
Regards,
Alexander Korotkov
Supabase

Re: SQL/JSON json_table plan clause

От:
Nikita Malakhov <hukutoc@gmail.com>
Дата:
Hi!

Alexander, thank you for your participation. I am very sorry, I've seen your previous
email but was busy with other tasks and didn't have time to work on your notes.
Thank you very much!

On Wed, Jul 8, 2026 at 2:39 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
Hi!

On Sun, Jul 5, 2026 at 12:02 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
> On Fri, May 15, 2026 at 2:53 PM Nikita Malakhov <hukutoc@gmail.com> wrote:
> > Again, Alexander and Amit, thanks for the review. I've rebased the patch and made
> > some changes according to Alexander's notes. I've slightly rearranged files between
> > patches for it to be easier to review, so now there are 3 patches:
> > v23-0001-json-table-plan-clause.patch - main code changes
> > v23-0002-json-table-plan-tests.patch - test cases and out file changes
> > v23-0003-json-table-plan-docs.patch - docs package
> >
> > <...>
> > >1. IsA(planstate, JsonTableSiblingJoin) is wrong.  planstate is not a
> > >node, thus IsA() can't be applied to it.  You should instead do
> > >IsA(planstate->plan, JsonTableSiblingJoin).  It wasn't catched,
> > >because regression tests don't exercise this branch.  So, you also
> > >need to improve the coverage.
> > - done;
> >
> > >2. get_json_table() with patch uses JSON_BEHAVIOR_EMPTY as the default
> > >value for deparsing, while parsing still uses
> > >JSON_BEHAVIOR_EMPTY_ARRAY.  Looks plain wrong.  I'm not sure what is
> > >intention here.
> > - looks like leftover from older version, changed to default behavior, so unnecessary
> > emission of ERROR is omitted;
> >
> > >3. PLAN clause is always emitted during deparsing even if user didn't
> > >specify anything.  I would prefer to skip PLAN clause in this case
> > >unless there is strong reason to do the opposite (this reason must be
> > >pointed if any).
> > - done, this made test cases much more readable;
> >
> > >4. Unused typedefs in src/tools/pgindent/typedefs.list:
> > >JsonTableScanState, JsonPathSpec, JsonTablePlanStateType,
> > >JsonTableJoinState.
> > - done;
> >
> > >5. Empty comment in JsonTablePlanState definition.  Pointed by Amit,
> > >but not fixed.
> > - done;
> >
> > >6. Rename passingArgs to passing_Args for no reason in parse_jsontable.c.
> > - done;
> >
> > >7. Patch lacks documentation (also pointed by Amit)
> > - done, documentation is provided in separate patch;
> >
> > >8. Patch could use pgindent run.
> > - not done yet but would provide a newer version with it.
>
> Thank you.  I've made following additional changes.
>
>  1. Actually, I don't see my previous item 3 fully addressed.  We
> still had PLAN clause deparsed in the case user didn't specify it.  I
> see this as an undesirable behavior.  And there are at least two ways
> to fix this: don't output PLAN clause if it's not the default,
> remember if user specified as a special flag or even original clause.
> I found first way (don't output default) more appealing as it don't
> require additional struct members and we already do this in other
> similar cases (NULLS FIRST/LAST, ON EMPTY/ON ERROR).  So, I did this
> for PLAN clause.
>  2. Simplification for JsonTablePlanNextRow(): no reason to check
> (planstate->advanceNested || planstate->nested) if we already issued
> break for (planstate->nested == NULL).
>  3. Similar simplification for transformJsonTableNestedColumns().

I made some additional changes to the patch.  In the previous versions
PLAN qual required name for root path, but didn't required names for
nested paths.  I've rechecked with standard, and it appears that
standard claims the opposite: name is not required for the root path,
but required for nested paths.  So, I decided to allow skipping name
for both root path (as standard claims), and nested path (our
extension to standard).  Corresponding changes made to code, tests and
documentation.

Also, I merged 3 patches into 1, and edited sql_features.txt.

I'm going to push this if no objections.

------
Regards,
Alexander Korotkov
Supabase


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

Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> Alexander, thank you for your participation. I am very sorry, I've seen your previous
> email but was busy with other tasks and didn't have time to work on your notes.
> Thank you very much!

OK, no problem.  Are you good with the current shape of the patch?

------
Regards,
Alexander Korotkov
Supabase


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov  wrote:
>
> On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > email but was busy with other tasks and didn't have time to work on your notes.
> > Thank you very much!
>
> OK, no problem.  Are you good with the current shape of the patch?

I've been giving this a test drive. I saw that this has been
committed, and re-tested against that, and the issues I identified
(unless I've misunderstood some functionality) seem to have survived.

First, doesn't this need a catversion bump? This adds fields to
existing node types.


I only get the first column from the following. The second one
disappears without error:

SELECT * FROM JSON_TABLE(
  jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
  COLUMNS (
    NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
    NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
  )
  PLAN (p0 OUTER json_table_path_0)
) jt;
 x
---
 1
(1 row)


I have the following view:
CREATE VIEW v_chain AS
SELECT * FROM JSON_TABLE(
  jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
  COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
   NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
  PLAN (p0 OUTER (p1 INNER p11))
) jt;

When I dump it, I get:

CREATE VIEW public.v_chain AS
 SELECT y
   FROM JSON_TABLE(
        '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
        COLUMNS (
            NESTED PATH '$."x"[*]' AS p1
            COLUMNS (
                NESTED PATH '$."y"[*]' AS p11
                COLUMNS (
                    y integer PATH '$'
                )
            )
        )
        PLAN (p0 OUTER p1 INNER p11)
    ) jt;

The parentheses around "p1 INNER p11" aren't preserved.


Another dump issue with the following:

CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
  jsonb '{}', '$' AS p0
  COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
  ERROR ON ERROR
) jt;

This dumps as:

CREATE VIEW public.v_onempty AS
 SELECT a
   FROM JSON_TABLE(
            '{}'::jsonb, '$' AS p0
            COLUMNS (
                a integer PATH '$."nosuch"'
            ) ERROR ON ERROR
        ) jt;

It's lost "ERROR ON EMPTY".


Another example:

SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
  COLUMNS (a int PATH '$')
  ERROR ON ERROR
) jt;

This gives me:

ERROR:  invalid input syntax for type integer: "mystring"

But the documentation says that the table-level clause "does not
affect the errors that occur when evaluating columns". Am I missing
something here?

I haven't done any performance testing yet

Thom


Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
Hi, Thom!

On Thu, Jul 9, 2026 at 7:12 PM Thom Brown <thom@linux.com> wrote:
> On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov <aekorotkov@gmail.com> wrote:
> >
> > On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov <hukutoc@gmail.com> wrote:
> > > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > > email but was busy with other tasks and didn't have time to work on your notes.
> > > Thank you very much!
> >
> > OK, no problem.  Are you good with the current shape of the patch?
>
> I've been giving this a test drive. I saw that this has been
> committed, and re-tested against that, and the issues I identified
> (unless I've misunderstood some functionality) seem to have survived.

I tried my best to check if all the critics is addressed.  SQL/JSON patches
go through so many threads.  Sorry that I missed your valuable insights.

> First, doesn't this need a catversion bump? This adds fields to
> existing node types.

Thank you for noticing, catversion is bumped.

> I only get the first column from the following. The second one
> disappears without error:
>
> SELECT * FROM JSON_TABLE(
>   jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
>   COLUMNS (
>     NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
>     NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
>   )
>   PLAN (p0 OUTER json_table_path_0)
> ) jt;
>  x
> ---
>  1
> (1 row)

Yes, there is a bug in assignment of generated names.  Fixed in 0003.

> I have the following view:
> CREATE VIEW v_chain AS
> SELECT * FROM JSON_TABLE(
>   jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
>   COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
>    NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
>   PLAN (p0 OUTER (p1 INNER p11))
> ) jt;
>
> When I dump it, I get:
>
> CREATE VIEW public.v_chain AS
>  SELECT y
>    FROM JSON_TABLE(
>         '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
>         COLUMNS (
>             NESTED PATH '$."x"[*]' AS p1
>             COLUMNS (
>                 NESTED PATH '$."y"[*]' AS p11
>                 COLUMNS (
>                     y integer PATH '$'
>                 )
>             )
>         )
>         PLAN (p0 OUTER p1 INNER p11)
>     ) jt;
>
> The parentheses around "p1 INNER p11" aren't preserved.

Yes, there is a bug in the deparsing.  0002 fixes that.

> Another dump issue with the following:
>
> CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
>   jsonb '{}', '$' AS p0
>   COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
>   ERROR ON ERROR
> ) jt;
>
> This dumps as:
>
> CREATE VIEW public.v_onempty AS
>  SELECT a
>    FROM JSON_TABLE(
>             '{}'::jsonb, '$' AS p0
>             COLUMNS (
>                 a integer PATH '$."nosuch"'
>             ) ERROR ON ERROR
>         ) jt;
>
> It's lost "ERROR ON EMPTY".
>
>
> Another example:
>
> SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
>   COLUMNS (a int PATH '$')
>   ERROR ON ERROR
> ) jt;
>
> This gives me:
>
> ERROR:  invalid input syntax for type integer: "mystring"
>
> But the documentation says that the table-level clause "does not
> affect the errors that occur when evaluating columns". Am I missing
> something here?

I've rechecked with SQL 2023 standard.  AFAICS, ON ERROR clause should
be propagated from table to column.  We don't propagate it, so that must be
a bug since PG 17.  But this patch shouldn't try to change it, it's out of scope.
0001 reverts attempt to change ON ERROR handling (this also fixes the case
about of invalid dumping).  I think we need to consider this subject separately.

> I haven't done any performance testing yet

I ran a basic benchmark to show where the PLAN clause actually helps
performance.  The lever is row multiplicity: the default parent/child
join is OUTER, so JSON_TABLE emits a row for every parent -- including
ones whose nested array is empty.  When the nested array is usually
empty (a common shape for real JSON), asking for INNER via the PLAN
clause prunes those parents at the source instead of generating them and
discarding them with a later WHERE.

Setup -- 300k parent items, each with a few scalar columns, only every
300th has a child array:

    SET jit = off;
    SET max_parallel_workers_per_gather = 0;

    CREATE TEMP TABLE d(js jsonb);
    INSERT INTO d
    SELECT jsonb_build_object('items', jsonb_agg(item))
    FROM (
      SELECT jsonb_build_object(
               'id', g, 'a', g+1, 'b', g+2, 'c', g+3, 'd', g+4,
               'kids', CASE WHEN g % 300 = 0
                            THEN jsonb_build_array(
                                   jsonb_build_object('v', g),
                                   jsonb_build_object('v', g+1))
                            ELSE '[]'::jsonb END) AS item
      FROM generate_series(1, 300000) g
    ) s;

A) Default plan (OUTER) + WHERE to keep only matched rows:

    SELECT count(jt.v) FROM d, JSON_TABLE(js, '$.items[*]'
      COLUMNS (id int PATH '$.id', a int PATH '$.a', b int PATH '$.b',
               c int PATH '$.c', dd int PATH '$.d',
               NESTED PATH '$.kids[*]' COLUMNS (v int PATH '$.v'))) jt
    WHERE jt.v IS NOT NULL;

B) Same result, but prune childless parents inside JSON_TABLE:

    SELECT count(jt.v) FROM d, JSON_TABLE(js, '$.items[*]' AS root
      COLUMNS (id int PATH '$.id', a int PATH '$.a', b int PATH '$.b',
               c int PATH '$.c', dd int PATH '$.d',
               NESTED PATH '$.kids[*]' AS kids COLUMNS (v int PATH '$.v'))
      PLAN (root INNER kids)) jt;

Both return the same 2000 rows.  EXPLAIN ANALYZE:

    A) OUTER + WHERE : ~1100 ms
       ->  Table Function Scan on "json_table" (rows=2000)
             Rows Removed by Filter: 299000
    B) PLAN INNER    : ~413 ms
       ->  Table Function Scan on "json_table" (rows=2000)

So ~2.7x here.  The "Rows Removed by Filter: 299000" line is the whole
story: (A) materializes 299k wide childless-parent rows (evaluating all
five column paths for each) just to throw them away, while (B) never
builds them.

To be fair, it's data-dependent – if most parents have children there's
nothing to prune and INNER has similar performance to OUTER
and the case of win is specifically OUTER => INNER pruning,
because UNION/CROSS changes the result set rather than speeding
anything up.

------
Regards,
Alexander Korotkov
Supabase

Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Thu, 9 Jul 2026 at 20:52, Alexander Korotkov  wrote:
>
> Hi, Thom!
>
> On Thu, Jul 9, 2026 at 7:12 PM Thom Brown  wrote:
> > On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov  wrote:
> > >
> > > On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> > > > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > > > email but was busy with other tasks and didn't have time to work on your notes.
> > > > Thank you very much!
> > >
> > > OK, no problem.  Are you good with the current shape of the patch?
> >
> > I've been giving this a test drive. I saw that this has been
> > committed, and re-tested against that, and the issues I identified
> > (unless I've misunderstood some functionality) seem to have survived.
>
> I tried my best to check if all the critics is addressed.  SQL/JSON patches
> go through so many threads.  Sorry that I missed your valuable insights.

You didn't miss anything from me, so nothing to be sorry about. I just
meant I retested the issues I had found against the commit to see if
they had been fixed prior to submitting them. I hadn't raised them
prior to today.

> > First, doesn't this need a catversion bump? This adds fields to
> > existing node types.
>
> Thank you for noticing, catversion is bumped.
>
> > I only get the first column from the following. The second one
> > disappears without error:
> >
> > SELECT * FROM JSON_TABLE(
> >   jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
> >   COLUMNS (
> >     NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
> >     NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
> >   )
> >   PLAN (p0 OUTER json_table_path_0)
> > ) jt;
> >  x
> > ---
> >  1
> > (1 row)
>
> Yes, there is a bug in assignment of generated names.  Fixed in 0003.
>
> > I have the following view:
> > CREATE VIEW v_chain AS
> > SELECT * FROM JSON_TABLE(
> >   jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
> >   COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
> >    NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
> >   PLAN (p0 OUTER (p1 INNER p11))
> > ) jt;
> >
> > When I dump it, I get:
> >
> > CREATE VIEW public.v_chain AS
> >  SELECT y
> >    FROM JSON_TABLE(
> >         '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
> >         COLUMNS (
> >             NESTED PATH '$."x"[*]' AS p1
> >             COLUMNS (
> >                 NESTED PATH '$."y"[*]' AS p11
> >                 COLUMNS (
> >                     y integer PATH '$'
> >                 )
> >             )
> >         )
> >         PLAN (p0 OUTER p1 INNER p11)
> >     ) jt;
> >
> > The parentheses around "p1 INNER p11" aren't preserved.
>
> Yes, there is a bug in the deparsing.  0002 fixes that.
>
> > Another dump issue with the following:
> >
> > CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
> >   jsonb '{}', '$' AS p0
> >   COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
> >   ERROR ON ERROR
> > ) jt;
> >
> > This dumps as:
> >
> > CREATE VIEW public.v_onempty AS
> >  SELECT a
> >    FROM JSON_TABLE(
> >             '{}'::jsonb, '$' AS p0
> >             COLUMNS (
> >                 a integer PATH '$."nosuch"'
> >             ) ERROR ON ERROR
> >         ) jt;
> >
> > It's lost "ERROR ON EMPTY".
> >
> >
> > Another example:
> >
> > SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
> >   COLUMNS (a int PATH '$')
> >   ERROR ON ERROR
> > ) jt;
> >
> > This gives me:
> >
> > ERROR:  invalid input syntax for type integer: "mystring"
> >
> > But the documentation says that the table-level clause "does not
> > affect the errors that occur when evaluating columns". Am I missing
> > something here?
>
> I've rechecked with SQL 2023 standard.  AFAICS, ON ERROR clause should
> be propagated from table to column.  We don't propagate it, so that must be
> a bug since PG 17.  But this patch shouldn't try to change it, it's out of scope.
> 0001 reverts attempt to change ON ERROR handling (this also fixes the case
> about of invalid dumpinged).  I think we need to consider this subject separately.

Yeah, I haven't exercised JSON_TABLE enough since release, so I'm
clearly late in hitting this.

Thanks for the fixes. I'll try to get round to testing them tomorrow
unless someone beats me to it.

Thom


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
Вложения:
On Thu, 9 Jul 2026 at 21:28, Thom Brown  wrote:
>
> On Thu, 9 Jul 2026 at 20:52, Alexander Korotkov  wrote:
> >
> > Hi, Thom!
> >
> > On Thu, Jul 9, 2026 at 7:12 PM Thom Brown  wrote:
> > > On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov  wrote:
> > > >
> > > > On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> > > > > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > > > > email but was busy with other tasks and didn't have time to work on your notes.
> > > > > Thank you very much!
> > > >
> > > > OK, no problem.  Are you good with the current shape of the patch?
> > >
> > > I've been giving this a test drive. I saw that this has been
> > > committed, and re-tested against that, and the issues I identified
> > > (unless I've misunderstood some functionality) seem to have survived.
> >
> > I tried my best to check if all the critics is addressed.  SQL/JSON patches
> > go through so many threads.  Sorry that I missed your valuable insights.
>
> You didn't miss anything from me, so nothing to be sorry about. I just
> meant I retested the issues I had found against the commit to see if
> they had been fixed prior to submitting them. I hadn't raised them
> prior to today.
>
> > > First, doesn't this need a catversion bump? This adds fields to
> > > existing node types.
> >
> > Thank you for noticing, catversion is bumped.
> >
> > > I only get the first column from the following. The second one
> > > disappears without error:
> > >
> > > SELECT * FROM JSON_TABLE(
> > >   jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
> > >   COLUMNS (
> > >     NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
> > >     NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
> > >   )
> > >   PLAN (p0 OUTER json_table_path_0)
> > > ) jt;
> > >  x
> > > ---
> > >  1
> > > (1 row)
> >
> > Yes, there is a bug in assignment of generated names.  Fixed in 0003.
> >
> > > I have the following view:
> > > CREATE VIEW v_chain AS
> > > SELECT * FROM JSON_TABLE(
> > >   jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
> > >   COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
> > >    NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
> > >   PLAN (p0 OUTER (p1 INNER p11))
> > > ) jt;
> > >
> > > When I dump it, I get:
> > >
> > > CREATE VIEW public.v_chain AS
> > >  SELECT y
> > >    FROM JSON_TABLE(
> > >         '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
> > >         COLUMNS (
> > >             NESTED PATH '$."x"[*]' AS p1
> > >             COLUMNS (
> > >                 NESTED PATH '$."y"[*]' AS p11
> > >                 COLUMNS (
> > >                     y integer PATH '$'
> > >                 )
> > >             )
> > >         )
> > >         PLAN (p0 OUTER p1 INNER p11)
> > >     ) jt;
> > >
> > > The parentheses around "p1 INNER p11" aren't preserved.
> >
> > Yes, there is a bug in the deparsing.  0002 fixes that.
> >
> > > Another dump issue with the following:
> > >
> > > CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
> > >   jsonb '{}', '$' AS p0
> > >   COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
> > >   ERROR ON ERROR
> > > ) jt;
> > >
> > > This dumps as:
> > >
> > > CREATE VIEW public.v_onempty AS
> > >  SELECT a
> > >    FROM JSON_TABLE(
> > >             '{}'::jsonb, '$' AS p0
> > >             COLUMNS (
> > >                 a integer PATH '$."nosuch"'
> > >             ) ERROR ON ERROR
> > >         ) jt;
> > >
> > > It's lost "ERROR ON EMPTY".
> > >
> > >
> > > Another example:
> > >
> > > SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
> > >   COLUMNS (a int PATH '$')
> > >   ERROR ON ERROR
> > > ) jt;
> > >
> > > This gives me:
> > >
> > > ERROR:  invalid input syntax for type integer: "mystring"
> > >
> > > But the documentation says that the table-level clause "does not
> > > affect the errors that occur when evaluating columns". Am I missing
> > > something here?
> >
> > I've rechecked with SQL 2023 standard.  AFAICS, ON ERROR clause should
> > be propagated from table to column.  We don't propagate it, so that must be
> > a bug since PG 17.  But this patch shouldn't try to change it, it's out of scope.
> > 0001 reverts attempt to change ON ERROR handling (this also fixes the case
> > about of invalid dumpinged).  I think we need to consider this subject separately.
>
> Yeah, I haven't exercised JSON_TABLE enough since release, so I'm
> clearly late in hitting this.
>
> Thanks for the fixes. I'll try to get round to testing them tomorrow
> unless someone beats me to it.

Attached are some documentation fixes.

Thom

Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
Вложения:
On Fri, Jul 10, 2026 at 6:48 PM Thom Brown  wrote:
>
> On Thu, 9 Jul 2026 at 21:28, Thom Brown  wrote:
> >
> > On Thu, 9 Jul 2026 at 20:52, Alexander Korotkov  wrote:
> > >
> > > Hi, Thom!
> > >
> > > On Thu, Jul 9, 2026 at 7:12 PM Thom Brown  wrote:
> > > > On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov  wrote:
> > > > >
> > > > > On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> > > > > > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > > > > > email but was busy with other tasks and didn't have time to work on your notes.
> > > > > > Thank you very much!
> > > > >
> > > > > OK, no problem.  Are you good with the current shape of the patch?
> > > >
> > > > I've been giving this a test drive. I saw that this has been
> > > > committed, and re-tested against that, and the issues I identified
> > > > (unless I've misunderstood some functionality) seem to have survived.
> > >
> > > I tried my best to check if all the critics is addressed.  SQL/JSON patches
> > > go through so many threads.  Sorry that I missed your valuable insights.
> >
> > You didn't miss anything from me, so nothing to be sorry about. I just
> > meant I retested the issues I had found against the commit to see if
> > they had been fixed prior to submitting them. I hadn't raised them
> > prior to today.
> >
> > > > First, doesn't this need a catversion bump? This adds fields to
> > > > existing node types.
> > >
> > > Thank you for noticing, catversion is bumped.
> > >
> > > > I only get the first column from the following. The second one
> > > > disappears without error:
> > > >
> > > > SELECT * FROM JSON_TABLE(
> > > >   jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
> > > >   COLUMNS (
> > > >     NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
> > > >     NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
> > > >   )
> > > >   PLAN (p0 OUTER json_table_path_0)
> > > > ) jt;
> > > >  x
> > > > ---
> > > >  1
> > > > (1 row)
> > >
> > > Yes, there is a bug in assignment of generated names.  Fixed in 0003.
> > >
> > > > I have the following view:
> > > > CREATE VIEW v_chain AS
> > > > SELECT * FROM JSON_TABLE(
> > > >   jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
> > > >   COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
> > > >    NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
> > > >   PLAN (p0 OUTER (p1 INNER p11))
> > > > ) jt;
> > > >
> > > > When I dump it, I get:
> > > >
> > > > CREATE VIEW public.v_chain AS
> > > >  SELECT y
> > > >    FROM JSON_TABLE(
> > > >         '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
> > > >         COLUMNS (
> > > >             NESTED PATH '$."x"[*]' AS p1
> > > >             COLUMNS (
> > > >                 NESTED PATH '$."y"[*]' AS p11
> > > >                 COLUMNS (
> > > >                     y integer PATH '$'
> > > >                 )
> > > >             )
> > > >         )
> > > >         PLAN (p0 OUTER p1 INNER p11)
> > > >     ) jt;
> > > >
> > > > The parentheses around "p1 INNER p11" aren't preserved.
> > >
> > > Yes, there is a bug in the deparsing.  0002 fixes that.
> > >
> > > > Another dump issue with the following:
> > > >
> > > > CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
> > > >   jsonb '{}', '$' AS p0
> > > >   COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
> > > >   ERROR ON ERROR
> > > > ) jt;
> > > >
> > > > This dumps as:
> > > >
> > > > CREATE VIEW public.v_onempty AS
> > > >  SELECT a
> > > >    FROM JSON_TABLE(
> > > >             '{}'::jsonb, '$' AS p0
> > > >             COLUMNS (
> > > >                 a integer PATH '$."nosuch"'
> > > >             ) ERROR ON ERROR
> > > >         ) jt;
> > > >
> > > > It's lost "ERROR ON EMPTY".
> > > >
> > > >
> > > > Another example:
> > > >
> > > > SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
> > > >   COLUMNS (a int PATH '$')
> > > >   ERROR ON ERROR
> > > > ) jt;
> > > >
> > > > This gives me:
> > > >
> > > > ERROR:  invalid input syntax for type integer: "mystring"
> > > >
> > > > But the documentation says that the table-level clause "does not
> > > > affect the errors that occur when evaluating columns". Am I missing
> > > > something here?
> > >
> > > I've rechecked with SQL 2023 standard.  AFAICS, ON ERROR clause should
> > > be propagated from table to column.  We don't propagate it, so that must be
> > > a bug since PG 17.  But this patch shouldn't try to change it, it's out of scope.
> > > 0001 reverts attempt to change ON ERROR handling (this also fixes the case
> > > about of invalid dumpinged).  I think we need to consider this subject separately.
> >
> > Yeah, I haven't exercised JSON_TABLE enough since release, so I'm
> > clearly late in hitting this.
> >
> > Thanks for the fixes. I'll try to get round to testing them tomorrow
> > unless someone beats me to it.
>
> Attached are some documentation fixes.

Good, I've added them to my patchset.  I see you moved OUTER to come
before INNER.  I made the same reordering in other places for
consistency.

------
Regards,
Alexander Korotkov
Supabase

Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Fri, 10 Jul 2026 at 17:28, Alexander Korotkov  wrote:
>
> On Fri, Jul 10, 2026 at 6:48 PM Thom Brown  wrote:
> >
> > On Thu, 9 Jul 2026 at 21:28, Thom Brown  wrote:
> > >
> > > On Thu, 9 Jul 2026 at 20:52, Alexander Korotkov  wrote:
> > > >
> > > > Hi, Thom!
> > > >
> > > > On Thu, Jul 9, 2026 at 7:12 PM Thom Brown  wrote:
> > > > > On Wed, 8 Jul 2026 at 18:04, Alexander Korotkov  wrote:
> > > > > >
> > > > > > On Wed, Jul 8, 2026 at 6:35 PM Nikita Malakhov  wrote:
> > > > > > > Alexander, thank you for your participation. I am very sorry, I've seen your previous
> > > > > > > email but was busy with other tasks and didn't have time to work on your notes.
> > > > > > > Thank you very much!
> > > > > >
> > > > > > OK, no problem.  Are you good with the current shape of the patch?
> > > > >
> > > > > I've been giving this a test drive. I saw that this has been
> > > > > committed, and re-tested against that, and the issues I identified
> > > > > (unless I've misunderstood some functionality) seem to have survived.
> > > >
> > > > I tried my best to check if all the critics is addressed.  SQL/JSON patches
> > > > go through so many threads.  Sorry that I missed your valuable insights.
> > >
> > > You didn't miss anything from me, so nothing to be sorry about. I just
> > > meant I retested the issues I had found against the commit to see if
> > > they had been fixed prior to submitting them. I hadn't raised them
> > > prior to today.
> > >
> > > > > First, doesn't this need a catversion bump? This adds fields to
> > > > > existing node types.
> > > >
> > > > Thank you for noticing, catversion is bumped.
> > > >
> > > > > I only get the first column from the following. The second one
> > > > > disappears without error:
> > > > >
> > > > > SELECT * FROM JSON_TABLE(
> > > > >   jsonb '[{"x":[1],"y":[2]}]', '$[*]' AS p0
> > > > >   COLUMNS (
> > > > >     NESTED PATH '$.x[*]' AS json_table_path_0 COLUMNS (x int PATH '$'),
> > > > >     NESTED PATH '$.y[*]' COLUMNS (y int PATH '$')
> > > > >   )
> > > > >   PLAN (p0 OUTER json_table_path_0)
> > > > > ) jt;
> > > > >  x
> > > > > ---
> > > > >  1
> > > > > (1 row)
> > > >
> > > > Yes, there is a bug in assignment of generated names.  Fixed in 0003.
> > > >
> > > > > I have the following view:
> > > > > CREATE VIEW v_chain AS
> > > > > SELECT * FROM JSON_TABLE(
> > > > >   jsonb '[{"x": [{"y": [1,2]}]}]', '$[*]' AS p0
> > > > >   COLUMNS ( NESTED PATH '$.x[*]' AS p1 COLUMNS (
> > > > >    NESTED PATH '$.y[*]' AS p11 COLUMNS ( y int PATH '$' ) ) )
> > > > >   PLAN (p0 OUTER (p1 INNER p11))
> > > > > ) jt;
> > > > >
> > > > > When I dump it, I get:
> > > > >
> > > > > CREATE VIEW public.v_chain AS
> > > > >  SELECT y
> > > > >    FROM JSON_TABLE(
> > > > >         '[{"x": [{"y": [1, 2]}]}]'::jsonb, '$[*]' AS p0
> > > > >         COLUMNS (
> > > > >             NESTED PATH '$."x"[*]' AS p1
> > > > >             COLUMNS (
> > > > >                 NESTED PATH '$."y"[*]' AS p11
> > > > >                 COLUMNS (
> > > > >                     y integer PATH '$'
> > > > >                 )
> > > > >             )
> > > > >         )
> > > > >         PLAN (p0 OUTER p1 INNER p11)
> > > > >     ) jt;
> > > > >
> > > > > The parentheses around "p1 INNER p11" aren't preserved.
> > > >
> > > > Yes, there is a bug in the deparsing.  0002 fixes that.
> > > >
> > > > > Another dump issue with the following:
> > > > >
> > > > > CREATE VIEW v_onempty AS SELECT * FROM JSON_TABLE(
> > > > >   jsonb '{}', '$' AS p0
> > > > >   COLUMNS ( a int PATH '$.nosuch' ERROR ON EMPTY )
> > > > >   ERROR ON ERROR
> > > > > ) jt;
> > > > >
> > > > > This dumps as:
> > > > >
> > > > > CREATE VIEW public.v_onempty AS
> > > > >  SELECT a
> > > > >    FROM JSON_TABLE(
> > > > >             '{}'::jsonb, '$' AS p0
> > > > >             COLUMNS (
> > > > >                 a integer PATH '$."nosuch"'
> > > > >             ) ERROR ON ERROR
> > > > >         ) jt;
> > > > >
> > > > > It's lost "ERROR ON EMPTY".
> > > > >
> > > > >
> > > > > Another example:
> > > > >
> > > > > SELECT * FROM JSON_TABLE(jsonb '"mystring"', '$'
> > > > >   COLUMNS (a int PATH '$')
> > > > >   ERROR ON ERROR
> > > > > ) jt;
> > > > >
> > > > > This gives me:
> > > > >
> > > > > ERROR:  invalid input syntax for type integer: "mystring"
> > > > >
> > > > > But the documentation says that the table-level clause "does not
> > > > > affect the errors that occur when evaluating columns". Am I missing
> > > > > something here?
> > > >
> > > > I've rechecked with SQL 2023 standard.  AFAICS, ON ERROR clause should
> > > > be propagated from table to column.  We don't propagate it, so that must be
> > > > a bug since PG 17.  But this patch shouldn't try to change it, it's out of scope.
> > > > 0001 reverts attempt to change ON ERROR handling (this also fixes the case
> > > > about of invalid dumpinged).  I think we need to consider this subject separately.
> > >
> > > Yeah, I haven't exercised JSON_TABLE enough since release, so I'm
> > > clearly late in hitting this.
> > >
> > > Thanks for the fixes. I'll try to get round to testing them tomorrow
> > > unless someone beats me to it.
> >
> > Attached are some documentation fixes.
>
> Good, I've added them to my patchset.  I see you moved OUTER to come
> before INNER.  I made the same reordering in other places for
> consistency.

I've tested patches 0001-0003.

0001 - the ERROR ON ERROR query returns a NULL row again, so it now
matches the docs. ERROR ON EMPTY is now output in pg_dump
too.paragraph is accurate again.

0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
including a three-level (p1 INNER (p11 INNER p111)) chain and the
mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
dump/restore of all test views work.

0003 - the silent column-drop now correctly errors with "PLAN clause
for nested path json_table_path_1 was not found".

However, I am getting a new error:

postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
(json_table_path_0 int PATH '$')) jt;
ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...

So I think patch 0003 needs a new version because it claims "a
generated name can no longer coincide with a user-supplied one"

I also did a few performance tests. I've noticed that JSON_TABLE
queries using NESTED PATH have become noticeably slower since this
commit, even when no PLAN clause is used.

Here's what I get prior to this commit:

postgres=# \timing
Timing is on.
postgres=# CREATE TEMP TABLE doc AS
  SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
            'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
          FROM generate_series(1,1000) i) AS j;
SELECT 1
Time: 195.441 ms
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 150.463 ms
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 146.457 ms
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 147.257 ms

And post-commit:

postgres=# CREATE TEMP TABLE doc AS
  SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
            'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
          FROM generate_series(1,1000) i) AS j;
SELECT 1
postgres=# \timing
Timing is on.
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 536.018 ms
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 526.044 ms
postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
  COLUMNS (a int PATH '$.a',
     NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
 count
-------
  4000
(1 row)

Time: 582.042 ms

This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
a 3.7 x slowdown.

This seems to grow with the size of the nested arrays., With small
ones the difference is only around 10%.

Is this expected?

Regards

Thom


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Sat, 11 Jul 2026 at 13:19, Alexander Korotkov  wrote:
>
> Hi, Thom!
>
> On Fri, Jul 10, 2026 at 9:00 PM Thom Brown  wrote:
> > I've tested patches 0001-0003.
>
> Thank you for testing the patchset.
>
> > 0001 - the ERROR ON ERROR query returns a NULL row again, so it now
> > matches the docs. ERROR ON EMPTY is now output in pg_dump
> > too.paragraph is accurate again.
> >
> > 0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
> > including a three-level (p1 INNER (p11 INNER p111)) chain and the
> > mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
> > dump/restore of all test views work.
>
> Good.
>
> > 0003 - the silent column-drop now correctly errors with "PLAN clause
> > for nested path json_table_path_1 was not found".
> >
> > However, I am getting a new error:
> >
> > postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
> > (json_table_path_0 int PATH '$')) jt;
> > ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
> > LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...
> >
> > So I think patch 0003 needs a new version because it claims "a
> > generated name can no longer coincide with a user-supplied one"
>
> Thank you for catching this. 0003 is revised.
>
> > I also did a few performance tests. I've noticed that JSON_TABLE
> > queries using NESTED PATH have become noticeably slower since this
> > commit, even when no PLAN clause is used.
> >
> > Here's what I get prior to this commit:
> >
> > postgres=# \timing
> > Timing is on.
> > postgres=# CREATE TEMP TABLE doc AS
> >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> >           FROM generate_series(1,1000) i) AS j;
> > SELECT 1
> > Time: 195.441 ms
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 150.463 ms
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 146.457 ms
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 147.257 ms
> >
> > And post-commit:
> >
> > postgres=# CREATE TEMP TABLE doc AS
> >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> >           FROM generate_series(1,1000) i) AS j;
> > SELECT 1
> > postgres=# \timing
> > Timing is on.
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 536.018 ms
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 526.044 ms
> > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> >   COLUMNS (a int PATH '$.a',
> >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> >  count
> > -------
> >   4000
> > (1 row)
> >
> > Time: 582.042 ms
> >
> > This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
> > a 3.7 x slowdown.
> >
> > This seems to grow with the size of the nested arrays., With small
> > ones the is only around 10%.
> >
> > Is this expected?
>
> No, it's no expected.  There is problem of multiple evaluation of
> nested paths.  0005 fixes that.

I've tested all patches, and all reported issues now resolved. The
regression is also completely eliminated. Thanks!

Is there any use for errdetail("PATH name was %s not found in nested
columns list.") anymore?  I don't think we need it, but if for some
reason we do, the wording needs fixing.

Also, in 0001, the dumping half of the fix doesn't seem to have a
test. There's a new test for the runtime side, and json_table_view9
covers the redundant NULL ON EMPTY, but nothing would fail if a dumped
view started losing an explicit ERROR ON EMPTY again. Maybe something
like this next to the other view tests:

CREATE VIEW jsonb_table_view_onempty AS
SELECT * FROM JSON_TABLE(jsonb '{}', '$' AS p0
  COLUMNS (a int PATH '$.nosuch' ERROR ON EMPTY)
  ERROR ON ERROR);
\sv jsonb_table_view_onempty
DROP VIEW jsonb_table_view_onempty;

The \sv output should keep the ERROR ON EMPTY clause.

Regards

Thom


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Sun, 12 Jul 2026 at 18:17, Alexander Korotkov  wrote:
>
> On Sat, Jul 11, 2026 at 10:30 PM Thom Brown  wrote:
> > On Sat, 11 Jul 2026 at 13:19, Alexander Korotkov  wrote:
> > > On Fri, Jul 10, 2026 at 9:00 PM Thom Brown  wrote:
> > > > I've tested patches 0001-0003.
> > >
> > > Thank you for testing the patchset.
> > >
> > > > 0001 - the ERROR ON ERROR query returns a NULL row again, so it now
> > > > matches the docs. ERROR ON EMPTY is now output in pg_dump
> > > > too.paragraph is accurate again.
> > > >
> > > > 0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
> > > > including a three-level (p1 INNER (p11 INNER p111)) chain and the
> > > > mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
> > > > dump/restore of all test views work.
> > >
> > > Good.
> > >
> > > > 0003 - the silent column-drop now correctly errors with "PLAN clause
> > > > for nested path json_table_path_1 was not found".
> > > >
> > > > However, I am getting a new error:
> > > >
> > > > postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
> > > > (json_table_path_0 int PATH '$')) jt;
> > > > ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
> > > > LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...
> > > >
> > > > So I think patch 0003 needs a new version because it claims "a
> > > > generated name can no longer coincide with a user-supplied one"
> > >
> > > Thank you for catching this. 0003 is revised.
> > >
> > > > I also did a few performance tests. I've noticed that JSON_TABLE
> > > > queries using NESTED PATH have become noticeably slower since this
> > > > commit, even when no PLAN clause is used.
> > > >
> > > > Here's what I get prior to this commit:
> > > >
> > > > postgres=# \timing
> > > > Timing is on.
> > > > postgres=# CREATE TEMP TABLE doc AS
> > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > >           FROM generate_series(1,1000) i) AS j;
> > > > SELECT 1
> > > > Time: 195.441 ms
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 150.463 ms
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 146.457 ms
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 147.257 ms
> > > >
> > > > And post-commit:
> > > >
> > > > postgres=# CREATE TEMP TABLE doc AS
> > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > >           FROM generate_series(1,1000) i) AS j;
> > > > SELECT 1
> > > > postgres=# \timing
> > > > Timing is on.
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 536.018 ms
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 526.044 ms
> > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > >   COLUMNS (a int PATH '$.a',
> > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > >  count
> > > > -------
> > > >   4000
> > > > (1 row)
> > > >
> > > > Time: 582.042 ms
> > > >
> > > > This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
> > > > a 3.7 x slowdown.
> > > >
> > > > This seems to grow with the size of the nested arrays., With small
> > > > ones the is only around 10%.
> > > >
> > > > Is this expected?
> > >
> > > No, it's no expected.  There is problem of multiple evaluation of
> > > nested paths.  0005 fixes that.
> >
> > I've tested all patches, and all reported issues now resolved. The
> > regression is also completely eliminated. Thanks!
> >
> > Is there any use for errdetail("PATH name was %s not found in nested
> > columns list.") anymore?  I don't think we need it, but if for some
> > reason we do, the wording needs fixing.
>
> Yes, no need for this ERROR.  This is checked before, replaced with an
> assert in 0006.
>
> > Also, in 0001, the dumping half of the fix doesn't seem to have a
> > test. There's a new test for the runtime side, and json_table_view9
> > covers the redundant NULL ON EMPTY, but nothing would fail if a dumped
> > view started losing an explicit ERROR ON EMPTY again. Maybe something
> > like this next to the other view tests:
> >
> > CREATE VIEW jsonb_table_view_onempty AS
> > SELECT * FROM JSON_TABLE(jsonb '{}', '$' AS p0
> >   COLUMNS (a int PATH '$.nosuch' ERROR ON EMPTY)
> >   ERROR ON ERROR);
> > \sv jsonb_table_view_onempty
> > DROP VIEW jsonb_table_view_onempty;
> >
> > The \sv output should keep the ERROR ON EMPTY clause.
>
> New test is added to 0001.

All issues I raised are resolved by these patches, so all good from my side.

Thom


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Mon, 13 Jul 2026 at 14:30, Thom Brown  wrote:
>
> On Sun, 12 Jul 2026 at 18:17, Alexander Korotkov  wrote:
> >
> > On Sat, Jul 11, 2026 at 10:30 PM Thom Brown  wrote:
> > > On Sat, 11 Jul 2026 at 13:19, Alexander Korotkov  wrote:
> > > > On Fri, Jul 10, 2026 at 9:00 PM Thom Brown  wrote:
> > > > > I've tested patches 0001-0003.
> > > >
> > > > Thank you for testing the patchset.
> > > >
> > > > > 0001 - the ERROR ON ERROR query returns a NULL row again, so it now
> > > > > matches the docs. ERROR ON EMPTY is now output in pg_dump
> > > > > too.paragraph is accurate again.
> > > > >
> > > > > 0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
> > > > > including a three-level (p1 INNER (p11 INNER p111)) chain and the
> > > > > mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
> > > > > dump/restore of all test views work.
> > > >
> > > > Good.
> > > >
> > > > > 0003 - the silent column-drop now correctly errors with "PLAN clause
> > > > > for nested path json_table_path_1 was not found".
> > > > >
> > > > > However, I am getting a new error:
> > > > >
> > > > > postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
> > > > > (json_table_path_0 int PATH '$')) jt;
> > > > > ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
> > > > > LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...
> > > > >
> > > > > So I think patch 0003 needs a new version because it claims "a
> > > > > generated name can no longer coincide with a user-supplied one"
> > > >
> > > > Thank you for catching this. 0003 is revised.
> > > >
> > > > > I also did a few performance tests. I've noticed that JSON_TABLE
> > > > > queries using NESTED PATH have become noticeably slower since this
> > > > > commit, even when no PLAN clause is used.
> > > > >
> > > > > Here's what I get prior to this commit:
> > > > >
> > > > > postgres=# \timing
> > > > > Timing is on.
> > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > SELECT 1
> > > > > Time: 195.441 ms
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 150.463 ms
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 146.457 ms
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 147.257 ms
> > > > >
> > > > > And post-commit:
> > > > >
> > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > SELECT 1
> > > > > postgres=# \timing
> > > > > Timing is on.
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 536.018 ms
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 526.044 ms
> > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > >   COLUMNS (a int PATH '$.a',
> > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > >  count
> > > > > -------
> > > > >   4000
> > > > > (1 row)
> > > > >
> > > > > Time: 582.042 ms
> > > > >
> > > > > This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
> > > > > a 3.7 x slowdown.
> > > > >
> > > > > This seems to grow with the size of the nested arrays., With small
> > > > > ones the is only around 10%.
> > > > >
> > > > > Is this expected?
> > > >
> > > > No, it's no expected.  There is problem of multiple evaluation of
> > > > nested paths.  0005 fixes that.
> > >
> > > I've tested all patches, and all reported issues now resolved. The
> > > regression is also completely eliminated. Thanks!
> > >
> > > Is there any use for errdetail("PATH name was %s not found in nested
> > > columns list.") anymore?  I don't think we need it, but if for some
> > > reason we do, the wording needs fixing.
> >
> > Yes, no need for this ERROR.  This is checked before, replaced with an
> > assert in 0006.
> >
> > > Also, in 0001, the dumping half of the fix doesn't seem to have a
> > > test. There's a new test for the runtime side, and json_table_view9
> > > covers the redundant NULL ON EMPTY, but nothing would fail if a dumped
> > > view started losing an explicit ERROR ON EMPTY again. Maybe something
> > > like this next to the other view tests:
> > >
> > > CREATE VIEW jsonb_table_view_onempty AS
> > > SELECT * FROM JSON_TABLE(jsonb '{}', '$' AS p0
> > >   COLUMNS (a int PATH '$.nosuch' ERROR ON EMPTY)
> > >   ERROR ON ERROR);
> > > \sv jsonb_table_view_onempty
> > > DROP VIEW jsonb_table_view_onempty;
> > >
> > > The \sv output should keep the ERROR ON EMPTY clause.
> >
> > New test is added to 0001.
>
> All issues I raised are resolved by these patches, so all good from my side.

Also, shouldn't this have been marked as committed in the current CF?

Thom


Re: SQL/JSON json_table plan clause

От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Tue, Jul 14, 2026 at 6:44 PM Thom Brown  wrote:
>
> On Mon, 13 Jul 2026 at 14:30, Thom Brown  wrote:
> >
> > On Sun, 12 Jul 2026 at 18:17, Alexander Korotkov  wrote:
> > >
> > > On Sat, Jul 11, 2026 at 10:30 PM Thom Brown  wrote:
> > > > On Sat, 11 Jul 2026 at 13:19, Alexander Korotkov  wrote:
> > > > > On Fri, Jul 10, 2026 at 9:00 PM Thom Brown  wrote:
> > > > > > I've tested patches 0001-0003.
> > > > >
> > > > > Thank you for testing the patchset.
> > > > >
> > > > > > 0001 - the ERROR ON ERROR query returns a NULL row again, so it now
> > > > > > matches the docs. ERROR ON EMPTY is now output in pg_dump
> > > > > > too.paragraph is accurate again.
> > > > > >
> > > > > > 0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
> > > > > > including a three-level (p1 INNER (p11 INNER p111)) chain and the
> > > > > > mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
> > > > > > dump/restore of all test views work.
> > > > >
> > > > > Good.
> > > > >
> > > > > > 0003 - the silent column-drop now correctly errors with "PLAN clause
> > > > > > for nested path json_table_path_1 was not found".
> > > > > >
> > > > > > However, I am getting a new error:
> > > > > >
> > > > > > postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
> > > > > > (json_table_path_0 int PATH '$')) jt;
> > > > > > ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
> > > > > > LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...
> > > > > >
> > > > > > So I think patch 0003 needs a new version because it claims "a
> > > > > > generated name can no longer coincide with a user-supplied one"
> > > > >
> > > > > Thank you for catching this. 0003 is revised.
> > > > >
> > > > > > I also did a few performance tests. I've noticed that JSON_TABLE
> > > > > > queries using NESTED PATH have become noticeably slower since this
> > > > > > commit, even when no PLAN clause is used.
> > > > > >
> > > > > > Here's what I get prior to this commit:
> > > > > >
> > > > > > postgres=# \timing
> > > > > > Timing is on.
> > > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > > SELECT 1
> > > > > > Time: 195.441 ms
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 150.463 ms
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 146.457 ms
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 147.257 ms
> > > > > >
> > > > > > And post-commit:
> > > > > >
> > > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > > SELECT 1
> > > > > > postgres=# \timing
> > > > > > Timing is on.
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 536.018 ms
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 526.044 ms
> > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > >   COLUMNS (a int PATH '$.a',
> > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > >  count
> > > > > > -------
> > > > > >   4000
> > > > > > (1 row)
> > > > > >
> > > > > > Time: 582.042 ms
> > > > > >
> > > > > > This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
> > > > > > a 3.7 x slowdown.
> > > > > >
> > > > > > This seems to grow with the size of the nested arrays., With small
> > > > > > ones the is only around 10%.
> > > > > >
> > > > > > Is this expected?
> > > > >
> > > > > No, it's no expected.  There is problem of multiple evaluation of
> > > > > nested paths.  0005 fixes that.
> > > >
> > > > I've tested all patches, and all reported issues now resolved. The
> > > > regression is also completely eliminated. Thanks!
> > > >
> > > > Is there any use for errdetail("PATH name was %s not found in nested
> > > > columns list.") anymore?  I don't think we need it, but if for some
> > > > reason we do, the wording needs fixing.
> > >
> > > Yes, no need for this ERROR.  This is checked before, replaced with an
> > > assert in 0006.
> > >
> > > > Also, in 0001, the dumping half of the fix doesn't seem to have a
> > > > test. There's a new test for the runtime side, and json_table_view9
> > > > covers the redundant NULL ON EMPTY, but nothing would fail if a dumped
> > > > view started losing an explicit ERROR ON EMPTY again. Maybe something
> > > > like this next to the other view tests:
> > > >
> > > > CREATE VIEW jsonb_table_view_onempty AS
> > > > SELECT * FROM JSON_TABLE(jsonb '{}', '$' AS p0
> > > >   COLUMNS (a int PATH '$.nosuch' ERROR ON EMPTY)
> > > >   ERROR ON ERROR);
> > > > \sv jsonb_table_view_onempty
> > > > DROP VIEW jsonb_table_view_onempty;
> > > >
> > > > The \sv output should keep the ERROR ON EMPTY clause.
> > >
> > > New test is added to 0001.
> >
> > All issues I raised are resolved by these patches, so all good from my side.
>
> Also, shouldn't this have been marked as committed in the current CF?

Pushed, thank you.
And the patch is marked as committed.

------
Regards,
Alexander Korotkov
Supabase


Re: SQL/JSON json_table plan clause

От:
Thom Brown <thom@linux.com>
Дата:
On Tue, 14 Jul 2026 at 23:32, Alexander Korotkov  wrote:
>
> On Tue, Jul 14, 2026 at 6:44 PM Thom Brown  wrote:
> >
> > On Mon, 13 Jul 2026 at 14:30, Thom Brown  wrote:
> > >
> > > On Sun, 12 Jul 2026 at 18:17, Alexander Korotkov  wrote:
> > > >
> > > > On Sat, Jul 11, 2026 at 10:30 PM Thom Brown  wrote:
> > > > > On Sat, 11 Jul 2026 at 13:19, Alexander Korotkov  wrote:
> > > > > > On Fri, Jul 10, 2026 at 9:00 PM Thom Brown  wrote:
> > > > > > > I've tested patches 0001-0003.
> > > > > >
> > > > > > Thank you for testing the patchset.
> > > > > >
> > > > > > > 0001 - the ERROR ON ERROR query returns a NULL row again, so it now
> > > > > > > matches the docs. ERROR ON EMPTY is now output in pg_dump
> > > > > > > too.paragraph is accurate again.
> > > > > > >
> > > > > > > 0002 - PLAN (p0 OUTER (p1 INNER p11)) is now preserving parentheses,
> > > > > > > including a three-level (p1 INNER (p11 INNER p111)) chain and the
> > > > > > > mixed version (p0 OUTER ((p1 INNER p11) UNION p2)). Schema-only
> > > > > > > dump/restore of all test views work.
> > > > > >
> > > > > > Good.
> > > > > >
> > > > > > > 0003 - the silent column-drop now correctly errors with "PLAN clause
> > > > > > > for nested path json_table_path_1 was not found".
> > > > > > >
> > > > > > > However, I am getting a new error:
> > > > > > >
> > > > > > > postgres=# SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS
> > > > > > > (json_table_path_0 int PATH '$')) jt;
> > > > > > > ERROR:  duplicate JSON_TABLE column or path name: json_table_path_0
> > > > > > > LINE 1: SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (json_table_...
> > > > > > >
> > > > > > > So I think patch 0003 needs a new version because it claims "a
> > > > > > > generated name can no longer coincide with a user-supplied one"
> > > > > >
> > > > > > Thank you for catching this. 0003 is revised.
> > > > > >
> > > > > > > I also did a few performance tests. I've noticed that JSON_TABLE
> > > > > > > queries using NESTED PATH have become noticeably slower since this
> > > > > > > commit, even when no PLAN clause is used.
> > > > > > >
> > > > > > > Here's what I get prior to this commit:
> > > > > > >
> > > > > > > postgres=# \timing
> > > > > > > Timing is on.
> > > > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > > > SELECT 1
> > > > > > > Time: 195.441 ms
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 150.463 ms
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 146.457 ms
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 147.257 ms
> > > > > > >
> > > > > > > And post-commit:
> > > > > > >
> > > > > > > postgres=# CREATE TEMP TABLE doc AS
> > > > > > >   SELECT (SELECT jsonb_agg(jsonb_build_object('a', i,
> > > > > > >             'b', (SELECT jsonb_agg(j) FROM generate_series(1,1000) j)))
> > > > > > >           FROM generate_series(1,1000) i) AS j;
> > > > > > > SELECT 1
> > > > > > > postgres=# \timing
> > > > > > > Timing is on.
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 536.018 ms
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 526.044 ms
> > > > > > > postgres=# SELECT count(*) FROM doc, JSON_TABLE(doc.j, '$[*]'
> > > > > > >   COLUMNS (a int PATH '$.a',
> > > > > > >      NESTED PATH '$.b[*] ? (@ < 5)' COLUMNS (v int PATH '$'))) jt;
> > > > > > >  count
> > > > > > > -------
> > > > > > >   4000
> > > > > > > (1 row)
> > > > > > >
> > > > > > > Time: 582.042 ms
> > > > > > >
> > > > > > > This gives an average of 148.059 pre-commit vs.548.035ms post-commit,
> > > > > > > a 3.7 x slowdown.
> > > > > > >
> > > > > > > This seems to grow with the size of the nested arrays., With small
> > > > > > > ones the is only around 10%.
> > > > > > >
> > > > > > > Is this expected?
> > > > > >
> > > > > > No, it's no expected.  There is problem of multiple evaluation of
> > > > > > nested paths.  0005 fixes that.
> > > > >
> > > > > I've tested all patches, and all reported issues now resolved. The
> > > > > regression is also completely eliminated. Thanks!
> > > > >
> > > > > Is there any use for errdetail("PATH name was %s not found in nested
> > > > > columns list.") anymore?  I don't think we need it, but if for some
> > > > > reason we do, the wording needs fixing.
> > > >
> > > > Yes, no need for this ERROR.  This is checked before, replaced with an
> > > > assert in 0006.
> > > >
> > > > > Also, in 0001, the dumping half of the fix doesn't seem to have a
> > > > > test. There's a new test for the runtime side, and json_table_view9
> > > > > covers the redundant NULL ON EMPTY, but nothing would fail if a dumped
> > > > > view started losing an explicit ERROR ON EMPTY again. Maybe something
> > > > > like this next to the other view tests:
> > > > >
> > > > > CREATE VIEW jsonb_table_view_onempty AS
> > > > > SELECT * FROM JSON_TABLE(jsonb '{}', '$' AS p0
> > > > >   COLUMNS (a int PATH '$.nosuch' ERROR ON EMPTY)
> > > > >   ERROR ON ERROR);
> > > > > \sv jsonb_table_view_onempty
> > > > > DROP VIEW jsonb_table_view_onempty;
> > > > >
> > > > > The \sv output should keep the ERROR ON EMPTY clause.
> > > >
> > > > New test is added to 0001.
> > >
> > > All issues I raised are resolved by these patches, so all good from my side.
> >
> > Also, shouldn't this have been marked as committed in the current CF?
>
> Pushed, thank you.
> And the patch is marked as committed.

Thanks.

I've been trying to break it further in the meantime. I haven't
managed to get it to crash or return incorrect results.

But I did manage to get it to blow up in memory consumption.

postgres=# SET track_io_timing = on;

CREATE TEMP TABLE onedoc AS
SELECT jsonb_build_array(jsonb_build_object('arr', arr)) AS js

FROM (
  SELECT jsonb_agg('str_' || lpad(x::text, 20, '0')) AS arr
  FROM generate_series(1, 2500) x) a;
SET
SELECT 1
postgres=# SELECT pg_backend_pid();
 pg_backend_pid
----------------
         281748
(1 row)

postgres=# EXPLAIN (ANALYSE, IO, MEMORY, SERIALIZE BINARY, SETTINGS,
SUMMARY, VERBOSE, WAL)
SELECT count(*)
FROM JSON_TABLE((SELECT js FROM onedoc), '$[*]' AS p0
  COLUMNS (NESTED PATH '$.arr[*]' AS pa COLUMNS (a text PATH '$'),
  NESTED PATH '$.arr[*]' AS pb COLUMNS (b text PATH '$'))
  PLAN (p0 INNER (pa CROSS pb))
) jt;

                                         QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=54.06..54.09 rows=1 width=8) (actual
time=7843.279..7843.281 rows=1.00 loops=1)
   Output: count(*)
   Buffers: shared hit=23, local hit=5 written=1, temp read=45777 written=45777
   I/O Timings: local write=0.060, temp read=91.437 write=237.507
   InitPlan expr_1
     ->  Seq Scan on pg_temp.onedoc  (cost=0.00..50.80 rows=1360
width=32) (actual time=0.019..0.020 rows=1.00 loops=1)
           Output: onedoc.js
           Prefetch: avg=1.00 max=1 capacity=256
           Buffers: local hit=1
   ->  Table Function Scan on "json_table" jt  (cost=0.01..3.01
rows=100 width=0) (actual time=7164.812..7614.681 rows=6250000.00
loops=1)
         Output: jt.a, jt.b
         Table Function Call: JSON_TABLE((InitPlan expr_1).col1,
'$[*]' AS p0 COLUMNS ( NESTED PATH '$."arr"[*]' AS pa COLUMNS (a text
PATH '$'), NESTED PATH '$."arr"[*]' AS pb COLUMNS (b text PATH '$'))
PLAN (p0 INNER (pa CROSS pb)))
         Storage: Disk  Maximum Storage: 366211kB
         Buffers: shared hit=23, local hit=5 written=1, temp
read=45777 written=45777
         I/O Timings: local write=0.060, temp read=91.437 write=237.507
 Settings: work_mem = '100MB', random_page_cost = '1.1',
cpu_tuple_cost = '0.03', effective_cache_size = '6GB'
 Planning:
   Buffers: shared hit=8
   Memory: used=19kB  allocated=32kB
 Planning Time: 0.273 ms
 Serialization: time=0.009 ms  output=1kB  format=binary
 Execution Time: 7931.529 ms
(22 rows)

$ grep VmHWM /proc/281748/status
VmHWM:     7031316 kB

Thom


FAQ