Skip to content

Commit c9b3d49

Browse files
committed
Fix memory leak in pgoutput with relation attribute map
pgoutput caches the attribute map of a relation, that is free()'d only when validating a RelationSyncEntry. However, this code path is not taken when calling any of the SQL functions able to do some logical decoding, like pg_logical_slot_{get,peek}_changes(), leaking some memory into CacheMemoryContext on repeated calls. To address this, a relation's attribute map is allocated in PGOutputData's cachectx, free()'d at the end of the execution of these SQL functions when logical decoding ends. This is available down to 15. v13 and v14 have a similar leak, which will be dealt with later. Reported-by: Masahiko Sawada Author: Vignesh C Reviewed-by: Hou Zhijie Discussion: https://postgr.es/m/CAD21AoDkAhQVSukOfH3_reuF-j4EU0-HxMqU3dU+bSTxsqT14Q@mail.gmail.com Discussion: https://postgr.es/m/CALDaNm1hewNAsZ_e6FF52a=9drmkRJxtEPrzCB6-9mkJyeBBqA@mail.gmail.com Backpatch-through: 15
1 parent ebf2ab4 commit c9b3d49

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,8 @@ init_tuple_slot(PGOutputData *data, Relation relation,
11971197
TupleDesc indesc = RelationGetDescr(relation);
11981198
TupleDesc outdesc = RelationGetDescr(ancestor);
11991199

1200-
/* Map must live as long as the session does. */
1201-
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
1200+
/* Map must live as long as the logical decoding context. */
1201+
oldctx = MemoryContextSwitchTo(data->cachectx);
12021202

12031203
entry->attrmap = build_attrmap_by_name_if_req(indesc, outdesc, false);
12041204

0 commit comments

Comments
 (0)