aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-03-18 20:50:56 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-03-18 20:50:56 -0400
commit1452a0bb87c78812372f103de14cb6492a932ac6 (patch)
tree58cc3727cae37ef1943e92c916b41d58943208a7
parentfbcc9fe33c43a3fb79812b72960df4909a4551e5 (diff)
downloadpostgresql-1452a0bb87c78812372f103de14cb6492a932ac6.tar.gz
postgresql-1452a0bb87c78812372f103de14cb6492a932ac6.zip
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/20210317023101.anvejcfotwka6gaa@alap3.anarazel.de Discussion: https://postgr.es/m/3816764.1616104288@sss.pgh.pa.us
-rw-r--r--src/backend/utils/cache/relcache.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index c6e3dc30169..2babf525380 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -3359,6 +3359,13 @@ RelationBuildLocalRelation(const char *relname,
rel->rd_rel->relam = accessmtd;
+ /*
+ * RelationInitTableAccessMethod will do syscache lookups, so we mustn't
+ * run it in CacheMemoryContext. Fortunately, the remaining steps don't
+ * require a long-lived current context.
+ */
+ MemoryContextSwitchTo(oldcxt);
+
if (relkind == RELKIND_RELATION ||
relkind == RELKIND_SEQUENCE ||
relkind == RELKIND_TOASTVALUE ||
@@ -3382,11 +3389,6 @@ RelationBuildLocalRelation(const char *relname,
*/
EOXactListAdd(rel);
- /*
- * done building relcache entry.
- */
- MemoryContextSwitchTo(oldcxt);
-
/* It's fully valid */
rel->rd_isvalid = true;