Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd9io-0003rj-27 for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 07:39:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fd9il-0001vx-9y for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 07:39:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd9ij-0001vp-EV for pgsql-hackers@lists.postgresql.org; Wed, 11 Jul 2018 07:39:31 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd9ic-0003d0-0e for pgsql-hackers@postgresql.org; Wed, 11 Jul 2018 07:39:28 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 81E222BC; Wed, 11 Jul 2018 03:39:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 11 Jul 2018 03:39:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=wZ0hBRUgkIE+gJDhqP1wkKzZggehjJKxs6461a5cV6M=; b=T2xREBz1 XyorSFDjWw6gt1L7eZqS+iMDKc6KPnm8nLWhPF/2GbzCfFZ5JJ8nrDh5L98YF+1l 08pF87Jg8bXn1s7gXbsoy2E/eLTTVYitF3xuf4TvZHGleCKhtYeY39KxokTinL+7 Q9OLdyHY06SHyqCiigLCOXnK6qIfP+UXXCT5LGLZqpo/EGah1Sve/91GhOhp2kY4 tkrAkGINRyPHqzpd8FVeC31WQ03Xe2zJBTGGcBbk6COnJFTIq5F30FxpyaqYFk66 wZ/AcyKUCFzz44CYmCRWkU5aMLLZFhCx0eGm/5QHTFW9JG39GfyOK2J5jzSwqPM6 xwMW1DTHXQOZ0Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=wZ0hBRUgkIE+gJDhqP1wkKzZggehj JKxs6461a5cV6M=; b=GXEyFcFCuJrVC9Zog3w5UwoqEi/yKYJcyMZaxvl6XvD7b 2Tn+zuj9jIjC+Y6/2qD8r5QT1An5XN3eQSleZQ6VH+6sEZEc4pR1a7gkp8NBtOCd nl9c+6Fdx6na6LRv3sViAxvLyDXfoVEBpiBYxB90Vo8Wl4BvI13RbPn6ruyliXLu gVixaSVHGEDTdEhbQFTLVmUkHfkjx8T34PiCdC/jztBuLdHhQ2i7jAAbRtA6rw2s fOCFHvWgCn+IYJp48urj/nU8eSB5fiLlXI7IstwThmCyZZcFdrGLgU2sXa46DOLm WJ9CqrGeOEEOE1nN7zPeJYSjv8N7qznysADfelgHw== X-ME-Proxy: X-ME-Sender: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id 8C2B1E4514; Wed, 11 Jul 2018 03:39:18 -0400 (EDT) Date: Wed, 11 Jul 2018 16:39:16 +0900 From: Michael Paquier To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: PostgreSQL-development , Tom Lane Subject: Re: Problem with tupdesc in jsonb_to_recordset Message-ID: <20180711073916.GD14301@paquier.xyz> References: <20180711062713.GA14301@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="47eKBCiAZYFK5l32" Content-Disposition: inline In-Reply-To: <20180711062713.GA14301@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --47eKBCiAZYFK5l32 Content-Type: multipart/mixed; boundary="pQhZXvAqiZgbeUkD" Content-Disposition: inline --pQhZXvAqiZgbeUkD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --pQhZXvAqiZgbeUkD Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="jsonfunc-composite-mcxt.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/json= funcs.c index 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 !=3D io->base_typid || - io->tupdesc->tdtypmod !=3D io->base_typmod) - { - TupleDesc tupdesc =3D lookup_rowtype_tupdesc(io->base_typid, - io->base_typmod); - MemoryContext oldcxt; + TupleDesc tupdesc; + MemoryContext oldcxt; =20 - if (io->tupdesc) - FreeTupleDesc(io->tupdesc); + if (io->tupdesc !=3D NULL && + io->tupdesc->tdtypeid =3D=3D io->base_typid && + io->tupdesc->tdtypmod =3D=3D io->base_typmod) + return; =20 - /* copy tuple desc without constraints into cache memory context */ - oldcxt =3D MemoryContextSwitchTo(mcxt); - io->tupdesc =3D CreateTupleDescCopy(tupdesc); - MemoryContextSwitchTo(oldcxt); + tupdesc =3D lookup_rowtype_tupdesc(io->base_typid, io->base_typmod); =20 - ReleaseTupleDesc(tupdesc); - } + /* copy tuple desc without constraints into cache memory context */ + oldcxt =3D MemoryContextSwitchTo(mcxt); + io->tupdesc =3D CreateTupleDescCopy(tupdesc); + MemoryContextSwitchTo(oldcxt); + + ReleaseTupleDesc(tupdesc); } =20 /* recursively populate a composite (row type) value from json/jsonb */ @@ -3577,8 +3575,8 @@ populate_recordset_worker(FunctionCallInfo fcinfo, co= nst char *funcname, if (!cache) { fcinfo->flinfo->fn_extra =3D cache =3D - MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt, sizeof(*cache)); - cache->fn_mcxt =3D fcinfo->flinfo->fn_mcxt; + MemoryContextAllocZero(rsi->econtext->ecxt_per_query_memory, sizeof(*ca= che)); + cache->fn_mcxt =3D rsi->econtext->ecxt_per_query_memory; =20 if (have_record_arg) { --pQhZXvAqiZgbeUkD-- --47eKBCiAZYFK5l32 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAltFtCQACgkQnvQgOdby QH0VCQ/9EH9RfGu0dXbM99KjfeeuBXh6kHLLLq2GogiEZY6dZx8eFnfIAml+nF7L x2WIrRseZ4BGVkLZIxuYH/0sNnjs1sSXevfhcVDLVnS/jRasJ2GVkvjB33Avtnne Y/eIGrEanRc79tdVbZO7R1lj7xm+ZdsBk5jGUmvg8+GJzV/el6CKjQnhjqMPw1kx hPkM8i1HpjVUa0eFucgX+2NpBd37wgfJkv5ttA/qN9PMFnKcJWjqK8cF1XXQbuei fxzW6dTRMrqtMmF/HV4O3tKQ0gPhpphhhk98csLTkQVpn6BEDQaCNb4GiLCHRI0T XP3NwrlWe5eVraTUTqEO7VgLR2axjRmxv6rTBs6cPLMuAPZ3WaQx/lzT/m/ByAkI kTAeirHXKx3Igas/feWF34hAkhDDe2+v59t1dOtM4hKz3EVHCFkRRghfxB4YiwS6 J1CmVI6BYq+4dmxQiJbhaOe0oKpZV+ojWg12w8amXDZ0GglAtvaws6xA6IhRMVXq ecNBKLlCHqMhDo+ah3xD69QFcURfEVWqoPCapHLbsWkqAQTN3Hd6564lLiBsHoTh kmRjUyG6b2rsEGoHXQdDPLYlOVN4ZcTrIO9HIzFfHMiJSJPeN08ofzG4sMcUJCfp p3DJBg3a6m2DpvhkBuy9+1xOCWsWSg8UdA/wdYbzDtU4wkPD0gQ= =v142 -----END PGP SIGNATURE----- --47eKBCiAZYFK5l32--