Fix OOM when rendering large geometry arrays

From: Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>
To: pgsql-hackers mailing list <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Fix OOM when rendering large geometry arrays
Date: 2026-11-25 15:38:04
Message-ID: CAB8bMivO7+s4SXCWNBCX5B8yqobtFmiUt9gkpYgFo_7ngewm4g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-Fix-OOM-when-rendering-large-geometry-arrays.patch text/x-patch 1.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Kirill Reshke 2026-11-25 15:38:38 Re: CYCLE over calculates windows functions
Previous Message Tom Lane 2026-11-25 15:30:29 Re: OOM kill in xmlelement() with large arrays