pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[PATCH] Fix leaked hash scans in pg_listening_channels()
7+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-10-25 2b491ff1cd dfd7ede529  Fix leaked hash scans in pg_listening_channels().
* [PATCH] Fix leaked hash scans in pg_listening_channels()
@ 2026-10-19 05:41 Joel Jacobson <joel@compiler.org>
  2026-10-21 07:19 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
  2026-10-25 18:30 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 7+ messages in thread

From: Joel Jacobson @ 2026-10-19 05:41 UTC (permalink / raw)
  To: pgsql-hackers

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:

0001-Fix-leaked-hash-scans-in-pg_listening_channels.patch (application/octet-stream, 4.7K) download From 0b82148fc95222eede848e05f94a25bd8c37dd9b Mon Sep 17 00:00:00 2001 From: Joel Jacobson <joel@compiler.org> Date: Mon, 19 Oct 2026 05:51:40 +0200 Subject: [PATCH] Fix leaked hash scans in pg_listening_channels(). Commit 282b1cde9d changed pg_listening_channels() to keep a hash table scan open between calls of the set-returning function. If the executor stops fetching rows early, as with a target-list call under LIMIT, the scan remains registered until transaction end. Repeated early exits can exhaust the hash scan limit and cause a server crash. Materialize the result in a tuplestore, completing the hash scan before returning any rows. This follows the approach used for pg_buffercache_pages() in commit 257c8231bf. Use MAT_SRF_USE_EXPECTED_DESC for the scalar SETOF text result, as in pg_ls_dir() in commit a19e5cee63. Add a regression test using a target-list call with LIMIT 1. Before the fix, this emits a leak warning at the statement's implicit commit. --- src/backend/commands/async.c | 56 +++++++++++------------------ src/test/regress/expected/async.out | 7 ++++ src/test/regress/sql/async.sql | 4 +++ 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c index e266fe64cc..9d4291acbe 100644 --- a/src/backend/commands/async.c +++ b/src/backend/commands/async.c @@ -188,6 +188,7 @@ #include "utils/ps_status.h" #include "utils/snapmgr.h" #include "utils/timestamp.h" +#include "utils/tuplestore.h" /* @@ -1087,53 +1088,38 @@ Async_UnlistenAll(void) /* * SQL function: return a set of the channel names this backend is actively * listening to. - * - * Note: this coding relies on the fact that the localChannelTable cannot - * change within a transaction. */ Datum pg_listening_channels(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - HASH_SEQ_STATUS *status; + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; - /* stuff done only on the first call of the function */ - if (SRF_IS_FIRSTCALL()) + InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC); + + /* + * Complete the hash scan in one call, since the caller might not fetch + * all the rows and would otherwise leave the scan registered until + * transaction end. + */ + if (localChannelTable != NULL) { - /* create a function context for cross-call persistence */ - funcctx = SRF_FIRSTCALL_INIT(); + HASH_SEQ_STATUS status; + ChannelName *channelEntry; - /* Initialize hash table iteration if we have any channels */ - if (localChannelTable != NULL) + hash_seq_init(&status, localChannelTable); + while ((channelEntry = (ChannelName *) hash_seq_search(&status)) != NULL) { - MemoryContext oldcontext; + Datum values[1]; + bool nulls[1] = {false}; - oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - status = palloc_object(HASH_SEQ_STATUS); - hash_seq_init(status, localChannelTable); - funcctx->user_fctx = status; - MemoryContextSwitchTo(oldcontext); - } - else - { - funcctx->user_fctx = NULL; + values[0] = CStringGetTextDatum(channelEntry->channel); + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, + values, nulls); + pfree(DatumGetPointer(values[0])); } } - /* stuff done on every call of the function */ - funcctx = SRF_PERCALL_SETUP(); - status = (HASH_SEQ_STATUS *) funcctx->user_fctx; - - if (status != NULL) - { - ChannelName *entry; - - entry = (ChannelName *) hash_seq_search(status); - if (entry != NULL) - SRF_RETURN_NEXT(funcctx, CStringGetTextDatum(entry->channel)); - } - - SRF_RETURN_DONE(funcctx); + return (Datum) 0; } /* diff --git a/src/test/regress/expected/async.out b/src/test/regress/expected/async.out index 19cbe38e63..20a30680ee 100644 --- a/src/test/regress/expected/async.out +++ b/src/test/regress/expected/async.out @@ -30,6 +30,13 @@ ERROR: channel name too long --Should work. Valid NOTIFY/LISTEN/UNLISTEN commands NOTIFY notify_async2; LISTEN notify_async2; +-- Check cleanup when a target-list SRF is abandoned early. +SELECT pg_listening_channels() LIMIT 1; + pg_listening_channels +----------------------- + notify_async2 +(1 row) + UNLISTEN notify_async2; UNLISTEN *; -- Should return zero while there are no pending notifications. diff --git a/src/test/regress/sql/async.sql b/src/test/regress/sql/async.sql index 40f6e01538..3965fd594c 100644 --- a/src/test/regress/sql/async.sql +++ b/src/test/regress/sql/async.sql @@ -15,6 +15,10 @@ SELECT pg_notify('notify_async_channel_name_too_long____________________________ --Should work. Valid NOTIFY/LISTEN/UNLISTEN commands NOTIFY notify_async2; LISTEN notify_async2; + +-- Check cleanup when a target-list SRF is abandoned early. +SELECT pg_listening_channels() LIMIT 1; + UNLISTEN notify_async2; UNLISTEN *; -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
@ 2026-10-21 07:19 ` Sho Ito <i.sho.0628@gmail.com>
  2026-10-21 18:56   ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  1 sibling, 1 reply; 7+ messages in thread

From: Sho Ito @ 2026-10-21 07:19 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-hackers

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/
=======================================


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  2026-10-21 07:19 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
@ 2026-10-21 18:56   ` Joel Jacobson <joel@compiler.org>
  2026-10-22 14:09     ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Joel Jacobson @ 2026-10-21 18:56 UTC (permalink / raw)
  To: Sho Ito <i.sho.0628@gmail.com>; +Cc: pgsql-hackers

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






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  2026-10-21 07:19 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
  2026-10-21 18:56   ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
