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 1wH2sI-006oxD-0Z for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Apr 2026 17:02:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wH2sH-00Adkl-1H for pgsql-hackers@arkaria.postgresql.org; Sun, 26 Apr 2026 17:02:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wH2sH-00Adkd-0P for pgsql-hackers@lists.postgresql.org; Sun, 26 Apr 2026 17:02:29 +0000 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wH2sF-00000003C6S-0beY for pgsql-hackers@lists.postgresql.org; Sun, 26 Apr 2026 17:02:28 +0000 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-82f1f03754bso429299b3a.3 for ; Sun, 26 Apr 2026 10:02:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777222945; x=1777827745; 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=X0b87VnkW+in+TdpyuIB6iL0jCh8tTjjxFNWBaUQF5k=; b=Dh5PqpCmCIZ2AZ/Ywoq3LsMchr1GVujvwKtbGF7CsFlmpS9ujJn8E57nmGh/o9hymn ybbUH5NRUYZCAWmV7IdECOAu7+RMjfzWpMBYveikq/bGNiAu3nnY00qmEjFhIyd27QIu EhYMnqGS68khrJhGr8eBBmiIYGn/zOSq3X+XMd9zB8IkFjZ9Xi6OEaTnnHs4WeECOgTJ FeUfma7dIlvJX0Lx3+uP8kxBOt7eZPswNHi40XaaKgOUzE+5Rt+akouMXVXeNEGtCdyD V9aj4cZU7YAr4IcD6MYvsuI4QayxzBQAlhG2GOq1XXr/YGIbhpCEG8B4gCpBHwQPiyrF Afkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777222945; x=1777827745; 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=X0b87VnkW+in+TdpyuIB6iL0jCh8tTjjxFNWBaUQF5k=; b=fHrlQY+2T8JYyWwqnvsJs3uLMkdngavuoJk/nKuiWwO7JwmvpcmZygCxF/0KHuU/EH eX35AiMKGZrUU0QPqaLCnCPs+/b+LIzGKTsKZht5xv7nhdwu5n4UihIDANcUnuKeBOEt zGPCnxspY+ftPuzYzniYoGmxt+EnKMgrLBA2hZ7TPk5jSji+CpXRHMU6FY14teQeEVSL 71EbMMPZHoFo1kAh3oDcDWjMzlkwdhCDWElEUT3cNvjXKpxnzlYVljjt++of284bQgh0 Xy53xstU/DHCYxalWN4lbUfj/Hp/Gu152MQ5dOfAmFrDFLR5Mm5oFgUHPOCxtCW56quf TylQ== X-Gm-Message-State: AOJu0YzQFgIE+Q7pOj1AtQw7syXngcKzDnUwSlt7x8zFzv9TqDBNNaAq AnOUY87tCjkwZWCvTJBrqkqeFwSy1355HNh5MVUxwleoNhN7/IZEalxHHSN24A== X-Gm-Gg: AeBDietlslEi95apfroVY7IpO6GD6qEMX9wVFBjw4/ldRI3pkA6MyOMFLBo1RfIeJg+ LX+DNSyNAZ2rNOkottbqt3w+Am1Dr42Mra2J1WV11l2kysLoF/JECBwBq5ME9hxw9CzjvWLxsRA Yxkwq+ybqU/kCMKXzOf+LD5r+bzOgV1hTC4u4xrSdJSAgs0o50vhGcTZIM7mH+qA3WJPNltK+u0 qZWQ7m0hWSXiYOrBBpthDcjpRvwstRxoPM2CUrjJ5vdnIw9yi7NgWpyvWj2v0ZAtePuMix0LFHS mnQLn8o7PIkyRiymM0QK9ULxQKksjjWQWnhv6La2Rdvu+8t6T2Do2Lx73TmQT18TjNJSCs/vCXW 2s8tiy7KfreKCViBTH7VQ446eFvpjLIO+xWnKmR46SLYAycDmWfFMIkTaDYheuyax1uDlQGWJaK hui17/qW70E2Jq9qscBXJFSUKWyYa7W3nY8Zut5Q== X-Received: by 2002:a05:6a00:10c5:b0:81f:4546:cdff with SMTP id d2e1a72fcca58-82f8c8d0138mr21081756b3a.3.1777222944747; Sun, 26 Apr 2026 10:02:24 -0700 (PDT) Received: from ser8.. ([221.156.231.192]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f8ebcc9easm37412659b3a.39.2026.04.26.10.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2026 10:02:24 -0700 (PDT) From: DaeMyung Kang To: pgsql-hackers@lists.postgresql.org Cc: DaeMyung Kang 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> 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, 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 --------------2.43.0 Content-Type: text/x-patch; name="0002-Fix-memory-leak-of-primary_sysid-in-walreceiver.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0002-Fix-memory-leak-of-primary_sysid-in-walreceiver.patch" 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 --------------2.43.0--