Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80fb2c1

Browse files
committed
Repair memory leakage while ANALYZE-ing complex index expressions.
The general design of memory management in Postgres is that intermediateresults computed by an expression are not freed until the end of the tuplecycle. For expression indexes, ANALYZE has to re-evaluate each expressionfor each of its sample rows, and it wasn't bothering to free intermediateresults until the end of processing of that index. This could lead to verysubstantial leakage if the intermediate results were large, as in a recentexample from Jakub Ouhrabka. Fix by doing ResetExprContext for each samplerow. This necessitates adding a datumCopy step to ensure that the finalexpression value isn't recycled too. Some quick testing suggests that thischange adds at worst about 10% to the time needed to analyze a table withan expression index; which is annoying, but seems a tolerable price to payto avoid unexpected out-of-memory problems.Back-patch to all supported branches.
1 parent000efc3 commit80fb2c1

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

‎src/backend/commands/analyze.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,12 @@ compute_index_stats(Relation onerel, double totalrows,
695695
{
696696
HeapTupleheapTuple=rows[rowno];
697697

698+
/*
699+
* Reset the per-tuple context each time, to reclaim any cruft
700+
* left behind by evaluating the predicate or index expressions.
701+
*/
702+
ResetExprContext(econtext);
703+
698704
/* Set up for predicate or expression evaluation */
699705
ExecStoreTuple(heapTuple,slot,InvalidBuffer, false);
700706

@@ -719,15 +725,26 @@ compute_index_stats(Relation onerel, double totalrows,
719725
isnull);
720726

721727
/*
722-
* Save just the columns we care about.
728+
* Save just the columns we care about. We copy the values
729+
* into ind_context from the estate's per-tuple context.
723730
*/
724731
for (i=0;i<attr_cnt;i++)
725732
{
726733
VacAttrStats*stats=thisdata->vacattrstats[i];
727734
intattnum=stats->attr->attnum;
728735

729-
exprvals[tcnt]=values[attnum-1];
730-
exprnulls[tcnt]=isnull[attnum-1];
736+
if (isnull[attnum-1])
737+
{
738+
exprvals[tcnt]= (Datum)0;
739+
exprnulls[tcnt]= true;
740+
}
741+
else
742+
{
743+
exprvals[tcnt]=datumCopy(values[attnum-1],
744+
stats->attrtype->typbyval,
745+
stats->attrtype->typlen);
746+
exprnulls[tcnt]= false;
747+
}
731748
tcnt++;
732749
}
733750
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp