Fix OOM when rendering large geometry arrays

Started by Andrey Rachitskiy3 days ago5 messageshackers

Thread notes

1 note#bug#not-a-bug
hackorum-bot#bug#not-a-bug
hackorum-bot
2 days ago

@global #bug #not-a-bug

#bug#not-a-bug
Jump to latest
#1Andrey Rachitskiy
pl0h0yp1@gmail.com

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

Attachments:

t259799_1
0001-Fix-OOM-when-rendering-large-geometry-arrays.patchtext/x-patch; charset=US-ASCII; name=0001-Fix-OOM-when-rendering-large-geometry-arrays.patchDownload+8-2
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrey Rachitskiy (#1)
Re: Fix OOM when rendering large geometry arrays

Andrey Rachitskiy <pl0h0yp1@gmail.com> writes:

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.

This seems quite myopic. Those are far from the only datatypes whose
output functions are memory-profligate. If we wanted to do something
about this, a temporary context similar to what's being discussed in
the adjacent thread about xmlelement() [1]/messages/by-id/7581277e-e089-417d-867f-8ffd54b9e4c6@uni-muenster.de would be a more plausible
answer.

However, I'm resistant to the idea that we need to do something
about this. It will always be possible to run a backend out of
memory. If that results in a crash for you, the right answer
is to fix your OOM-killer configuration so you get a clean ENOMEM
error instead [2]https://www.postgresql.org/docs/current/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT.

regards, tom lane

[1]: /messages/by-id/7581277e-e089-417d-867f-8ffd54b9e4c6@uni-muenster.de
[2]: https://www.postgresql.org/docs/current/kernel-resources.html#LINUX-MEMORY-OVERCOMMIT

#3Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Tom Lane (#2)
Re: Fix OOM when rendering large geometry arrays

On Wed, Nov 25, 2026, 22:52 Tom Lane <tgl@sss.pgh.pa.us> wrote:

This seems quite myopic. Those are far from the only datatypes whose
output functions are memory-profligate. If we wanted to do something
about this, a temporary context similar to what's being discussed in
the adjacent thread about xmlelement() [1] would be a more plausible
answer.

I thought about temporary context, but decided not to use it since I
figured it would be too expensive in this case — it's not free, after all.

However, I'm resistant to the idea that we need to do something
about this. It will always be possible to run a backend out of
memory.

I think you're right.

Thanks for taking note of the email.

---
Regards,
Rachitskiy Andrey

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrey Rachitskiy (#3)
Re: Fix OOM when rendering large geometry arrays

Andrey Rachitskiy <pl0h0yp1@gmail.com> writes:

I thought about temporary context, but decided not to use it since I
figured it would be too expensive in this case — it's not free, after all.

Indeed, but we could use the trick I suggested in the other thread
of making a context only for big-enough arrays. Whether it's worth
the trouble remains unclear to me though.

regards, tom lane

#5Andrey Rachitskiy
pl0h0yp1@gmail.com
In reply to: Tom Lane (#4)
Re: Fix OOM when rendering large geometry arrays

чт, 26 нояб. 2026 г. в 00:34, Tom Lane <tgl@sss.pgh.pa.us>:

Andrey Rachitskiy <pl0h0yp1@gmail.com> writes:

I thought about temporary context, but decided not to use it since I
figured it would be too expensive in this case — it's not free, after

all.

Indeed, but we could use the trick I suggested in the other thread
of making a context only for big-enough arrays.

I tried to take all the comments into account in v2.
The only thing I'm not sure about MinArrayElemsForTempCxt, what should be
considered 'big'.

Whether it's worth

the trouble remains unclear to me though.

Let it rest for now, but it seems to me it won't be superfluous.

--
Regards,
Rachitskiy Andrey

Attachments:

t259799_5
v2-0001-Render-large-arrays-in-a-temporary-context.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Render-large-arrays-in-a-temporary-context.patchDownload+48-1