pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
jsonb subscript operator returns null when key is fetched from table
3+ messages / 3 participants
[nested] [flat]

* jsonb subscript operator returns null when key is fetched from table
@ 2022-12-09 20:06 exe-dealer <exe-dealer@yandex.ru>
  2022-12-12 18:51 ` Re: jsonb subscript operator returns null when key is fetched from table David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: exe-dealer @ 2022-12-09 20:06 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

<div><div><div>jsonb subscript operator returns null when key is fetched from table.</div><div> </div></div><div>PostgreSQL 15.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit</div><div> </div><div><span style="font-family:'courier new' , monospace">WITH cte AS MATERIALIZED (SELECT 'foo' k, '{"foo":"bar"}'::jsonb j)</span></div><div><span style="font-family:'courier new' , monospace">SELECT j[k], j[k || ''] FROM cte;</span></div><div> </div><div>-- returns</div><div>-- null, "bar"</div><div> </div><div>-- expected</div><div>-- "bar", "bar"</div><div> </div></div>





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: jsonb subscript operator returns null when key is fetched from table
  2022-12-09 20:06 jsonb subscript operator returns null when key is fetched from table exe-dealer <exe-dealer@yandex.ru>
@ 2022-12-12 18:51 ` David G. Johnston <david.g.johnston@gmail.com>
  2022-12-12 19:09   ` Re: jsonb subscript operator returns null when key is fetched from table Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2022-12-12 18:51 UTC (permalink / raw)
  To: exe-dealer <exe-dealer@yandex.ru>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

On Fri, Dec 9, 2022 at 1:35 PM exe-dealer <exe-dealer@yandex.ru> wrote:

> jsonb subscript operator returns null when key is fetched from table.
>
> PostgreSQL 15.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine
> 12.2.1_git20220924-r4) 12.2.1 20220924, 64-bit
>
> WITH cte AS MATERIALIZED (SELECT 'foo' k, '{"foo":"bar"}'::jsonb j)
> SELECT j[k], j[k || ''] FROM cte;
>
> -- returns
> -- null, "bar"
>
> -- expected
> -- "bar", "bar"
>

Thanks for the report.  I can confirm that this is a problem in both v14
and v15.

To clarify the subject line - the issue manifests only if the CTE is
MATERIALIZED.

Someone more experienced than I will need to actually do the debugging work
though; hoping this reply will be seen by one of them.

David J.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: jsonb subscript operator returns null when key is fetched from table
  2022-12-09 20:06 jsonb subscript operator returns null when key is fetched from table exe-dealer <exe-dealer@yandex.ru>
  2022-12-12 18:51 ` Re: jsonb subscript operator returns null when key is fetched from table David G. Johnston <david.g.johnston@gmail.com>
@ 2022-12-12 19:09   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2022-12-12 19:09 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: exe-dealer <exe-dealer@yandex.ru>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> Someone more experienced than I will need to actually do the debugging work
> though; hoping this reply will be seen by one of them.

Meh.  Somebody was cavalier about detoasting the subscript:

            jbvp = getKeyJsonValueFromContainer(container,
                                                VARDATA(path[i]),
                                                VARSIZE(path[i]) - VARHDRSZ,
                                                NULL);

Neither VARDATA nor VARSIZE are valid to apply to an arbitrary datum.
Apparently, nobody has ever tested this with a subscript value fetched
straight from disk :-(.  The given example accidentally works with
a NOT MATERIALIZED CTE because the whole thing gets stepwise folded
to constants.  Likewise, the expression with || accidentally works
because concatenation always yields a non-toasted result.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2022-12-12 19:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09 20:06 jsonb subscript operator returns null when key is fetched from table exe-dealer <exe-dealer@yandex.ru>
2022-12-12 18:51 ` David G. Johnston <david.g.johnston@gmail.com>
2022-12-12 19:09   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox