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 16:39:16 +0900
Message-ID: <20180711073916.GD14301@paquier.xyz> (raw)
In-Reply-To: <20180711062713.GA14301@paquier.xyz>
References: <CA+q6zcWzN9ztCfR47ZwgTr1KLnuO6BAY6FurxXhovP4hxr+yOQ@mail.gmail.com>
<20180711062713.GA14301@paquier.xyz>
On Wed, Jul 11, 2018 at 03:27:13PM +0900, Michael Paquier wrote:
> 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?
Hacking my way out I am finishing with the attached, which fixes the
problem and passes all regression tests. I am not completely sure that
this the right approach though, I would need to think a bit more about
it.
--
Michael
Attachments:jsonfunc-composite-mcxt.patch (text/x-diff, 1.8K) downloaddiff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.cindex e358b5ad13..b82060528b 100644--- a/src/backend/utils/adt/jsonfuncs.c+++ b/src/backend/utils/adt/jsonfuncs.c@@ -2721,24 +2721,22 @@ JsValueToJsObject(JsValue *jsv, JsObject *jso)
static void
update_cached_tupdesc(CompositeIOData *io, MemoryContext mcxt)
{
- if (!io->tupdesc ||- io->tupdesc->tdtypeid != io->base_typid ||- io->tupdesc->tdtypmod != io->base_typmod)- {- TupleDesc tupdesc = lookup_rowtype_tupdesc(io->base_typid,- io->base_typmod);- MemoryContext oldcxt;+ TupleDesc tupdesc;+ MemoryContext oldcxt;- if (io->tupdesc)- FreeTupleDesc(io->tupdesc);+ if (io->tupdesc != NULL &&+ io->tupdesc->tdtypeid == io->base_typid &&+ io->tupdesc->tdtypmod == io->base_typmod)+ return;- /* copy tuple desc without constraints into cache memory context */- oldcxt = MemoryContextSwitchTo(mcxt);- io->tupdesc = CreateTupleDescCopy(tupdesc);- MemoryContextSwitchTo(oldcxt);+ tupdesc = lookup_rowtype_tupdesc(io->base_typid, io->base_typmod);- ReleaseTupleDesc(tupdesc);- }+ /* copy tuple desc without constraints into cache memory context */+ oldcxt = MemoryContextSwitchTo(mcxt);+ io->tupdesc = CreateTupleDescCopy(tupdesc);+ MemoryContextSwitchTo(oldcxt);++ ReleaseTupleDesc(tupdesc);
}
/* recursively populate a composite (row type) value from json/jsonb */
@@ -3577,8 +3575,8 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
if (!cache)
{
fcinfo->flinfo->fn_extra = cache =
- MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt, sizeof(*cache));- cache->fn_mcxt = fcinfo->flinfo->fn_mcxt;+ MemoryContextAllocZero(rsi->econtext->ecxt_per_query_memory, sizeof(*cache));+ cache->fn_mcxt = rsi->econtext->ecxt_per_query_memory;
if (have_record_arg)
{
signature.asc (application/pgp-signature, 832B) download
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: <20180711073916.GD14301@paquier.xyz>
References: <CA+q6zcWzN9ztCfR47ZwgTr1KLnuO6BAY6FurxXhovP4hxr+yOQ@mail.gmail.com> <20180711062713.GA14301@paquier.xyz> <20180711073916.GD14301@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