pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: DaeMyung Kang <charsyam@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Cc: DaeMyung Kang <charsyam@gmail.com>
Subject: [PATCH] Fix memory leak of primary_sysid in walreceiver
Date: Mon, 27 Apr 2026 02:01:30 +0900
Message-ID: <20260426170219.849330-1-charsyam@gmail.com> (raw)


  Hi, Hackers,

  In WalReceiverMain(), the outer streaming loop calls
  walrcv_identify_system() once per iteration to verify the primary's
  system identifier:

      primary_sysid = walrcv_identify_system(wrconn, &primaryTLI);
      ...
      if (strcmp(primary_sysid, standby_sysid) != 0)
          ereport(ERROR, ...);

  walrcv_identify_system() (libpqrcv_identify_system() in
  libpqwalreceiver.c) returns a pstrdup()'d string, but the caller
  never frees it. Each streaming restart therefore leaks the string.
  The error path is unaffected because the surrounding memory context
  is reset on ERROR.

  The attached patch adds a pfree(primary_sysid) right after the
  comparison.

  This dates back to commit 78c8c814390 ("Refactor libpqwalreceiver",
  2016), so it should be a back-patch candidate as well.

  No new tests are added; the fix only releases resources and does not
  change observable behavior. `make check` and the streaming replication
  TAP test (src/test/recovery/t/001_stream_rep.pl) pass with the patch
  applied.

  Patch attached.

  Regards,
  DaeMyung Kang

Attachments:

0002-Fix-memory-leak-of-primary_sysid-in-walreceiver.patch (text/x-patch, 539B) download diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 23fce948967..8dbfaffaa9a 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -327,6 +327,7 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) errdetail("The primary's identifier is %s, the standby's identifier is %s.", primary_sysid, standby_sysid))); } + pfree(primary_sysid); /* * Confirm that the current timeline of the primary is the same or

view thread (2+ messages)  latest in thread

Message-ID: <20260426170219.849330-1-charsyam@gmail.com>
Permalink:  ../20260426170219.849330-1-charsyam@gmail.com/
Also on:    postgresql.org/message-id/20260426170219.849330-1-charsyam@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: charsyam@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: [PATCH] Fix memory leak of primary_sysid in walreceiver
  In-Reply-To: <20260426170219.849330-1-charsyam@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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