Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BACKPORT 2.20.7][#24951] Create separate memory context for Relation…
…BuildTriggers Summary: Original commit: 39b28c2 / D40005 Currently, there is an issue where on certain customer schemas, PG backend memory usage spikes up to 1GB during connection startup. In short, this is caused by our relcache preloading combined with inefficient memory management. The issue is as follows: 1. First, we prefetch the `pg_trigger` table by loading it from the master leader into the local tserver's memory. 2. For every table, we call `RelationBuildTriggers` to build the triggers section of the table's relcache entry. 3. Inside `RelationBuildTriggers`, we scan `pg_trigger` for the relevant triggers. Normally, we can use the `pg_trigger_tgrelid_tgname_index` index to seek directly to the triggers we want using the table's OID. But we can't do index seeks on the prefetched table, so we do a sequential scan on the entire table, copying every row of `pg_trigger` into PG memory and then filtering for the triggers we want. The entire YbUpdateRelationCache process uses a single memory context (`UpdateRelationCacheContext`), so each call to `RelationBuildTriggers` will allocate memory from this memory context which isn't freed until the entire relation cache is built. 4. As a result, we are using a lot of extra memory. If we have `m` total triggers and `n` total tables, we are allocating memory for `mn` rows in the `UpdateRelationCacheContext`. As a shorter-term fix for this issue, this revision creates a new memory context every time we invoke `RelationBuildTriggers` which gets freed at the end of `RelationBuildTriggers`. This way, even though we are still copying `mn` rows into memory, we are only allocating memory for `m` rows at a time before freeing the memory, avoiding the spike in memory usage. This issue does not address the CPU overhead of doing all of these extra copies—this is addressed in the longer-term fix D40003. Test Plan: Jenkins: urgent Reviewers: mihnea, myang Reviewed By: myang Subscribers: yql Differential Revision: https://phorge.dev.yugabyte.com/D40108
- Loading branch information