From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Korotkov <a.korotkov@postgrespro.ru>
Cc: Erik Rijkers <er@xs4all.nl>
Cc: Alexander Korotkov <akorotkov@postgresql.org>
Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>
Subject: Re: pgsql: Improve handling of NULLs in KNN-GiST and KNN-SP-GiST
Date: Thu, 19 Sep 2019 17:13:26 -0400
Message-ID: <14346.1568927606@sss.pgh.pa.us> (raw)
In-Reply-To: <CAPpHfdu2wcoAVAm3Ek66rP=uo_C-D84++uf1VEcbyi_caBXWCA@mail.gmail.com>
References: <E1iB1pb-00075a-Dx@gemulon.postgresql.org>
<6d51305e1159241cabee132f7efc7eff@xs4all.nl>
<2608.1568924017@sss.pgh.pa.us>
<CAPpHfdu+ZSp2rB0KOKYb971sfjGgOs20ACmna0ZMb4EiU=woTg@mail.gmail.com>
<CAPpHfduVKNxSmJ8Udgrcq+O5yDf8TCm--gdF52EXN-EZGvoBoQ@mail.gmail.com>
<CAPpHfdu2wcoAVAm3Ek66rP=uo_C-D84++uf1VEcbyi_caBXWCA@mail.gmail.com>
Alexander Korotkov <a.korotkov@postgrespro.ru> 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] = distances[i].isnull;
if (scan->xs_orderbynulls[i])
scan->xs_orderbyvals[i] = (Datum) 0;
if (orderByTypes[i] == 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] = Float8GetDatum(distances[i].value);
}
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] = distances[i].isnull;
if (scan->xs_orderbynulls[i])
scan->xs_orderbyvals[i] = (Datum) 0;
else
scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value);
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] = distances[i].isnull;
if (scan->xs_orderbynulls[i])
scan->xs_orderbyvals[i] = (Datum) 0;
else
scan->xs_orderbyvals[i] = Float8GetDatum(distances[i].value);
}
else
{
scan->xs_orderbynulls[i] = true;
scan->xs_orderbyvals[i] = (Datum) 0;
}
regards, tom lane
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-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us, a.korotkov@postgrespro.ru, er@xs4all.nl, akorotkov@postgresql.org
Subject: Re: pgsql: Improve handling of NULLs in KNN-GiST and KNN-SP-GiST
In-Reply-To: <14346.1568927606@sss.pgh.pa.us>
References: <E1iB1pb-00075a-Dx@gemulon.postgresql.org> <6d51305e1159241cabee132f7efc7eff@xs4all.nl> <2608.1568924017@sss.pgh.pa.us> <CAPpHfdu+ZSp2rB0KOKYb971sfjGgOs20ACmna0ZMb4EiU=woTg@mail.gmail.com> <CAPpHfduVKNxSmJ8Udgrcq+O5yDf8TCm--gdF52EXN-EZGvoBoQ@mail.gmail.com> <CAPpHfdu2wcoAVAm3Ek66rP=uo_C-D84++uf1VEcbyi_caBXWCA@mail.gmail.com> <14346.1568927606@sss.pgh.pa.us>
* 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