@@ -1841,12 +1841,12 @@ tuplesort_performsort(Tuplesortstate *state)
18411841/*
18421842 * Internal routine to fetch the next tuple in either forward or back
18431843 * direction into *stup. Returns FALSE if no more tuples.
1844- * If *should_free is set, the caller must pfree stup.tuple when done with it.
1845- * Otherwise, caller should not use tuple following next call here.
1844+ * Returned tuple belongs to tuplesort memory context, and must not be freed
1845+ * by caller. Caller should not use tuple following next call here.
18461846 */
18471847static bool
18481848tuplesort_gettuple_common (Tuplesortstate * state , bool forward ,
1849- SortTuple * stup , bool * should_free )
1849+ SortTuple * stup )
18501850{
18511851 unsigned int tuplen ;
18521852
@@ -1855,7 +1855,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
18551855 case TSS_SORTEDINMEM :
18561856 Assert (forward || state -> randomAccess );
18571857 Assert (!state -> slabAllocatorUsed );
1858- * should_free = false;
18591858 if (forward )
18601859 {
18611860 if (state -> current < state -> memtupcount )
@@ -1927,7 +1926,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
19271926 */
19281927 state -> lastReturnedTuple = stup -> tuple ;
19291928
1930- * should_free = false;
19311929 return true;
19321930 }
19331931 else
@@ -2008,14 +2006,12 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
20082006 */
20092007 state -> lastReturnedTuple = stup -> tuple ;
20102008
2011- * should_free = false;
20122009 return true;
20132010
20142011 case TSS_FINALMERGE :
20152012 Assert (forward );
20162013 /* We are managing memory ourselves, with the slab allocator. */
20172014 Assert (state -> slabAllocatorUsed );
2018- * should_free = false;
20192015
20202016 /*
20212017 * The slab slot holding the tuple that we returned in previous
@@ -2097,9 +2093,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
20972093{
20982094 MemoryContext oldcontext = MemoryContextSwitchTo (state -> sortcontext );
20992095 SortTuple stup ;
2100- bool should_free ;
21012096
2102- if (!tuplesort_gettuple_common (state , forward , & stup , & should_free ))
2097+ if (!tuplesort_gettuple_common (state , forward , & stup ))
21032098 stup .tuple = NULL ;
21042099
21052100 MemoryContextSwitchTo (oldcontext );
@@ -2110,12 +2105,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
21102105 if (state -> sortKeys -> abbrev_converter && abbrev )
21112106 * abbrev = stup .datum1 ;
21122107
2113- if (!should_free )
2114- {
2115- stup .tuple = heap_copy_minimal_tuple ((MinimalTuple ) stup .tuple );
2116- should_free = true;
2117- }
2118- ExecStoreMinimalTuple ((MinimalTuple ) stup .tuple , slot , should_free );
2108+ stup .tuple = heap_copy_minimal_tuple ((MinimalTuple ) stup .tuple );
2109+ ExecStoreMinimalTuple ((MinimalTuple ) stup .tuple , slot , true);
21192110 return true;
21202111 }
21212112 else
@@ -2127,18 +2118,17 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
21272118
21282119/*
21292120 * Fetch the next tuple in either forward or back direction.
2130- * Returns NULL if no more tuples. If *should_free is set, the
2131- * caller must pfree the returned tuple when done with it.
2132- * If it is not set, caller should not use tuple following next
2133- * call here.
2121+ * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
2122+ * context, and must not be freed by caller. Caller should not use tuple
2123+ * following next call here.
21342124 */
21352125HeapTuple
2136- tuplesort_getheaptuple (Tuplesortstate * state , bool forward , bool * should_free )
2126+ tuplesort_getheaptuple (Tuplesortstate * state , bool forward )
21372127{
21382128 MemoryContext oldcontext = MemoryContextSwitchTo (state -> sortcontext );
21392129 SortTuple stup ;
21402130
2141- if (!tuplesort_gettuple_common (state , forward , & stup , should_free ))
2131+ if (!tuplesort_gettuple_common (state , forward , & stup ))
21422132 stup .tuple = NULL ;
21432133
21442134 MemoryContextSwitchTo (oldcontext );
@@ -2148,19 +2138,17 @@ tuplesort_getheaptuple(Tuplesortstate *state, bool forward, bool *should_free)
21482138
21492139/*
21502140 * Fetch the next index tuple in either forward or back direction.
2151- * Returns NULL if no more tuples. If *should_free is set, the
2152- * caller must pfree the returned tuple when done with it.
2153- * If it is not set, caller should not use tuple following next
2154- * call here.
2141+ * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
2142+ * context, and must not be freed by caller. Caller should not use tuple
2143+ * following next call here.
21552144 */
21562145IndexTuple
2157- tuplesort_getindextuple (Tuplesortstate * state , bool forward ,
2158- bool * should_free )
2146+ tuplesort_getindextuple (Tuplesortstate * state , bool forward )
21592147{
21602148 MemoryContext oldcontext = MemoryContextSwitchTo (state -> sortcontext );
21612149 SortTuple stup ;
21622150
2163- if (!tuplesort_gettuple_common (state , forward , & stup , should_free ))
2151+ if (!tuplesort_gettuple_common (state , forward , & stup ))
21642152 stup .tuple = NULL ;
21652153
21662154 MemoryContextSwitchTo (oldcontext );
@@ -2173,7 +2161,8 @@ tuplesort_getindextuple(Tuplesortstate *state, bool forward,
21732161 * Returns FALSE if no more datums.
21742162 *
21752163 * If the Datum is pass-by-ref type, the returned value is freshly palloc'd
2176- * and is now owned by the caller.
2164+ * and is now owned by the caller (this differs from similar routines for
2165+ * other types of tuplesorts).
21772166 *
21782167 * Caller may optionally be passed back abbreviated value (on TRUE return
21792168 * value) when abbreviation was used, which can be used to cheaply avoid
@@ -2188,9 +2177,8 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
21882177{
21892178 MemoryContext oldcontext = MemoryContextSwitchTo (state -> sortcontext );
21902179 SortTuple stup ;
2191- bool should_free ;
21922180
2193- if (!tuplesort_gettuple_common (state , forward , & stup , & should_free ))
2181+ if (!tuplesort_gettuple_common (state , forward , & stup ))
21942182 {
21952183 MemoryContextSwitchTo (oldcontext );
21962184 return false;
@@ -2208,11 +2196,7 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
22082196 else
22092197 {
22102198 /* use stup.tuple because stup.datum1 may be an abbreviation */
2211-
2212- if (should_free )
2213- * val = PointerGetDatum (stup .tuple );
2214- else
2215- * val = datumCopy (PointerGetDatum (stup .tuple ), false, state -> datumTypeLen );
2199+ * val = datumCopy (PointerGetDatum (stup .tuple ), false, state -> datumTypeLen );
22162200 * isNull = false;
22172201 }
22182202
@@ -2270,16 +2254,12 @@ tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
22702254 while (ntuples -- > 0 )
22712255 {
22722256 SortTuple stup ;
2273- bool should_free ;
22742257
2275- if (!tuplesort_gettuple_common (state , forward ,
2276- & stup , & should_free ))
2258+ if (!tuplesort_gettuple_common (state , forward , & stup ))
22772259 {
22782260 MemoryContextSwitchTo (oldcontext );
22792261 return false;
22802262 }
2281- if (should_free && stup .tuple )
2282- pfree (stup .tuple );
22832263 CHECK_FOR_INTERRUPTS ();
22842264 }
22852265 MemoryContextSwitchTo (oldcontext );
0 commit comments