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 1iB3k8-0007co-VY for pgsql-committers@arkaria.postgresql.org; Thu, 19 Sep 2019 21:13:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iB3k6-0006z3-Fn for pgsql-committers@arkaria.postgresql.org; Thu, 19 Sep 2019 21:13:34 +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 1iB3k6-0006yu-8h for pgsql-committers@lists.postgresql.org; Thu, 19 Sep 2019 21:13:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iB3k3-0005Fw-9C for pgsql-committers@lists.postgresql.org; Thu, 19 Sep 2019 21:13:33 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x8JLDQmg014347; Thu, 19 Sep 2019 17:13:26 -0400 From: Tom Lane To: Alexander Korotkov cc: Erik Rijkers , Alexander Korotkov , pgsql-committers Subject: Re: pgsql: Improve handling of NULLs in KNN-GiST and KNN-SP-GiST In-reply-to: References: <6d51305e1159241cabee132f7efc7eff@xs4all.nl> <2608.1568924017@sss.pgh.pa.us> Comments: In-reply-to Alexander Korotkov message dated "Thu, 19 Sep 2019 23:47:43 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14345.1568927606.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 19 Sep 2019 17:13:26 -0400 Message-ID: <14346.1568927606@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexander Korotkov writes: > Both dromedary and tern, where segfault happened, are 32-bit. Bug > seems related to USE_FLOAT8_BYVAL or something. Yeah, I was just about to write back that there's an independent problem. Look at the logic inside the loop in index_store_float8_orderby_distances: scan->xs_orderbynulls[i] =3D distances[i].isnull; if (scan->xs_orderbynulls[i]) scan->xs_orderbyvals[i] =3D (Datum) 0; if (orderByTypes[i] =3D=3D FLOAT8OID) { #ifndef USE_FLOAT8_BYVAL /* must free any old value to avoid memory leakage */ if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif if (!scan->xs_orderbynulls[i]) scan->xs_orderbyvals[i] =3D Float8GetDatum(distances[i].va= lue); } The pfree is being done way too late, as you've already stomped on both the isnull flag and the pointer value. I think the first three lines quoted above need to be moved to after the #ifndef stanza (and, hence, duplicated for the float4 case), like #ifndef USE_FLOAT8_BYVAL /* must free any old value to avoid memory leakage */ if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif scan->xs_orderbynulls[i] =3D distances[i].isnull; if (scan->xs_orderbynulls[i]) scan->xs_orderbyvals[i] =3D (Datum) 0; else scan->xs_orderbyvals[i] =3D Float8GetDatum(distances[i].va= lue); Another issue here is that the short-circuit path above (for !distances) leaks memory, in not-passbyval cases. I'd be inclined to get rid of the short circuit and just handle the case within the main loop, so really that'd be more like if (distances) { scan->xs_orderbynulls[i] =3D distances[i].isnull; if (scan->xs_orderbynulls[i]) scan->xs_orderbyvals[i] =3D (Datum) 0; else scan->xs_orderbyvals[i] =3D Float8GetDatum(distances[i= ].value); } else { scan->xs_orderbynulls[i] =3D true; scan->xs_orderbyvals[i] =3D (Datum) 0; } regards, tom lane