mirror of
https://github.com/postgres/postgres.git
synced 2025-07-14 08:21:07 +03:00
Prevent memory leaks associated with relcache rd_partcheck structures.
The original coding of generate_partition_qual() just copied the list of predicate expressions into the global CacheMemoryContext, making it effectively impossible to clean up when the owning relcache entry is destroyed --- the relevant code in RelationDestroyRelation() only managed to free the topmost List header :-(. This resulted in a session-lifespan memory leak whenever a table partition's relcache entry is rebuilt. Fortunately, that's not normally a large data structure, and rebuilds shouldn't occur all that often in production situations; but this is still a bug worth fixing back to v10 where the code was introduced. To fix, put the cached expression tree into its own small memory context, as we do with other complicated substructures of relcache entries. Also, deal more honestly with the case that a partition has an empty partcheck list; while that probably isn't a case that's very interesting for production use, it's legal. In passing, clarify comments about how partitioning-related relcache data structures are managed, and add some Asserts that we're not leaking old copies when we overwrite these data fields. Amit Langote and Tom Lane Discussion: https://postgr.es/m/7961.1552498252@sss.pgh.pa.us
This commit is contained in:
20
src/backend/utils/cache/relcache.c
vendored
20
src/backend/utils/cache/relcache.c
vendored
@ -1191,11 +1191,15 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
|
||||
}
|
||||
else
|
||||
{
|
||||
relation->rd_partkeycxt = NULL;
|
||||
relation->rd_partkey = NULL;
|
||||
relation->rd_partkeycxt = NULL;
|
||||
relation->rd_partdesc = NULL;
|
||||
relation->rd_pdcxt = NULL;
|
||||
}
|
||||
/* ... but partcheck is not loaded till asked for */
|
||||
relation->rd_partcheck = NIL;
|
||||
relation->rd_partcheckvalid = false;
|
||||
relation->rd_partcheckcxt = NULL;
|
||||
|
||||
/*
|
||||
* if it's an index, initialize index-related information
|
||||
@ -2285,8 +2289,8 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
|
||||
MemoryContextDelete(relation->rd_partkeycxt);
|
||||
if (relation->rd_pdcxt)
|
||||
MemoryContextDelete(relation->rd_pdcxt);
|
||||
if (relation->rd_partcheck)
|
||||
pfree(relation->rd_partcheck);
|
||||
if (relation->rd_partcheckcxt)
|
||||
MemoryContextDelete(relation->rd_partcheckcxt);
|
||||
if (relation->rd_fdwroutine)
|
||||
pfree(relation->rd_fdwroutine);
|
||||
pfree(relation);
|
||||
@ -5617,18 +5621,20 @@ load_relcache_init_file(bool shared)
|
||||
* format is complex and subject to change). They must be rebuilt if
|
||||
* needed by RelationCacheInitializePhase3. This is not expected to
|
||||
* be a big performance hit since few system catalogs have such. Ditto
|
||||
* for RLS policy data, index expressions, predicates, exclusion info,
|
||||
* and FDW info.
|
||||
* for RLS policy data, partition info, index expressions, predicates,
|
||||
* exclusion info, and FDW info.
|
||||
*/
|
||||
rel->rd_rules = NULL;
|
||||
rel->rd_rulescxt = NULL;
|
||||
rel->trigdesc = NULL;
|
||||
rel->rd_rsdesc = NULL;
|
||||
rel->rd_partkeycxt = NULL;
|
||||
rel->rd_partkey = NULL;
|
||||
rel->rd_pdcxt = NULL;
|
||||
rel->rd_partkeycxt = NULL;
|
||||
rel->rd_partdesc = NULL;
|
||||
rel->rd_pdcxt = NULL;
|
||||
rel->rd_partcheck = NIL;
|
||||
rel->rd_partcheckvalid = false;
|
||||
rel->rd_partcheckcxt = NULL;
|
||||
rel->rd_indexprs = NIL;
|
||||
rel->rd_indpred = NIL;
|
||||
rel->rd_exclops = NULL;
|
||||
|
Reference in New Issue
Block a user