Fix per-relation memory leakage in autovacuum.
PgStat_StatTabEntry and AutoVacOpts structs were leaked until the end of the autovacuum worker's run, which is bad news if there are a lot of relations in the database. Note: pfree'ing the PgStat_StatTabEntry structs here seems a bit risky, because pgstat_fetch_stat_tabentry_ext does not guarantee anything about whether its result is long-lived. It appears okay so long as autovacuum forces PGSTAT_FETCH_CONSISTENCY_NONE, but I think that API could use a re-think. Also ensure that the VacuumRelation structure passed to vacuum() is in recoverable storage. Back-patch to v15 where we started to manage table statistics this way. (The AutoVacOpts leakage is probably older, but I'm not excited enough to worry about just that part.) Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/285483.1746756246@sss.pgh.pa.us Backpatch-through: 15
This commit is contained in:
parent
0e0174b497
commit
13d21b48a3
@ -2154,6 +2154,12 @@ do_autovacuum(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Release stuff to avoid per-relation leakage */
|
||||||
|
if (relopts)
|
||||||
|
pfree(relopts);
|
||||||
|
if (tabentry)
|
||||||
|
pfree(tabentry);
|
||||||
}
|
}
|
||||||
|
|
||||||
table_endscan(relScan);
|
table_endscan(relScan);
|
||||||
@ -2170,7 +2176,8 @@ do_autovacuum(void)
|
|||||||
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
|
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
|
||||||
PgStat_StatTabEntry *tabentry;
|
PgStat_StatTabEntry *tabentry;
|
||||||
Oid relid;
|
Oid relid;
|
||||||
AutoVacOpts *relopts = NULL;
|
AutoVacOpts *relopts;
|
||||||
|
bool free_relopts = false;
|
||||||
bool dovacuum;
|
bool dovacuum;
|
||||||
bool doanalyze;
|
bool doanalyze;
|
||||||
bool wraparound;
|
bool wraparound;
|
||||||
@ -2188,7 +2195,9 @@ do_autovacuum(void)
|
|||||||
* main rel
|
* main rel
|
||||||
*/
|
*/
|
||||||
relopts = extract_autovac_opts(tuple, pg_class_desc);
|
relopts = extract_autovac_opts(tuple, pg_class_desc);
|
||||||
if (relopts == NULL)
|
if (relopts)
|
||||||
|
free_relopts = true;
|
||||||
|
else
|
||||||
{
|
{
|
||||||
av_relation *hentry;
|
av_relation *hentry;
|
||||||
bool found;
|
bool found;
|
||||||
@ -2209,6 +2218,12 @@ do_autovacuum(void)
|
|||||||
/* ignore analyze for toast tables */
|
/* ignore analyze for toast tables */
|
||||||
if (dovacuum)
|
if (dovacuum)
|
||||||
table_oids = lappend_oid(table_oids, relid);
|
table_oids = lappend_oid(table_oids, relid);
|
||||||
|
|
||||||
|
/* Release stuff to avoid leakage */
|
||||||
|
if (free_relopts)
|
||||||
|
pfree(relopts);
|
||||||
|
if (tabentry)
|
||||||
|
pfree(tabentry);
|
||||||
}
|
}
|
||||||
|
|
||||||
table_endscan(relScan);
|
table_endscan(relScan);
|
||||||
@ -2572,6 +2587,8 @@ deleted:
|
|||||||
VacuumCostLimit = stdVacuumCostLimit;
|
VacuumCostLimit = stdVacuumCostLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list_free(table_oids);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Perform additional work items, as requested by backends.
|
* Perform additional work items, as requested by backends.
|
||||||
*/
|
*/
|
||||||
@ -2749,8 +2766,8 @@ deleted2:
|
|||||||
/*
|
/*
|
||||||
* extract_autovac_opts
|
* extract_autovac_opts
|
||||||
*
|
*
|
||||||
* Given a relation's pg_class tuple, return the AutoVacOpts portion of
|
* Given a relation's pg_class tuple, return a palloc'd copy of the
|
||||||
* reloptions, if set; otherwise, return NULL.
|
* AutoVacOpts portion of reloptions, if set; otherwise, return NULL.
|
||||||
*
|
*
|
||||||
* Note: callers do not have a relation lock on the table at this point,
|
* Note: callers do not have a relation lock on the table at this point,
|
||||||
* so the table could have been dropped, and its catalog rows gone, after
|
* so the table could have been dropped, and its catalog rows gone, after
|
||||||
@ -2799,6 +2816,7 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
|
|||||||
autovac_table *tab = NULL;
|
autovac_table *tab = NULL;
|
||||||
bool wraparound;
|
bool wraparound;
|
||||||
AutoVacOpts *avopts;
|
AutoVacOpts *avopts;
|
||||||
|
bool free_avopts = false;
|
||||||
|
|
||||||
/* fetch the relation's relcache entry */
|
/* fetch the relation's relcache entry */
|
||||||
classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
|
classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
|
||||||
@ -2811,8 +2829,10 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
|
|||||||
* main table reloptions if the toast table itself doesn't have.
|
* main table reloptions if the toast table itself doesn't have.
|
||||||
*/
|
*/
|
||||||
avopts = extract_autovac_opts(classTup, pg_class_desc);
|
avopts = extract_autovac_opts(classTup, pg_class_desc);
|
||||||
if (classForm->relkind == RELKIND_TOASTVALUE &&
|
if (avopts)
|
||||||
avopts == NULL && table_toast_map != NULL)
|
free_avopts = true;
|
||||||
|
else if (classForm->relkind == RELKIND_TOASTVALUE &&
|
||||||
|
table_toast_map != NULL)
|
||||||
{
|
{
|
||||||
av_relation *hentry;
|
av_relation *hentry;
|
||||||
bool found;
|
bool found;
|
||||||
@ -2921,6 +2941,8 @@ table_recheck_autovac(Oid relid, HTAB *table_toast_map,
|
|||||||
avopts->vacuum_cost_delay >= 0));
|
avopts->vacuum_cost_delay >= 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (free_avopts)
|
||||||
|
pfree(avopts);
|
||||||
heap_freetuple(classTup);
|
heap_freetuple(classTup);
|
||||||
return tab;
|
return tab;
|
||||||
}
|
}
|
||||||
@ -2952,6 +2974,10 @@ recheck_relation_needs_vacanalyze(Oid relid,
|
|||||||
effective_multixact_freeze_max_age,
|
effective_multixact_freeze_max_age,
|
||||||
dovacuum, doanalyze, wraparound);
|
dovacuum, doanalyze, wraparound);
|
||||||
|
|
||||||
|
/* Release tabentry to avoid leakage */
|
||||||
|
if (tabentry)
|
||||||
|
pfree(tabentry);
|
||||||
|
|
||||||
/* ignore ANALYZE for toast tables */
|
/* ignore ANALYZE for toast tables */
|
||||||
if (classForm->relkind == RELKIND_TOASTVALUE)
|
if (classForm->relkind == RELKIND_TOASTVALUE)
|
||||||
*doanalyze = false;
|
*doanalyze = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user