From cc9327b8f207c70fa35ba4bc20fac8ddbeb4eab1 Mon Sep 17 00:00:00 2001
From: "zhaotinghai.zth" <zhaotinghai.zth@alibaba-inc.com>
Date: Fri, 22 Mar 2024 16:41:31 +0800
Subject: [PATCH] patch

---
 src/backend/executor/nodeSubplan.c |  9 +++++++++
 src/backend/utils/sort/logtape.c   | 15 +++++++++++++++
 src/backend/utils/sort/tuplesort.c |  9 +++++++++
 src/include/utils/logtape.h        |  1 +
 src/include/utils/tuplesort.h      |  1 +
 5 files changed, 35 insertions(+)

diff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.c
index 45f8ef518f..fb6d575885 100644
--- a/src/backend/executor/nodeSubplan.c
+++ b/src/backend/executor/nodeSubplan.c
@@ -1147,6 +1147,15 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
 			prm->value = BoolGetDatum(true);
 			prm->isnull = false;
 			found = true;
+
+			if (IsA(planstate, AggState))
+			{
+				AggState *state = (AggState *) planstate;
+
+				tuplesort_cleanup_tape(state->sort_in);
+				tuplesort_cleanup_tape(state->sort_out);
+			}
+
 			break;
 		}
 
diff --git a/src/backend/utils/sort/logtape.c b/src/backend/utils/sort/logtape.c
index c361e47cee..180ca1892b 100644
--- a/src/backend/utils/sort/logtape.c
+++ b/src/backend/utils/sort/logtape.c
@@ -1281,3 +1281,18 @@ LogicalTapeSetBlocks(LogicalTapeSet *lts)
 #endif
 	return lts->nBlocksWritten - lts->nHoleBlocks;
 }
+
+void
+LogicalTapeFreeMem(LogicalTapeSet *lts)
+{
+	for (int i = 0; i < lts->nTapes; i++)
+	{
+		LogicalTape *lt = &lts->tapes[i];
+
+		if (lt->buffer)
+		{
+			pfree(lt->buffer);
+			lt->buffer = NULL;
+		}
+	}
+}
diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index 9f1df87438..6050efceb8 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -4606,6 +4606,15 @@ tuplesort_attach_shared(Sharedsort *shared, dsm_segment *seg)
 	SharedFileSetAttach(&shared->fileset, seg);
 }
 
+void
+tuplesort_cleanup_tape(Tuplesortstate *state)
+{
+	if (state == NULL)
+		return;
+
+	LogicalTapeFreeMem(state->tapeset);
+}
+
 /*
  * worker_get_identifier - Assign and return ordinal identifier for worker
  *
diff --git a/src/include/utils/logtape.h b/src/include/utils/logtape.h
index 85d2e03c63..e5601f1a1e 100644
--- a/src/include/utils/logtape.h
+++ b/src/include/utils/logtape.h
@@ -75,6 +75,7 @@ extern void LogicalTapeSeek(LogicalTapeSet *lts, int tapenum,
 							long blocknum, int offset);
 extern void LogicalTapeTell(LogicalTapeSet *lts, int tapenum,
 							long *blocknum, int *offset);
+extern void LogicalTapeFreeMem(LogicalTapeSet *lts);
 extern long LogicalTapeSetBlocks(LogicalTapeSet *lts);
 
 #endif							/* LOGTAPE_H */
diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h
index f94949370b..f726540d84 100644
--- a/src/include/utils/tuplesort.h
+++ b/src/include/utils/tuplesort.h
@@ -266,6 +266,7 @@ extern Size tuplesort_estimate_shared(int nworkers);
 extern void tuplesort_initialize_shared(Sharedsort *shared, int nWorkers,
 										dsm_segment *seg);
 extern void tuplesort_attach_shared(Sharedsort *shared, dsm_segment *seg);
+extern void tuplesort_cleanup_tape(Tuplesortstate *state);
 
 /*
  * These routines may only be called if randomAccess was specified 'true'.
-- 
2.39.3

