diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 421afcf47d..12e2fbb2c7 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -2657,9 +2657,6 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward, return false; } - /* Ensure we copy into caller's memory context */ - MemoryContextSwitchTo(oldcontext); - /* Record abbreviated key for caller */ if (state->sortKeys->abbrev_converter && abbrev) *abbrev = stup.datum1; @@ -2676,6 +2673,8 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward, *isNull = false; } + MemoryContextSwitchTo(oldcontext); + return true; }