Open sushantrmishra opened 2 weeks ago
How I came up with the patch:
a simple SELECT * FROM yb_heap_stats();
confirmed that most memory was allocated in PGGate:
SELECT * FROM yb_heap_stats() -- converted to kb, commas added for clarity
total_heap_requested | 1,503,657
total_heap_usage | 1,752,451
total_heap_allocation | 1,499,350
cached_free_memory | 1,335
total_heap_released | 0
PostgreSQL_memory_usage | 1,473
PostgreSQL_storage_gateway_usage | 1,497,876
Apply the diff https://phorge.dev.yugabyte.com/D33735 and rebuild
Modify the test script:
a. Add cursor.execute("set yb_tcmalloc_sample_freq = 16;")
to the beginning
b. To the main loop, add
if (i % 100000 == 0):
cursor.execute("select * from yb_heap_snapshot(false);")
print(cursor.fetchall())
Run the test script and observe the following profiled allocations after a few hundred thousand iterations: sample.txt
e.g.
[(23615616, 368994, 64, 23615616, 368994, 'tcmalloc::tcmalloc_internal::SampleifyAllocation<>()
slow_alloc<>()
TCMallocInternalNewArray
std::__1::__hash_table<>::__emplace_unique_impl<>()
yb::pggate::PgSession::AddForeignKeyReference()
yb::pggate::PgApiImpl::AddForeignKeyReference()
YBCPgAddIntoForeignKeyReferenceCache
...
(the first few numbers correspond to estimated_bytes, estimated_count, avg_bytes_per_allocation, sampled_bytes, sample_count)
The top three entries each reference the ForeignKeyReferenceCache. There might be more, but starting with that will give an idea of what percent of the bloat is due to the cache.
Jira Link: DB-12638
Description
Memory bloat is observed when multiple inserts are performed in single transaction. Once the transaction is committed or aborted then memory is released.
Query Pattern :
Begin; insert <1> ; insert <2> , insert <3> ......, Insert <N>; commit;
reproduction script:
Sample output:
The issue is resolved with the following patch applied (key difference is the change in
yb_exec_query_wrapper_one_attempt
) invalidate-foreign-key-cache.patchwith the patch:
(This is probably not the right fix. But it proves that the growing allocations exist in the FK reference caches)
Issue Type
kind/bug
Warning: Please confirm that this issue does not contain any sensitive information