Skip to content

Commit 415ffdc

Browse files
committed
Don't run RelationInitTableAccessMethod in a long-lived context.
Some code paths in this function perform syscache lookups, which can lead to table accesses and possibly leakage of cruft into the caller's context. If said context is CacheMemoryContext, we eventually will have visible bloat. But fixing this is no harder than moving one memory context switch step. (The other callers don't have a problem.) Andres Freund and I independently found this via valgrind testing. Back-patch to v12 where this code was added. Discussion: https://postgr.es/m/[email protected] Discussion: https://postgr.es/m/[email protected]
1 parent 28644fa commit 415ffdc

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/backend/utils/cache/relcache.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -3543,6 +3543,13 @@ RelationBuildLocalRelation(const char *relname,
35433543

35443544
rel->rd_rel->relam = accessmtd;
35453545

3546+
/*
3547+
* RelationInitTableAccessMethod will do syscache lookups, so we mustn't
3548+
* run it in CacheMemoryContext. Fortunately, the remaining steps don't
3549+
* require a long-lived current context.
3550+
*/
3551+
MemoryContextSwitchTo(oldcxt);
3552+
35463553
if (relkind == RELKIND_RELATION ||
35473554
relkind == RELKIND_SEQUENCE ||
35483555
relkind == RELKIND_TOASTVALUE ||
@@ -3566,11 +3573,6 @@ RelationBuildLocalRelation(const char *relname,
35663573
*/
35673574
EOXactListAdd(rel);
35683575

3569-
/*
3570-
* done building relcache entry.
3571-
*/
3572-
MemoryContextSwitchTo(oldcxt);
3573-
35743576
/* It's fully valid */
35753577
rel->rd_isvalid = true;
35763578

0 commit comments

Comments
 (0)