Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioiWv-0001x1-UT for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jan 2020 06:39:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ioiWu-0003r3-Ai for pgsql-bugs@arkaria.postgresql.org; Tue, 07 Jan 2020 06:39:52 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1ioiWs-0003qk-7O for pgsql-bugs@lists.postgresql.org; Tue, 07 Jan 2020 06:39:52 +0000 Received: from [64.147.123.25] (helo=wout2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ioiWo-0006yq-1E for pgsql-bugs@lists.postgresql.org; Tue, 07 Jan 2020 06:39:49 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 433054C8; Tue, 7 Jan 2020 01:39:33 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 07 Jan 2020 01:39:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=YzxyPQY0OjwBr8f6r6CFKVL/9V6 N2Xn4PwU5ldQEXZg=; b=CAUfuUl8QYt4gSAN3dxVfjQbURj4s7j+WRYkJWTKfaP lG/Fk9SZXgOGxgJZUQTpJY+vppnNHIN3FrKBkrVg4j1B4HYF79IlYwxLLiMpQq8J 67+WFjevylDWh8eOciRZrfYPEQuqvG4uBAeuDZuQnsPb9gIOiFvvmrUqOg71LaI5 nWk6Sbbw4fFWbKhr/CkxTGg/o8rGq9+yGhxlhCI16wOhblFwB4myoWDwVPHWzLmS yeDnesBfqTnwqMl6+jONUm5U1k6AuZcGqMbPJH3wx+MtR0qBGWpo7o9iBNbG8thQ MrwEcW+1Pu/n4Q4b5fnyo9K0jgwbw/IOBFYGxeD79NQ== 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-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=YzxyPQ Y0OjwBr8f6r6CFKVL/9V6N2Xn4PwU5ldQEXZg=; b=BmmYsizQtZZBA+aP3p+UpU TNOePYTWsk7shehdgVW7dDbZ2/GkiMhq3QFH0hcid76kHdfXODXOw2qqRmAo5mY7 GUluvRVTEMDX5fQHJdMrMckJUjz64pIY1aPtCV4vWIQ6d+l3M9Jd7vS42eQm32qW qIKfrIVswnEMYbQ8R/oFE0cS6QVMs7CoMicmRBAMUosyB//7DItFiFOfwCe3g6tY nBkkKO2e8Gkxftcp6JYnDw9tGv7UVTejrl9IpR7ADuY8FTxCjmrcr+ffTEY+8ggI u2k2XZiX9tPqjasbX7290oS7ymUCZ0CTLmeF2NaZeLrOpB3iZr4YLjnp3g94OoiQ == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdehuddgleelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucfkphepudduuddruddtvddrudektddrudekheenucfrrghr rghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihiienucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id 3BA5380059; Tue, 7 Jan 2020 01:39:25 -0500 (EST) Date: Tue, 7 Jan 2020 15:39:07 +0900 From: Michael Paquier To: Tom Lane Cc: Andrey Borodin , Tomas Vondra , Adam Scott , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16122: segfault pg_detoast_datum (datum=0x0) at fmgr.c:1833 numrange query Message-ID: <20200107063907.GF2386@paquier.xyz> References: <16122-eb35bc248c806c15@postgresql.org> <20191118120312.cblcswef6vuqbijq@development> <20191119014827.GC1614@paquier.xyz> <20191119103813.xnsloisoxkwrxoim@development> <20191119114056.GA516103@paquier.xyz> <20191210053815.GI72921@paquier.xyz> <837E8CEE-FFF3-497C-B7C2-37BB42FAF2C5@yandex-team.ru> <5794.1578342514@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="L1c6L/cjZjI9d0Eq" Content-Disposition: inline In-Reply-To: <5794.1578342514@sss.pgh.pa.us> 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 --L1c6L/cjZjI9d0Eq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Jan 06, 2020 at 03:28:34PM -0500, Tom Lane wrote: > I believe that Michael's draft fix is actually about right, because > we don't want to treat the key space above the histogram upper bound > as being an actual bin: we should assume that there are no values there. > So just starting the loop at the last real bin is sufficient. However, > I'd prefer to handle the two edge cases (i.e., probe value below histogram > lower bound or above histogram upper bound) explicitly, because that makes > it more apparent what's going on. Actually, I suspect that the original error comes from a copy-pasto =66rom calc_hist_selectivity_contains to calc_hist_selectivity_contained when the code has been originally written. > There are a couple of other ways in which this code seems insufficiently > paranoid to me: >=20 > * It seems worth spending a couple lines of code at the beginning to > verify that the histogram has at least one bin, as it already did for > the range-length histogram. Otherwise we've got various divide-by-zero > hazards here, along with a need for extra tests in the looping functions. Fine by me. > * I think we'd better guard against NaNs coming back from the range > subdiff function, as well as the possibility of getting a NaN from > the division in get_position (compare [1]). Good idea. > * I am not quite totally sure about this part, but it seems to me > that calc_hist_selectivity_contains probably ought to handle the > range-bound cases the same as calc_hist_selectivity_contained, > even though only the latter is trying to access an unsafe array > index. Both could actually be merged, no? The assumptions behind the upper/lower bound lookups are actually just mirrors of each other based on which side of the operators <@ or @> the code looks at. Perhaps the current code is cleaner as done currently anyway.. > That all leads me to the attached draft patch. It lacks a test > case --- I wonder if we can find one that crashes more portably > than Michael's? And more eyeballs on calc_hist_selectivity_contains > would be good too. It would be nice to keep the test case as it crashes immediately on own Debian laptop. Anyway, if we add a test, we really need something that pokes at the bin with the histogram with the upper bound. Just something like the attached on HEAD proves that we have zero coverage for it:=20 --- a/src/backend/utils/adt/rangetypes_selfuncs.c +++ b/src/backend/utils/adt/rangetypes_selfuncs.c @@ -1050,6 +1050,7 @@ calc_hist_selectivity_contained(TypeCacheEntry *typca= che, double dist; double length_hist_frac; bool final_bin =3D false; + Assert(i =3D=3D upper_index); So that's not good from the start.. - if (bin_width <=3D 0.0) + if (isnan(bin_width) || bin_width <=3D 0.0) return 0.5; /* zero width bin */ This comment is incorrect now? + if (isnan(position)) + return 0.5; /* punt if we have any NaNs or Infs */ + This comment is incorrect as well, no? In this case both histograms have finite bounds, and you just check if that's a number. -- Michael --L1c6L/cjZjI9d0Eq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl4UJ4sACgkQnvQgOdby QH3LAg//W0gaRhontv3+ZY5cBvu8AhQfDYlSkzQ/wVhBBy7SFNb+63VbM8lQAPVW BKT9mFX0kW061K13OkPwmjx+G68pbxdPf6lbkZuPiX7mShtZtaCRDUROU8f7giRa Mewidqi4Ox8te3DKmSA///U0xdySxFyzDyuFn0VVKnR63sDS6bC4+8Z012Tgn3HE IybT3YfvIyqNq2bfKARML/j1Kr4v67TRXYnBNwrVsa10qTKepdTw4+VGIMgV9p2q INf+D8Bq1V3VRVMg6h5lB01nubq9cjgTVpAFF0nrcfhZpQMnHJlA6EKQFLP6M4YD jg7hvyiStt1yg1bDV4u+8lvhSuR6Yz3b35Fla3Vs+pSb1dqNyLqqbMYnx0P8+TEj 6L9edzQ3zyrUZmZ9ppHuSXMMELkoOUyunD4OTWG2XBY8kRaw4u61h8QnsZZkW734 /5JxnujGTMFjZ6WwTAm5JMoSIpxtBPf4KBELKTP5eg98V8jvFQcqZwDN0pW0RKAV rIYjPrJqYsUgN97TlYXABDThUAMVm/CW/eKrLYWuJk/PIyxqQvpuiBtc2t0vO+Ml 8z9ksEK0ybzZx0Ba/xTHpB8uTqWRf67mBXJNLv1ILm3VAuy7hbFUEfoNAFgodm6T 1jFo9cnKerzHmjkyWUQupoHaurlxHK0JDWRqq8xHdFXlTdbDqCU= =Vgbl -----END PGP SIGNATURE----- --L1c6L/cjZjI9d0Eq--