diff --git a/src/postgres/src/backend/utils/cache/catcache.c b/src/postgres/src/backend/utils/cache/catcache.c index fb3660a39b4a..d14201843a14 100644 --- a/src/postgres/src/backend/utils/cache/catcache.c +++ b/src/postgres/src/backend/utils/cache/catcache.c @@ -505,13 +505,17 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct) /* delink from linked list */ dlist_delete(&ct->cache_elem); + bool need_to_free_ybctid = false; /* * Free keys when we're dealing with a negative entry, normal entries just * point into tuple, allocated together with the CatCTup. + * YB Note: for normal entries we may need to free ybctid. */ if (ct->negative) CatCacheFreeKeys(cache->cc_tupdesc, cache->cc_nkeys, cache->cc_keyno, ct->keys); + else if (IsYugaByteEnabled() && ct->tuple.t_ybctid) + need_to_free_ybctid = true; #ifdef CATCACHE_STATS /* @@ -521,10 +525,16 @@ CatCacheRemoveCTup(CatCache *cache, CatCTup *ct) if (ct->negative) cache->yb_cc_size_bytes -= sizeof(CatCTup); else + { cache->yb_cc_size_bytes -= sizeof(CatCTup) + MAXIMUM_ALIGNOF + ct->tuple.t_len; + if (need_to_free_ybctid) + cache->yb_cc_size_bytes -= VARSIZE(ct->tuple.t_ybctid); + } #endif + if (need_to_free_ybctid) + pfree(DatumGetPointer(ct->tuple.t_ybctid)); pfree(ct); --cache->cc_ntup; @@ -2362,6 +2372,12 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, Datum *arguments, (const char *) dtp->t_data, dtp->t_len); HEAPTUPLE_COPY_YBCTID(dtp->t_ybctid, ct->tuple.t_ybctid); +#ifdef CATCACHE_STATS + /* HEAPTUPLE_COPY_YBCTID makes allocation for t_ybctid. */ + bool allocated_ybctid = IsYugaByteEnabled() && ct->tuple.t_ybctid; + if (allocated_ybctid) + cache->yb_cc_size_bytes += VARSIZE(ct->tuple.t_ybctid); +#endif MemoryContextSwitchTo(oldcxt); if (dtp != ntp) diff --git a/src/postgres/src/backend/utils/cache/relcache.c b/src/postgres/src/backend/utils/cache/relcache.c index c8ba1fadcc84..781e8f9ea3b3 100644 --- a/src/postgres/src/backend/utils/cache/relcache.c +++ b/src/postgres/src/backend/utils/cache/relcache.c @@ -4265,6 +4265,8 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc) pfree(relation->rd_partcheck); if (relation->rd_fdwroutine) pfree(relation->rd_fdwroutine); + if (relation->yb_table_properties) + pfree(relation->yb_table_properties); pfree(relation); }