From: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Date: Wed, 25 Nov 2026 19:40:00 +0500
Subject: [PATCH] Fix OOM when rendering large geometry arrays

path_encode() and circle_out() return the data pointer of a StringInfo
started by initStringInfo(). The buffer is 1024 bytes. array_out()
keeps every element string until the array text is built, so a large
point[] cast holds one such buffer per element.

Copy out the bytes actually used and free the StringInfo buffer.
point, lseg, box, path and polygon are rendered by path_encode().
circle_out() frees its own buffer the same way.
---
 src/backend/utils/adt/geo_ops.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c
index 73324b91fe5..531b56af4e3 100644
--- a/src/backend/utils/adt/geo_ops.c
+++ b/src/backend/utils/adt/geo_ops.c
@@ -341,6 +341,7 @@ static char *
 path_encode(enum path_delim path_delim, int npts, Point *pt)
 {
 	StringInfoData str;
+	char	   *result;
 	int			i;
 
 	initStringInfo(&str);
@@ -379,7 +380,9 @@ path_encode(enum path_delim path_delim, int npts, Point *pt)
 			break;
 	}
 
-	return str.data;
+	result = pnstrdup(str.data, str.len);
+	pfree(str.data);
+	return result;
 }								/* path_encode() */
 
 /*-------------------------------------------------------------
@@ -4784,6 +4787,7 @@ circle_out(PG_FUNCTION_ARGS)
 {
 	CIRCLE	   *circle = PG_GETARG_CIRCLE_P(0);
 	StringInfoData str;
+	char	   *result;
 
 	initStringInfo(&str);
 
@@ -4795,7 +4799,9 @@ circle_out(PG_FUNCTION_ARGS)
 	single_encode(circle->radius, &str);
 	appendStringInfoChar(&str, RDELIM_C);
 
-	PG_RETURN_CSTRING(str.data);
+	result = pnstrdup(str.data, str.len);
+	pfree(str.data);
+	PG_RETURN_CSTRING(result);
 }
 
 /*
