On 19.09.2019 22:14, Alexander Korotkov wrote:
Pushed.
Attached patch fixes premature xs_orderbynulls[] assignment. The old value of NULL flag, not the new, should be checked before pfree()ing the old value. -- Nikita Glukhov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company
>From bf4ffb0c066dd55e027ed156790277c597f44e45 Mon Sep 17 00:00:00 2001 From: Nikita Glukhov <n.glu...@postgrespro.ru> Date: Fri, 20 Sep 2019 00:07:30 +0300 Subject: [PATCH] Fix pfree()ing of index order-by distances --- src/backend/access/index/indexam.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/backend/access/index/indexam.c b/src/backend/access/index/indexam.c index 442f414..3a061e5 100644 --- a/src/backend/access/index/indexam.c +++ b/src/backend/access/index/indexam.c @@ -869,11 +869,6 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, for (i = 0; i < scan->numberOfOrderBys; i++) { - 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 @@ -881,7 +876,11 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif - if (!scan->xs_orderbynulls[i]) + 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 if (orderByTypes[i] == FLOAT4OID) @@ -892,7 +891,11 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, if (!scan->xs_orderbynulls[i]) pfree(DatumGetPointer(scan->xs_orderbyvals[i])); #endif - if (!scan->xs_orderbynulls[i]) + scan->xs_orderbynulls[i] = distances[i].isnull; + + if (scan->xs_orderbynulls[i]) + scan->xs_orderbyvals[i] = (Datum) 0; + else scan->xs_orderbyvals[i] = Float4GetDatum((float4) distances[i].value); } else @@ -908,6 +911,7 @@ index_store_float8_orderby_distances(IndexScanDesc scan, Oid *orderByTypes, if (scan->xs_recheckorderby) elog(ERROR, "ORDER BY operator must return float8 or float4 if the distance function is lossy"); scan->xs_orderbynulls[i] = true; + scan->xs_orderbyvals[i] = (Datum) 0; } } } -- 2.7.4