pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Dmitry Dolgov <9erthalion6@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Subject: Re: Problem with tupdesc in jsonb_to_recordset
Date: Wed, 11 Jul 2018 15:27:13 +0900
Message-ID: <20180711062713.GA14301@paquier.xyz> (raw)
In-Reply-To: <CA+q6zcWzN9ztCfR47ZwgTr1KLnuO6BAY6FurxXhovP4hxr+yOQ@mail.gmail.com>
References: <CA+q6zcWzN9ztCfR47ZwgTr1KLnuO6BAY6FurxXhovP4hxr+yOQ@mail.gmail.com>

On Tue, Jul 10, 2018 at 10:39:28PM +0200, Dmitry Dolgov wrote:
> I've found out that currently in some situations jsonb_to_recordset can lead to
> a crash. Minimal example that I've managed to create looks like this:

It would be better not to send the same email across multiple mailing
lists.

> After brief investigation it looks like an issue with tupdesc from the function
> cache:
> 
>     if (!cache)
>     {
>         fcinfo->flinfo->fn_extra = cache =
>             MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt, sizeof(*cache));
>     //...
> 
>     rsi->setDesc = cache->c.io.composite.tupdesc;
> 
> Then after the first call of populate_recordset_worker rsi->setDesc is being
> reset since we never increased tdrefcount and the next call will use wrong
> cache data. Apparently, it can be fixed by incrementing a tdrefcount (see the
> attached patch).

This is new as of REL_11_STABLE, so I am adding an open item.  Bisecting
the error, I have the following commit pointed out as the culprit:
commit: 37a795a60b4f4b1def11c615525ec5e0e9449e05
committer: Tom Lane <tgl@sss.pgh.pa.us>
date: Thu, 26 Oct 2017 13:47:45 -0400
Support domains over composite types.

> diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
> index e358b5a..9250646 100644
> --- a/src/backend/utils/adt/jsonfuncs.c
> +++ b/src/backend/utils/adt/jsonfuncs.c
> @@ -3728,6 +3728,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
>  
>  	rsi->setResult = state->tuple_store;
>  	rsi->setDesc = cache->c.io.composite.tupdesc;
> +	rsi->setDesc->tdrefcount = 1;
>  
>  	PG_RETURN_NULL();
>  }

I don't think that your solution is correct.  From my read of 37a795a6,
the tuple descriptor is moved from the query-lifespan memory context
(ecxt_per_query_memory) to a function-level context, which could cause
the descriptor to become busted as your test is pointing out.  Tom?
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (7+ messages)  latest in thread

Message-ID: <20180711062713.GA14301@paquier.xyz>
Permalink:  ../20180711062713.GA14301@paquier.xyz/
Also on:    postgresql.org/message-id/20180711062713.GA14301@paquier.xyz

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, 9erthalion6@gmail.com, pgsql-hackers@postgresql.org, tgl@sss.pgh.pa.us
  Subject: Re: Problem with tupdesc in jsonb_to_recordset
  In-Reply-To: <20180711062713.GA14301@paquier.xyz>
  References: <CA+q6zcWzN9ztCfR47ZwgTr1KLnuO6BAY6FurxXhovP4hxr+yOQ@mail.gmail.com> <20180711062713.GA14301@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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