mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-27 08:39:28 +08:00
Repair memory leakage while ANALYZE-ing complex index expressions.
The general design of memory management in Postgres is that intermediate results computed by an expression are not freed until the end of the tuple cycle. For expression indexes, ANALYZE has to re-evaluate each expression for each of its sample rows, and it wasn't bothering to free intermediate results until the end of processing of that index. This could lead to very substantial leakage if the intermediate results were large, as in a recent example from Jakub Ouhrabka. Fix by doing ResetExprContext for each sample row. This necessitates adding a datumCopy step to ensure that the final expression value isn't recycled too. Some quick testing suggests that this change adds at worst about 10% to the time needed to analyze a table with an expression index; which is annoying, but seems a tolerable price to pay to avoid unexpected out-of-memory problems. Back-patch to all supported branches.
This commit is contained in:
parent
000efc3dfd
commit
80fb2c1f40
@ -695,6 +695,12 @@ compute_index_stats(Relation onerel, double totalrows,
|
|||||||
{
|
{
|
||||||
HeapTuple heapTuple = rows[rowno];
|
HeapTuple heapTuple = rows[rowno];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Reset the per-tuple context each time, to reclaim any cruft
|
||||||
|
* left behind by evaluating the predicate or index expressions.
|
||||||
|
*/
|
||||||
|
ResetExprContext(econtext);
|
||||||
|
|
||||||
/* Set up for predicate or expression evaluation */
|
/* Set up for predicate or expression evaluation */
|
||||||
ExecStoreTuple(heapTuple, slot, InvalidBuffer, false);
|
ExecStoreTuple(heapTuple, slot, InvalidBuffer, false);
|
||||||
|
|
||||||
@ -719,15 +725,26 @@ compute_index_stats(Relation onerel, double totalrows,
|
|||||||
isnull);
|
isnull);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Save just the columns we care about.
|
* Save just the columns we care about. We copy the values
|
||||||
|
* into ind_context from the estate's per-tuple context.
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < attr_cnt; i++)
|
for (i = 0; i < attr_cnt; i++)
|
||||||
{
|
{
|
||||||
VacAttrStats *stats = thisdata->vacattrstats[i];
|
VacAttrStats *stats = thisdata->vacattrstats[i];
|
||||||
int attnum = stats->attr->attnum;
|
int attnum = stats->attr->attnum;
|
||||||
|
|
||||||
exprvals[tcnt] = values[attnum - 1];
|
if (isnull[attnum - 1])
|
||||||
exprnulls[tcnt] = isnull[attnum - 1];
|
{
|
||||||
|
exprvals[tcnt] = (Datum) 0;
|
||||||
|
exprnulls[tcnt] = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exprvals[tcnt] = datumCopy(values[attnum - 1],
|
||||||
|
stats->attrtype->typbyval,
|
||||||
|
stats->attrtype->typlen);
|
||||||
|
exprnulls[tcnt] = false;
|
||||||
|
}
|
||||||
tcnt++;
|
tcnt++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user