Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f245236

Browse files
committed
Use per-tuple context in ExecGetAllUpdatedCols
Commit fc22b66 (generated columns) replaced ExecGetUpdatedCols() with ExecGetAllUpdatedCols() in a couple places handling UPDATE (triggers and lock mode). However, ExecGetUpdatedCols() did exec_rt_fetch() while ExecGetAllUpdatedCols() also allocates memory through bms_union() without paying attention to the memory context and happened to use the long-lived ExecutorState, leaking the memory until the end of the query. The amount of leaked memory is proportional to the number of (updated) attributes, types of UPDATE triggers, and the number of processed rows (which for UPDATE ... FROM ... may be much higher than updated rows). Fixed by switching to the per-tuple context in GetAllUpdatedColumns(). This is fine for all in-core callers, but external callers may need to copy the result. But we're not aware of any such callers. Note the issue was introduced by fc22b66, but the macros were later renamed by f50e888. Backpatch to 12, where the issue was introduced. Reported-by: Tomas Vondra Reviewed-by: Andres Freund, Tom Lane, Jakub Wartak Backpatch-through: 12 Discussion: https://postgr.es/m/222a3442-7f7d-246c-ed9b-a76209d19239@enterprisedb.com
1 parent fb5a7d8 commit f245236

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/backend/executor/execUtils.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,12 +1345,26 @@ ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13451345
return relinfo->ri_extraUpdatedCols;
13461346
}
13471347

1348-
/* Return columns being updated, including generated columns */
1348+
/*
1349+
* Return columns being updated, including generated columns
1350+
*
1351+
* The bitmap is allocated in per-tuple memory context. It's up to the caller to
1352+
* copy it into a different context with the appropriate lifespan, if needed.
1353+
*/
13491354
Bitmapset *
13501355
ExecGetAllUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13511356
{
1352-
return bms_union(ExecGetUpdatedCols(relinfo, estate),
1353-
ExecGetExtraUpdatedCols(relinfo, estate));
1357+
Bitmapset *ret;
1358+
MemoryContext oldcxt;
1359+
1360+
oldcxt = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
1361+
1362+
ret = bms_union(ExecGetUpdatedCols(relinfo, estate),
1363+
ExecGetExtraUpdatedCols(relinfo, estate));
1364+
1365+
MemoryContextSwitchTo(oldcxt);
1366+
1367+
return ret;
13541368
}
13551369

13561370
/*

0 commit comments

Comments
 (0)