pgsql: Fix memory leak in pgoutput with streamed prepared transactions.

From: Masahiko Sawada <msawada(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix memory leak in pgoutput with streamed prepared transactions.
Date: 2026-09-18 17:19:48
Message-ID: E1x7cFY-00000000HwZ-27fj@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix memory leak in pgoutput with streamed prepared transactions.

pgoutput records the toplevel XID in the relation's streamed_txns
list, allocated in CacheMemoryContext, when it sends a relation's
schema during a streamed transaction. We clean up that list at stream
commit and stream abort, but missed stream prepare, so the XIDs stay
around until a relcache invalidation for the relation or until the
walsender exits.

Fix by cleaning up the cache after writing the stream prepare message,
marking the schema as sent as we do at stream commit. The subscriber
applies the spooled relation messages at PREPARE time and its relation
map isn't rolled back by ROLLBACK PREPARED, so we don't need to send
the schema again even if the prepared transaction eventually aborts.

Oversight in commit 63cf61cdeb7b.

Author: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
Reviewed-by: Hayato Kuroda <kuroda(dot)hayato(at)fujitsu(dot)com>
Reviewed-by: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAJTYsWWt5aaKHCnV=jNOwABzspd-WbnhWLndDqtESDQRQ-BkKQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3d6a4a582efe5c64abf1a3669d707f3f248bd3ef

Modified Files
--------------
src/backend/replication/pgoutput/pgoutput.c | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Masahiko Sawada 2026-09-18 17:19:52 pgsql: Fix memory leak in pgoutput with streamed prepared transactions.
Previous Message Masahiko Sawada 2026-09-18 17:19:44 pgsql: Fix memory leak in pgoutput with streamed prepared transactions.