@ 2026-10-22 14:09     ` Sho Ito <i.sho.0628@gmail.com>
  2026-10-25 11:43       ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  0 siblings, 1 reply; 7+ messages in thread

From: Sho Ito @ 2026-10-22 14:09 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-hackers

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/
=======================================


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  2026-10-21 07:19 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
  2026-10-21 18:56   ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  2026-10-22 14:09     ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Sho Ito <i.sho.0628@gmail.com>
@ 2026-10-25 11:43       ` Joel Jacobson <joel@compiler.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Joel Jacobson @ 2026-10-25 11:43 UTC (permalink / raw)
  To: Sho Ito <i.sho.0628@gmail.com>; +Cc: pgsql-hackers

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






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
@ 2026-10-25 18:30 ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-10-25 18:42   ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 7+ messages in thread

From: Tom Lane @ 2026-10-25 18:30 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-hackers

"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






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Fix leaked hash scans in pg_listening_channels()
  2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
  2026-10-25 18:30 ` Re: [PATCH] Fix leaked hash scans in pg_listening_channels() Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-10-25 18:42   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Tom Lane @ 2026-10-25 18:42 UTC (permalink / raw)
  To: Joel Jacobson <joel@compiler.org>; +Cc: pgsql-hackers

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






^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2026-10-25 18:42 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 05:41 [PATCH] Fix leaked hash scans in pg_listening_channels() Joel Jacobson <joel@compiler.org>
2026-10-21 07:19 ` Sho Ito <i.sho.0628@gmail.com>
2026-10-21 18:56   ` Joel Jacobson <joel@compiler.org>
2026-10-22 14:09     ` Sho Ito <i.sho.0628@gmail.com>
2026-10-25 11:43       ` Joel Jacobson <joel@compiler.org>
2026-10-25 18:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-10-25 18:42   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox