Skip to content

Commit

Permalink
[BACKPORT 2024.1][#22262] YSQL: Fix memory leak in pg_constraint/pg_a…
Browse files Browse the repository at this point in the history
…ttrdef local cache

Summary:
The memory leak was identified via code inspection during debugging the issue.

```
static void
YbInitUpdateRelationCacheState(YbUpdateRelationCacheState *state)
{
    YbLoadTupleCache(&state->pg_attrdef_cache, AttrDefaultRelationId,
                     &YbExtractAttrDefTupleCacheKey, "pg_attrdef local cache");
    YbLoadTupleCache(&state->pg_constraint_cache, ConstraintRelationId,
                     &YbExtractConstraintTupleCacheKey,
                     "pg_constraint local cache");
}
```

and

```
static void
YbLoadTupleCache(YbTupleCache *cache, Oid relid,
                 YbTupleCacheKeyExtractor key_extractor, const char *cache_name)
{
    Assert(!(cache->rel || cache->data));
    cache->rel = heap_open(relid, AccessShareLock);
    HASHCTL ctl = {0};
    ctl.keysize = sizeof(Oid);
    ctl.entrysize = sizeof(YbTupleCacheEntry);
    cache->data = hash_create(cache_name, 32, &ctl, HASH_ELEM | HASH_BLOBS);
```

We have called `hash_create` in `YbLoadTupleCache`, but in the corresponding
`YbCleanupTupleCache` I do not see a call to `hash_destroy`:

```
static void
YbCleanupTupleCache(YbTupleCache *cache)
{
    if (!cache->rel)
        return;

    heap_close(cache->rel, AccessShareLock);
}
```

This diff fixes the memory leak by calling `hash_destroy` in
`YbCleanupTupleCache`.

Jira: DB-11181

Original commit: 18754b1 / D34845

Test Plan:
Manual test.
(1) ./bin/yb-ctl create --rf 1 --tserver_flags ysql_catalog_preload_additional_tables=true
(2) before the fix

```
yugabyte=# select used_bytes,total_bytes from pg_get_backend_memory_contexts() where name like '%local cache';
 used_bytes | total_bytes
------------+-------------
       6616 |        8192
       5576 |        8192
(2 rows)

```
(3) after the fix

```
yugabyte=# select used_bytes,total_bytes from pg_get_backend_memory_contexts() where name like '%local cache';
 used_bytes | total_bytes
------------+-------------
(0 rows)

```

Reviewers: kfranz

Reviewed By: kfranz

Subscribers: yql

Tags: #jenkins-ready

Differential Revision: https://phorge.dev.yugabyte.com/D35137
  • Loading branch information
myang2021 committed May 16, 2024
1 parent b93c21f commit 5280d2c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/postgres/src/backend/utils/cache/relcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,14 @@ YbCleanupTupleCache(YbTupleCache *cache)
if (!cache->rel)
return;

if (cache->data)
{
hash_destroy(cache->data);
cache->data = NULL;
}

heap_close(cache->rel, AccessShareLock);
cache->rel = NULL;
}

typedef struct YbUpdateRelationCacheState {
Expand Down

0 comments on commit 5280d2c

Please sign in to comment.