Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wH2r1-006ow2-1B for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Apr 2026 17:01:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wH2qy-00AZpD-22 for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Apr 2026 17:01:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wH2qy-00AZoy-0q for pgsql-hackers@lists.postgresql.org; Sun, 26 Apr 2026 17:01:08 +0000 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wH2qw-00000002tm4-0Xqv for pgsql-hackers@lists.postgresql.org; Sun, 26 Apr 2026 17:01:07 +0000 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-364746755a8so61489a91.1 for ; Sun, 26 Apr 2026 10:01:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777222865; x=1777827665; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=RPNAZ6bfeTWvJ4fLAFrcEFVTyxNiIBpfizCVmbVYyro=; b=fSMUfiFqZ+bmHCZvYng9NCs9SV0lLd9Vq1+PX0Ex+/9oQtuB4gfEzDh6Hl1BWBjV+W R/SddIWSkUegcRCusZhfBCpkYvPbHSrEZpGqe96EiJ0j9C3wKwj1VmnOk3iM88kHYPq9 SN9x+O1/ntwqxEPr4h/stfw77PT29b1Z7vRJXtbCTG6A3CWCHf1ojVkr+ZUIBpGN7jwH UDtlDcedJNHkSBVM7AzKcCtj3jA57TYkQCvzUJugmZGWi6YC9/igCiphOG0OKPcadihh PyL0tKdwWpLsTCfAkiNln8Zgkr/e61O9Cmmg1PEr1PJCtrtTjhGs/wFRPOE5w3LQ+q6O Om+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777222865; x=1777827665; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=RPNAZ6bfeTWvJ4fLAFrcEFVTyxNiIBpfizCVmbVYyro=; b=nVlQFZVC2oh3jtvw5LvRaK7xVCp+i64TRy0afnC8FkOzcOA55EDxC+y3OMD6lFFUH4 Pc5TZ1lYB5vm3fouyxguIZ/EByzU/xSFL10gThLseQUQ6OWD/YDtifzIR9NgT+8CfEcs eNF3q5wZgDuWSJdvKL6UeQ4IPnd8aODT35Ac0Fts5YP8nzQbEOlYXVw5m52P2rmbOIpp iHg2+3hSD5jzN1hq7rCcWL31bCoVyp7TL2xcMC+hQoVAz/1eRnKm1ewHjLao3Jv8UUOq 825fAuRNhq9UibBzFBoAzA8/n4G6VptOR/B6PfQo8KEup7IM15cwzzfR2i5RUE7Y/+vU dsyA== X-Gm-Message-State: AOJu0YzrbO2yYOFzveooKefzl0cuhTjKY3w/DKqv6pusnYE0Humk3AvW Em7rRXILLaMbHqwc8mqnzgZRqfuww06vvjxXZgUOUyD2PcJmK7STQxz3THFuBw== X-Gm-Gg: AeBDieteBh7zcpxusJwlp1SwXVgdqfFOEN7kcdHrbbq7xrBGa59UVy0DUKanAlo5pHI Qv8PucZJMQgayVfJWOj71bO6tKK66mN33d8K1KNJyzu7xEJ8gREskl1xTRgwexnT5ftc8wua06s 0yd/jDHAIB5xk3k2Oc6J3cEglRZ463lgBvY6g1WeygM7InvcqQJ54j2wFFvfArJ2roYkEe5g8NW 8KAdTSyGzGIL1IniY87sQxpTvTD7cYYy0fIzPo0lh/+o+53wPb4uBJVpZHm+Fslkv11hcu5qO9C dISqk0x/7wmryqBHUBol1GOwlfJmonNEQbSHXvv/5loGpdLBxLDNynVmfjyjOxus4a/Z2c+DBL1 /3IWOBQtl7xVRLT6Z3tyC+LUiu8xFu58M4LpLtN8mlsKJUAAWalD6yuBWi+m5CBKAdpy+jaKVBw lhnccpGvApXVxOxfNLuRrzJ1xWdYg= X-Received: by 2002:a17:903:1a6b:b0:2b9:42a3:6013 with SMTP id d9443c01a7336-2b942a361famr35070095ad.1.1777222865047; Sun, 26 Apr 2026 10:01:05 -0700 (PDT) Received: from ser8.. ([221.156.231.192]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fab288aesm269216315ad.65.2026.04.26.10.01.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 10:01:04 -0700 (PDT) From: DaeMyung Kang To: pgsql-hackers@lists.postgresql.org Cc: DaeMyung Kang Subject: [PATCH] Fix memory leak of reply_message in walreceiver Date: Mon, 27 Apr 2026 01:59:09 +0900 Message-ID: <20260426170100.847923-1-charsyam@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2.43.0" Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------2.43.0 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: 8bit Hi, Hackers, While reading walreceiver.c, I noticed that the static StringInfoData reply_message gets initialized inside the outer streaming loop in WalReceiverMain(), specifically right after walrcv_startstreaming() succeeds: /* Initialize LogstreamResult and buffers for processing messages */ LogstreamResult.Write = LogstreamResult.Flush = GetXLogReplayRecPtr(NULL); initStringInfo(&reply_message); initStringInfo() unconditionally allocates a fresh ~1KB buffer with palloc() and overwrites the existing data pointer without freeing the previous one. So every time the walreceiver re-enters the streaming path -- e.g., after a timeline switch, end-of-WAL, or any other condition that drives the outer for(;;) loop to iterate -- the prior buffer is leaked. The leak is bounded per streaming restart but accumulates over the lifetime of a long-running standby that restarts streaming often. Subsequent uses of reply_message already call resetStringInfo() to reuse the buffer, so a single one-time initialization before the outer loop is sufficient. The attached patch moves the call up and adjusts the comment accordingly. This appears to date back to commit add6c3179a4 ("Make the streaming replication protocol messages architecture-independent.", 2012), so the fix is likely a candidate for back-patching to all supported branches. 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 --------------2.43.0 Content-Type: text/x-patch; name="0001-Fix-memory-leak-of-reply_message-in-walreceiver.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0001-Fix-memory-leak-of-reply_message-in-walreceiver.patch" diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 8185412a810..23fce948967 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -302,6 +302,8 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) if (sender_host) pfree(sender_host); + initStringInfo(&reply_message); + first_stream = true; for (;;) { @@ -405,9 +407,8 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len) walrcv->walRcvState = WALRCV_STREAMING; SpinLockRelease(&walrcv->mutex); - /* Initialize LogstreamResult and buffers for processing messages */ + /* Initialize LogstreamResult for processing messages */ LogstreamResult.Write = LogstreamResult.Flush = GetXLogReplayRecPtr(NULL); - initStringInfo(&reply_message); /* Initialize nap wakeup times. */ now = GetCurrentTimestamp(); --------------2.43.0--