Re: jsonb nesting level edge case

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: jsonb nesting level edge case
Date: 2018-04-03 14:40:07
Message-ID: 20180403144007.GB6472@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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(at)momjian(dot)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 +

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Magnus Hagander 2018-04-03 14:42:26 Re: [PATCH] Verify Checksums during Basebackups
Previous Message Tomas Vondra 2018-04-03 14:38:55 Re: [HACKERS] logical decoding of two-phase transactions