jsonb nesting level edge case

From: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: jsonb nesting level edge case
Date: 2018-03-18 20:42:14
Message-ID: CA+q6zcVkO7un+ssGepk5LJCdU4b_jRbropwOFNV68d9TaNHh2g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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).

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2018-03-18 20:55:48 ECPG installcheck tests fail if PGDATABASE is set
Previous Message Anton Dignös 2018-03-18 19:56:31 Re: IndexJoin memory problem using spgist and boxes