Thread: jsonb nesting level edge case

jsonb nesting level edge case

From
Dmitry Dolgov
Date:
Hi,

I've just realized, that looks like there is one edge-case in the current jsonb
implementation, that can be quite confusing, and I couldn't find any related
discussion about it. From what I see there is no limit for how many nested
levels can be in a jsonb field, and e.g. when a jsonb is created from a string
it basically means that we're limited only by `check_stack_depth` (in the
following case it's about recursive `parse_object`/`parse_object_field`). So
you can create a jsonb with quite many nesting levels:

    =# insert into test_jsonb values((
(select '{' || string_agg('"field' || s.id || '"', ': {')
from generate_series(1, 10000) as s(id))
|| ': "some_value"' ||
(select string_agg('}', '') from generate_series(1, 10000)))::jsonb);

    INSERT 0 1
    Time: 29.129 ms

But at the same time `jsonb_set` apparently has a different recursion schema,
and reaches max_stack_depth faster (in this case it's about recursive
`setPath`/`setPathObject`). It means that you can create a document, but you
can't update its value using function, that was specified for that (so you
probably need to override the entire jsonb to actually update something):

    =# update test_jsonb set data = jsonb_set(data,
        (select array_agg('field' || s.id) from generate_series(1,
10000) as s(id)),
        '"other_value"');

    ERROR:  54001: stack depth limit exceeded
    HINT:  Increase the configuration parameter "max_stack_depth"
    (currently 2048kB), after ensuring the platform's stack depth limit is
    adequate.
    LOCATION:  check_stack_depth, postgres.c:3163
    Time: 17.143 ms

Is it something significant enough to worry about? Just to mention, in some
other databases there is just a limit for number of nested levels for a
document (e.g. in MongoDB Bson, MySQL binary json it's exactly 100).


Re: jsonb nesting level edge case

From
Bruce Momjian
Date:
On Sun, Mar 18, 2018 at 09:42:14PM +0100, Dmitry Dolgov wrote:
> Hi,
> 
> I've just realized, that looks like there is one edge-case in the current jsonb
> implementation, that can be quite confusing, and I couldn't find any related
> discussion about it. From what I see there is no limit for how many nested
> levels can be in a jsonb field, and e.g. when a jsonb is created from a string
> it basically means that we're limited only by `check_stack_depth` (in the
> following case it's about recursive `parse_object`/`parse_object_field`). So
> you can create a jsonb with quite many nesting levels:
> 
>     =# insert into test_jsonb values((
> (select '{' || string_agg('"field' || s.id || '"', ': {')
> from generate_series(1, 10000) as s(id))
> || ': "some_value"' ||
> (select string_agg('}', '') from generate_series(1, 10000)))::jsonb);
> 
>     INSERT 0 1
>     Time: 29.129 ms
> 
> But at the same time `jsonb_set` apparently has a different recursion schema,
> and reaches max_stack_depth faster (in this case it's about recursive
> `setPath`/`setPathObject`). It means that you can create a document, but you
> can't update its value using function, that was specified for that (so you
> probably need to override the entire jsonb to actually update something):
> 
>     =# update test_jsonb set data = jsonb_set(data,
>         (select array_agg('field' || s.id) from generate_series(1,
> 10000) as s(id)),
>         '"other_value"');
> 
>     ERROR:  54001: stack depth limit exceeded
>     HINT:  Increase the configuration parameter "max_stack_depth"
>     (currently 2048kB), after ensuring the platform's stack depth limit is
>     adequate.
>     LOCATION:  check_stack_depth, postgres.c:3163
>     Time: 17.143 ms
> 
> Is it something significant enough to worry about? Just to mention, in some
> other databases there is just a limit for number of nested levels for a
> document (e.g. in MongoDB Bson, MySQL binary json it's exactly 100).

I think our current behavior is the best we can do.  We are limited
only by configured stack memory, which people can increase.  I don't see
how we could improve it unless we reduced jsonb_set()'s stack memory
use.

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

+ As you are, so once was I.  As I am, so you will be. +
+                      Ancient Roman grave inscription +