Hello, Hackers!

While studying geometric types, I found a way to crash the backend:

postgres=# SELECT array_fill('(0,0)'::point, ARRAY[67108862]);
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.
!?>

dmesg log:
[67596.130371] Out of memory: Killed process 685226 (postgres)
total-vm:26289536kB, anon-rss:23991064kB, file-rss:8kB, shmem-rss:10624kB,
UID:1000 pgtables:50444kB oom_score_adj:200

Problem:
Casting a large geometry array to text can run the backend out of memory.
The text representation of a single point is only a few bytes, but it is
rendered into a 1024-byte buffer. Since array_out() retains each element's
string until the entire array text has been assembled, the backend ends up
holding one such buffer per element.

Proposed fix:
Copy only the bytes actually used by the rendered text and free the
1024-byte buffer. The point, lseg, box, path, and polygon types are
rendered via path_encode(). circle_out() builds its own buffer and frees it
in the same way.

Thoughts ?

-- 
Regards,
Rachitskiy Andrey
From: Andrey Rachitskiy <[email protected]>
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);
 }
 
 /*

Reply via email to