mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
Fix tuplesort memory accounting for datum sorts on byref types
6ed83d5famade it so tuplesorts could use a bump.c memory allocator for non-bounded sorts in order to save memory and increase performance. That commit didn't get the memory accounting correct for datum sorts and mistakenly passed 0 bytes to tuplesort_puttuple_common(). That was correct for byval types, but failed to take into account palloc'd memory for byref types. Fix by using datumGetSize() when using the bump allocator and by using GetMemoryChunkSpace() when not. Backpatch to v17, where6ed83d5fawas added. Author: Mario Karuza <mkaruza.pg@icloud.com> Reviewed-by: David Rowley <dgrowleyml@gmail.com> Reviewed-by: Joao Detomini <joao.detomini@enterprisedb.com> Discussion: https://postgr.es/m/e9f9f54337cd86a982837763958518cdbf08863c.camel%40icloud.com Backpatch-through: 17
This commit is contained in:
@@ -939,6 +939,7 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
|
||||
MemoryContext oldcontext = MemoryContextSwitchTo(base->tuplecontext);
|
||||
TuplesortDatumArg *arg = (TuplesortDatumArg *) base->arg;
|
||||
SortTuple stup;
|
||||
Size tuplen;
|
||||
|
||||
/*
|
||||
* Pass-by-value types or null values are just stored directly in
|
||||
@@ -961,17 +962,26 @@ tuplesort_putdatum(Tuplesortstate *state, Datum val, bool isNull)
|
||||
stup.datum1 = !isNull ? val : (Datum) 0;
|
||||
stup.isnull1 = isNull;
|
||||
stup.tuple = NULL; /* no separate storage */
|
||||
tuplen = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
stup.isnull1 = false;
|
||||
stup.datum1 = datumCopy(val, false, arg->datumTypeLen);
|
||||
stup.tuple = DatumGetPointer(stup.datum1);
|
||||
|
||||
/* GetMemoryChunkSpace is not supported for bump contexts */
|
||||
if (TupleSortUseBumpTupleCxt(base->sortopt))
|
||||
tuplen = MAXALIGN(datumGetSize(PointerGetDatum(stup.tuple),
|
||||
false,
|
||||
arg->datumTypeLen));
|
||||
else
|
||||
tuplen = GetMemoryChunkSpace(stup.tuple);
|
||||
}
|
||||
|
||||
tuplesort_puttuple_common(state, &stup,
|
||||
base->tuples &&
|
||||
base->sortKeys->abbrev_converter && !isNull, 0);
|
||||
base->sortKeys->abbrev_converter && !isNull, tuplen);
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user