From f787c61580d75574ba1263ab5da218aa08a3e6c0 Mon Sep 17 00:00:00 2001
From: liyonghao <liyonghao@highgo.com>
Date: Wed, 4 Mar 2026 10:19:54 +0800
Subject: [PATCH] Fix improper tuple deallocation in import_pg_statistic().

The import_pg_statistic() function in src/backend/statistics/stat_utils.c
was using pfree() to release a HeapTuple after heap_form_tuple(). This is
incorrect because HeapTupleData contains a nested pointer t_data that
points to separately allocated tuple header data.

Using pfree() only frees the HeapTupleData structure (typically 24-32
bytes) but leaves the actual tuple data leaked. Replace it with the
proper heap_freetuple() API which correctly frees both the structure
and its underlying data.

Author: Yonghao Lee <yonghao_lee@qq.com>
---
 src/backend/statistics/extended_stats_funcs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index 0ec77a6..9279904 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -1509,7 +1509,7 @@ import_pg_statistic(Relation pgsd, JsonbContainer *cont,
 	pgstup = heap_form_tuple(RelationGetDescr(pgsd), values, nulls);
 	pgstdat = heap_copy_tuple_as_datum(pgstup, RelationGetDescr(pgsd));
 
-	pfree(pgstup);
+	heap_freetuple(pgstup);
 
 	*pg_statistic_ok = true;
 
-- 
1.8.3.1

