jsonb_set for an array with an index outside of boundaries

From: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Subject: jsonb_set for an array with an index outside of boundaries
Date: 2019-01-16 12:47:06
Message-ID: CA+q6zcUgeNJxZrc3K7LQhAS7NYOqz5+xYyDsggjrpDxVK203KQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Per discussion in [1] about generic type subscripting pathc Pavel had
interesting commentary. So far jsonb_set, if is invoked for a jsonb array with
an index outside of the array boundaries, will implicitely add a value:

=# insert into table values('["a", "b", "c"]');
=# update table set data = jsonb_set(data, '{1000}', '"d"');
=# table test;
=# table test;
data
----------------------
["a", "b", "c", "d"]

This is perfectly documented feature, there are no questions here. But for
generic type subscripting infrastructure I'm introducing another, more
convenient, syntax:

=# update table test set data['selector'] = 'value';

Since the idea behind generic type subsripting patch is just to introduce
extendable subscripting operation for different data types, here I'm relying on
already existing functionality for jsonb. But then this feature of jsonb_set
indeed became more confusing with the new syntax.

=# update table test set data[1000] = 'd';
=# table test;
data
----------------------
["a", "b", "c", "d"]

Unfortunately, the only alternative option here would be to return an error and
reject such a value, which differs from jsonb_set. I would like to ask , what
would be the best solution here - to keep this confusing behaviour, or to have
two different implementation of updating jsonb functionality (one for
jsonb_set, another for subscripting)?

[1]: https://www.postgresql.org/message-id/CA%2Bq6zcXmwR9BDrcf188Mcz5%2BjU8DaqrrOat2mzizKf-nYgDXkg%40mail.gmail.com

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2019-01-16 12:56:47 Re: [HACKERS] PATCH: multivariate histograms and MCV lists
Previous Message Dmitry Dolgov 2019-01-16 12:18:06 Re: ArchiveEntry optional arguments refactoring