From 3a5817695a8360011864c1834f8a90ffdfc7f840 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Thu, 29 Sep 2022 11:43:00 +1300 Subject: [PATCH] Restrict Datum sort optimization to byval types only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 91e9e89dc modified nodeSort.c so that it used datum sorts when the targetlist of the outer node contained only a single column. That commit failed to recognise that the Datum returned by tuplesort_getdatum() must be pfree'd when the type is a byref type. Ronan Dunklau did originally propose the patch with that restriction, but that, probably through my own fault, got lost during further development work. Due to the timing of this report (PG15 RC1 is almost out the door), let's just restrict the datum sort optimization to apply for byval types only. We might want to look harder into making this work for byref types in PG16. Reported-by: Önder Kalacı Diagnosis-by: Tom Lane Discussion: https://postgr.es/m/CACawEhVxe0ufR26UcqtU7GYGRuubq3p6ZWPGXL4cxy_uexpAAQ@mail.gmail.com Backpatch-through: 15, where 91e9e89dc was introduced. --- src/backend/executor/nodeSort.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/nodeSort.c b/src/backend/executor/nodeSort.c index 3c28d60c3e..37ad35704b 100644 --- a/src/backend/executor/nodeSort.c +++ b/src/backend/executor/nodeSort.c @@ -220,6 +220,7 @@ SortState * ExecInitSort(Sort *node, EState *estate, int eflags) { SortState *sortstate; + TupleDesc outerTupDesc; SO1_printf("ExecInitSort: %s\n", "initializing sort node"); @@ -274,11 +275,13 @@ ExecInitSort(Sort *node, EState *estate, int eflags) ExecInitResultTupleSlotTL(&sortstate->ss.ps, &TTSOpsMinimalTuple); sortstate->ss.ps.ps_ProjInfo = NULL; + outerTupDesc = ExecGetResultType(outerPlanState(sortstate)); + /* - * We perform a Datum sort when we're sorting just a single column, + * We perform a Datum sort when we're sorting just a single byval column, * otherwise we perform a tuple sort. */ - if (ExecGetResultType(outerPlanState(sortstate))->natts == 1) + if (outerTupDesc->natts == 1 && TupleDescAttr(outerTupDesc, 0)->attbyval) sortstate->datumSort = true; else sortstate->datumSort = false;