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 1fd8bO-0000E0-2G for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 06:27:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fd8bM-00044r-6h for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 06:27:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd8bK-00044k-Mq for pgsql-hackers@lists.postgresql.org; Wed, 11 Jul 2018 06:27:48 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fd8bF-0003s1-3a for pgsql-hackers@postgresql.org; Wed, 11 Jul 2018 06:27:45 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 59A7A251; Wed, 11 Jul 2018 02:27:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Wed, 11 Jul 2018 02:27:18 -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=+cEmbPUQ5HfmK65XoiUgX7dEW6XPMc67uwHVgCSt/ss=; b=3MeIadW8 /tOYZ/LxbkYQUgZJzTp8sg9h9FLof5l3TEECvLzCCjpAky7/MlyaYWKsEc2CeBOs bR+iPYcIZSyYnPUUPkGoZz5NpCGp2p5qwtJCxLCoZxEhDX6rnfCJL++Rc462jSW8 RP+M1uCRGIcKNLCXHu1f+X0Yindk5dbWUm+Qm3/VxmX2QOin+RASu6aYxEVxnFAt QVfxcUxoaMDUjFX+fMeL3dOL52VCUo08gdmlYq+Sef+MyLfsPaN4aCeQxqTDQrSh uT/B6Qlay8TDAO7OK+3IDumK0pzhu0NPyYNcjS6h/aGMe3HhtpVYrW0RdGGihdS2 yHUIWB8uKCY5HQ== 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=+cEmbPUQ5HfmK65XoiUgX7dEW6XPM c67uwHVgCSt/ss=; b=roUhPLyCia3rZDpc3TUH6Zdg1LRfgTJ+HOrjZ+qQdd4op cCgv5A9a+vfRHY5o6BrsBiwgFRotWRGHAQmveNY59Kq5t48hiPiUJszxUQLw/ZVQ O4TEE5rmm9fDoRQl/tHMQqAljqv1DKu0lhuXH0szqy3+1XknMf+TQ0L6jrDvsAKY KkJKEUSWcx9WPm/WTZuFEGw+voX1ps0dDKF4ZQVZSTnnEoI8s0qvGF5x68RC0xvv KOn634ZVrzMN83C8LLJftU1H+3QhWCdqrXCDlf8p2ipg9H8l2+ZZPf8qB5nc2Fa7 w2sX3jBWH9iTLJsz7YdcWPLQIzo95w7VFjgkybIFg== 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 1B42210268; Wed, 11 Jul 2018 02:27:15 -0400 (EDT) Date: Wed, 11 Jul 2018 15:27:13 +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: <20180711062713.GA14301@paquier.xyz> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="cNdxnHkX5QqsyA0e" Content-Disposition: inline In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.25 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --cNdxnHkX5QqsyA0e Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 l= ead 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 fu= nction > cache: >=20 > if (!cache) > { > fcinfo->flinfo->fn_extra =3D cache =3D > MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt, sizeof(*cache= )); > //... >=20 > rsi->setDesc =3D cache->c.io.composite.tupdesc; >=20 > Then after the first call of populate_recordset_worker rsi->setDesc is be= ing > 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 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/js= onfuncs.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, > =20 > rsi->setResult =3D state->tuple_store; > rsi->setDesc =3D cache->c.io.composite.tupdesc; > + rsi->setDesc->tdrefcount =3D 1; > =20 > 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 --cNdxnHkX5QqsyA0e Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAltFo0EACgkQnvQgOdby QH17uw/+OnhEauJ8vvMTyOn9ish929i503l0mrz8fZWELqagFRbhDBuJLppmKCLX swcRp6R77gmfqMlkhb/nYvkMdyLcd0ci2E7W01Ay9eUKlv88IM2Vs9uDbSTT/Jlk bxQjW45mAg1JYmmaX+O+CE/64pOyXNB5GqtTo7M7znIcFVDUEC+OGyPhy3nHDfHj ELCtehyLSHFHBu8ljlggrCPNGjUzGwFjHyVk3IparMF6w37XXwQKoRGm2TvWlWyg n+wbadwTALVva5lSjB9F0HEOWRVnKpa9HFXz+sNSq0E1TacbKmHSVHPXYyAZRgae x9v+S3MjLe3mWaqrzQyXIKHqNMHAQZXYrdZkucW7SA0Sw2NX1iA3SDFDat3+DIRm lccu/nFLLkTqNKJnEYK+05GOuAx9CLrKIlYrSwFtQFKaOH3OJpX6LRR8UQSvovpo 1zIwgCJV97YggWzEI2Lbzw5MYeaprLR9zjms76yExDAOAISFP687U0kqSuvWqmN0 mTVR2jUzkjG9EiTuNi6RnwwOfbHxSk+giDSDt7c4lC6MV9DTUFhARaW6ycBnRcjm 3HuorPdepwdrRdppZV9VtmYKLK6mp6u7k+CrT6jnQTKvhP/u2rwD3ZhwvnD8dpuf XN/N0IYHy60zBJNPzbN3mJL+hu9yC/x/m9v8//y6eAkMFU/QPQ4= =4ZSV -----END PGP SIGNATURE----- --cNdxnHkX5QqsyA0e--