@@ -1725,6 +1725,8 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
1725
1725
bool
1726
1726
tuplesort_skiptuples (Tuplesortstate * state ,int64 ntuples ,bool forward )
1727
1727
{
1728
+ MemoryContext oldcontext ;
1729
+
1728
1730
/*
1729
1731
* We don't actually support backwards skip yet, because no callers need
1730
1732
* it.The API is designed to allow for that later, though.
@@ -1760,18 +1762,23 @@ tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
1760
1762
* We could probably optimize these cases better, but for now it's
1761
1763
* not worth the trouble.
1762
1764
*/
1765
+ oldcontext = MemoryContextSwitchTo (state -> sortcontext );
1763
1766
while (ntuples -- > 0 )
1764
1767
{
1765
1768
SortTuple stup ;
1766
1769
bool should_free ;
1767
1770
1768
1771
if (!tuplesort_gettuple_common (state ,forward ,
1769
1772
& stup ,& should_free ))
1773
+ {
1774
+ MemoryContextSwitchTo (oldcontext );
1770
1775
return false;
1771
- if (should_free )
1776
+ }
1777
+ if (should_free && stup .tuple )
1772
1778
pfree (stup .tuple );
1773
1779
CHECK_FOR_INTERRUPTS ();
1774
1780
}
1781
+ MemoryContextSwitchTo (oldcontext );
1775
1782
return true;
1776
1783
1777
1784
default :