From 29186da04e425db6c014f6376b8afcd2f699dfdc Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Fri, 12 Dec 2025 16:39:06 +0800
Subject: [PATCH v1] Fix memory leak in gist_page_items()

gist_page_items() builds a StringInfo buffer for each index tuple in
order to produce a textual representation of index attributes, but the
buffer was never freed after its contents were copied into a text
Datum.

Free the StringInfo buffer after use to avoid the leak.

While here, use index_close() instead of relation_close() for symmetry
with index_open().

Author: Chao Li <lic@highgo.com>
---
 contrib/pageinspect/gistfuncs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/contrib/pageinspect/gistfuncs.c b/contrib/pageinspect/gistfuncs.c
index de3746a156b..cdb35742891 100644
--- a/contrib/pageinspect/gistfuncs.c
+++ b/contrib/pageinspect/gistfuncs.c
@@ -351,6 +351,8 @@ gist_page_items(PG_FUNCTION_ARGS)
 
 			values[4] = CStringGetTextDatum(buf.data);
 			nulls[4] = false;
+
+			pfree(buf.data);
 		}
 		else
 		{
@@ -361,7 +363,7 @@ gist_page_items(PG_FUNCTION_ARGS)
 		tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 	}
 
-	relation_close(indexRel, AccessShareLock);
+	index_close(indexRel, AccessShareLock);
 
 	return (Datum) 0;
 }
-- 
2.39.5 (Apple Git-154)

