[PATCH] Fix leaked hash scans in pg_listening_channels()

Started by Joel Jacobsonabout 1 month ago7 messageshackers
Jump to latest
#1Joel Jacobson
joel@compiler.org

Hi hackers,

I found a hash scan leak in pg_listening_channels(), introduced by
282b1cde9d.

LISTEN ch;
SELECT pg_listening_channels() LIMIT 1;

The SELECT emits a leak warning in the server logs.

Repeated early exits can exhaust the hash scan limit and cause a
server crash.

Attached patch uses InitMaterializedSRF() to complete the scan
before returning rows, following 257c8231bf.

/Joel

Attachments:

t254268_1
0001-Fix-leaked-hash-scans-in-pg_listening_channels.patchapplication/octet-stream; name="=?UTF-8?Q?0001-Fix-leaked-hash-scans-in-pg=5Flistening=5Fchannels.patch?="Download+32-36
#2Sho Ito
i.sho.0628@gmail.com
In reply to: Joel Jacobson (#1)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

On Mon, Oct 19, 2026 at 2:42 PM Joel Jacobson <joel@compiler.org> wrote:

Attached patch uses InitMaterializedSRF() to complete the scan
before returning rows, following 257c8231bf.

Hi Joel,

I reviewed the attached patch on master with assertions enabled. Before the
fix,
repeated target-list calls under `LIMIT` in one transaction cause
a backend crash with `PANIC: ERRORDATA_STACK_SIZE exceeded`. With the patch,
the same workload completes without warnings or errors.

The materialization approach follows the SRF guidance in `funcapi.h` and
looks good to me.

Thanks,
Sho

--
=======================================
*Sho Ito (Sean)*

E-mail: i.sho.0628@gmail.com
LinkedIn: *https://www.linkedin.com/in/sho-ito-0628/
<https://www.linkedin.com/in/sho-ito-0628/>*
GitHub: https://github.com/sean0628/
=======================================

#3Joel Jacobson
joel@compiler.org
In reply to: Sho Ito (#2)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

On Wed, Oct 21, 2026, at 09:19, Sho Ito wrote:

On Mon, Oct 19, 2026 at 2:42 PM Joel Jacobson <joel@compiler.org> wrote:

Attached patch uses InitMaterializedSRF() to complete the scan
before returning rows, following 257c8231bf.

Hi Joel,

I reviewed the attached patch on master with assertions enabled. Before the fix,
repeated target-list calls under `LIMIT` in one transaction cause
a backend crash with `PANIC: ERRORDATA_STACK_SIZE exceeded`. With the patch,
the same workload completes without warnings or errors.

The materialization approach follows the SRF guidance in `funcapi.h`
and looks good to me.

Thanks for review.

Here is the commitfest entry:

https://commitfest.postgresql.org/patch/7481/

If you think it's Ready for Committer
you can update the status there.

Thanks,
Sho

--
=======================================
*Sho Ito (Sean)*

E-mail: i.sho.0628@gmail.com
LinkedIn: _https://www.linkedin.com/in/sho-ito-0628/_
GitHub: https://github.com/sean0628/
=======================================

--
Kind regards,

Joel

#4Sho Ito
i.sho.0628@gmail.com
In reply to: Joel Jacobson (#3)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

On Thu, Oct 22, 2026 at 3:56 AM Joel Jacobson <joel@compiler.org> wrote:

If you think it's Ready for Committer
you can update the status there.

Updated. Thanks!

Sho

--
=======================================
*Sho Ito (Sean)*

E-mail: i.sho.0628@gmail.com
LinkedIn: *https://www.linkedin.com/in/sho-ito-0628/
<https://www.linkedin.com/in/sho-ito-0628/>*
GitHub: https://github.com/sean0628/
=======================================

#5Joel Jacobson
joel@compiler.org
In reply to: Sho Ito (#4)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

On Thu, Oct 22, 2026, at 16:09, Sho Ito wrote:

On Thu, Oct 22, 2026 at 3:56 AM Joel Jacobson <joel@compiler.org> wrote:

If you think it's Ready for Committer
you can update the status there.

Updated. Thanks!

Thanks.

I noticed that the Windows job failed on cfbot, but it seems
unrelated to this patch. It's failing libpq's SSL negotiation test,
005_negotiate_encryption.pl.

All 239 core regression tests passed, including async:
ok 142 + async 795 ms

Above from log archive
https://nightly.link/postgres/postgres-cfbot/actions/artifacts/12553603210.zip
build/meson-logs/testlog.txt, line 224

/Joel

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joel Jacobson (#1)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

"Joel Jacobson" <joel@compiler.org> writes:

I found a hash scan leak in pg_listening_channels(), introduced by
282b1cde9d.

Good catch.

Attached patch uses InitMaterializedSRF() to complete the scan
before returning rows, following 257c8231bf.

I am wondering why you used MAT_SRF_USE_EXPECTED_DESC?
AFAICT that's only needed in functions returning "setof record",
and with a fixed output type it's usual to just pass flags = 0.

regards, tom lane

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#6)
Re: [PATCH] Fix leaked hash scans in pg_listening_channels()

I wrote:

I am wondering why you used MAT_SRF_USE_EXPECTED_DESC?

Ah, never mind that: I find that if you don't, you get

regression=# SELECT pg_listening_channels() ;
ERROR: return type must be a row type
regression=# \errverbose
ERROR: XX000: return type must be a row type
LOCATION: InitMaterializedSRF, funcapi.c:109

I question whether this aspect of InitMaterializedSRF is
well-designed, but I suppose now is no time to rethink that.

regards, tom lane