From: Amit Kapila <akapila@postgresql.org>
Date: Thu, 23 Jun 2022 02:56:56 +0000 (+0530)
Subject: Fix memory leak due to LogicalRepRelMapEntry.attrmap.
X-Git-Tag: REL_10_22~43
X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=d873b5a5a34069a316df77285b231b792aee1349;p=postgresql.git

Fix memory leak due to LogicalRepRelMapEntry.attrmap.

When rebuilding the relation mapping on subscribers, we were not releasing
the attribute mapping's memory which was no longer required.

The attribute mapping used in logical tuple conversion was refactored in
PG13 (by commit e1551f96e6) but we forgot to update the related code that
frees the attribute map.

Author: Hou Zhijie
Reviewed-by: Amit Langote, Amit Kapila, Shi yu
Backpatch-through: 10, where it was introduced
Discussion: https://postgr.es/m/OSZPR01MB6310F46CD425A967E4AEF736FDA49@OSZPR01MB6310.jpnprd01.prod.outlook.com
---

diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c
index bc87105acc3..aa41b791129 100644
--- a/src/backend/replication/logical/relation.c
+++ b/src/backend/replication/logical/relation.c
@@ -260,6 +260,13 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
 		MemoryContext oldctx;
 		int			i;
 
+		/* Release the no-longer-useful attrmap, if any. */
+		if (entry->attrmap)
+		{
+			pfree(entry->attrmap);
+			entry->attrmap = NULL;
+		}
+
 		/* Try to find and lock the relation by name. */
 		relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
 											  remoterel->relname, -1),