pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix memory leaks when freeing WAL readers
6+ messages / 1 participants
[nested] [flat]

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:20  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:20 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6a146d297503b271f4cff6cdf8eaf8f80bbd26a6

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:21  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/a919ea702eb6b452f20a0ed915c662bc6c16e725

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:21  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/7418d9e9605fe208e8f642a1c4789415f244dcc2

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:21  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/aa6f14da4b6f796d94f78959be2eb8deb05152c6

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:21  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/4c271dc216065c8e6c99ba3097e831e6ef6cd9e1

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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

* pgsql: Fix memory leaks when freeing WAL readers
@ 2026-11-04 02:21  Fujii Masao <fujii@postgresql.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Fujii Masao @ 2026-11-04 02:21 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix memory leaks when freeing WAL readers

Commit 3f1ce97346 introduced a circular buffer for decoded WAL records,
with separate allocations for records that do not fit in the buffer.
However, XLogReaderFree() did not release separately allocated
records remaining in the decode queue. In particular, the last record
returned by XLogReadRecord() remains queued until released, so freeing
the reader could leak that record.

For many callers, the leaked memory is reclaimed when the containing
memory context is reset or deleted. However, it can accumulate in
long-lived contexts. For example, when checkpointing prepared
transactions, XlogReadTwoPhaseData() creates a WAL reader, reads the
PREPARE record, and frees the reader. Large PREPARE records can
therefore leave allocations in checkpointer_context, which is not
reset during normal operation. These allocations accumulate until
an error resets the context or the checkpointer exits.

Fix this by calling ResetDecoder() in XLogReaderFree() to release all
separately allocated records in the queue. Do this before freeing the
decode buffer, since the queue can also contain records stored in that
buffer.

Backpatch to v15, where the circular WAL decoding buffer was
introduced.

Author: Tomás Senart <ts@perfloop.ai>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/CAEEBa3Uai8TGPd6P5SX_fzbu3KHMwSvu9WuqA8JPSPQ1RJR8fQ@mail.gmail.com
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8b04cc817f7255ba8a9ad306678bf0dde900a1cc

Modified Files
--------------
src/backend/access/transam/xlogreader.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)



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


end of thread, other threads:[~2026-11-04 02:21 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 02:20 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>
2026-11-04 02:21 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>
2026-11-04 02:21 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>
2026-11-04 02:21 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>
2026-11-04 02:21 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>
2026-11-04 02:21 pgsql: Fix memory leaks when freeing WAL readers Fujii Masao <fujii@postgresql.org>

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