| From: | John Naylor <johncnaylorls(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | memory leak in bounded sorts |
| Date: | 2026-12-02 10:04:53 |
| Message-ID: | CANWCAZZs8rmJhsO9PSj9m5fJmasJOhrXM3FcC=Q5LnBqKey8Pw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
While reviewing bounded-sort improvements, I found a separate issue:
make_bounded_heap() doesn't free the root tuple when replaced (and
hasn't for 10 years), which shows up as increased memory reporting,
especially with inputs in reverse order. In the attached repro, we see
master:
QUERY PLAN
--------------------------------------------------------------
Limit (actual rows=5000.00 loops=1)
-> Sort (actual rows=5000.00 loops=1)
Sort Key: k DESC
Sort Method: top-N heapsort Memory: 10542kB
-> Seq Scan on mem01 (actual rows=20000.00 loops=1)
(5 rows)
patch:
QUERY PLAN
--------------------------------------------------------------
Limit (actual rows=5000.00 loops=1)
-> Sort (actual rows=5000.00 loops=1)
Sort Key: k DESC
Sort Method: top-N heapsort Memory: 5463kB
-> Seq Scan on mem01 (actual rows=20000.00 loops=1)
(5 rows)
It's academic from the OS perspective, since the blocks can't be freed
until the sort ends, but the user-visible anomaly still seems worth
backpatching. Barring objections I'll commit the attached fix soon.
--
John Naylor
Amazon Web Services
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Free-the-displaced-heap-top-when-switching-to-a-b.patch | text/x-patch | 1.3 KB |
| bounded-sort-leak.sql | application/sql | 1.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Amit Kapila | 2026-12-02 10:23:26 | Re: ALTER DEFAULT PRIVILEGES IN SCHEMA pg_conflict silently has no effect |
| Previous Message | Zhijie Hou | 2026-12-02 09:58:22 | Re: Show the conflict log table in \dRs+ output |