Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BACKPORT 2024.1][#22262] YSQL: Fix more memory leaks in catalog cach…
…e refresh Summary: This diff fixes the remaining memory leaks in `CacheMemoryContext` that were identified to allow a stable cache memory after each catalog cache refresh. Two more identified memory leaks: * `YbCompleteAttrProcessingImpl`: `relation->rd_att->constr` needs to be freed. * `YbPreloadPgInheritsCache`: tighter scope of `MemoryContextSwitchTo(CacheMemoryContext)` For the first leak, we do not cleanup all the relations from rel cache on a catalog cache refresh. Some are considered `YbIsNonAlterableRelation` and will remain. But the overall processing flow of `YbCompleteAttrProcessingImpl` processes them anyway and will set their `relation->rd_att->constr` to the newly allocated `constr`. For the second leak in `YbPreloadPgInheritsCache`, PG code already appropriately allocates/copies into hcxt (CacheMemoryContext in this case) whenever required. If we still allocate any temporary memory into `CacheMemoryContext`, then they are not freed after being copied into `CacheMemoryContext`. Related code are: ``` static HTAB *YbPgInheritsCache; ``` and ``` void YbInitPgInheritsCache() { Assert(YbPgInheritsCache == NULL); HASHCTL ctl = {0}; ctl.keysize = sizeof(Oid); ctl.entrysize = sizeof(YbPgInheritsCacheEntryData); ctl.hcxt = CacheMemoryContext; ``` Note that `ctl.hcxt = CacheMemoryContext` ensures the copying into `CacheMemoryContext`. By moving `MemoryContextSwitchTo(CacheMemoryContext)` to the minimum scope where it is truly required to ensure the right memory context, those temporary memory will be allocated from transaction memory that will be auto-freed after the transaction (created just for doing cache refresh) is over. With these two leaks fixed, I am able to add a new unit test that checks that the cache memory size stay the same after cache refresh. Jira: DB-11181 Original commit: a9b9a2b / D35017 Test Plan: ./yb_build.sh release --cxx-test pg_libpq-test --gtest_filter PgLibPqTest.CatalogCacheMemoryLeak -n 20 Reviewers: fizaa, kfranz Reviewed By: kfranz Subscribers: yql Tags: #jenkins-ready Differential Revision: https://phorge.dev.yugabyte.com/D35199
- Loading branch information