| From: | Michael Paquier <michael(at)paquier(dot)xyz> |
|---|---|
| To: | Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com> |
| Cc: | pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: Memory leak in statext_ndistinct_build() during ANALYZE |
| Date: | 2026-10-09 06:45:18 |
| Message-ID: | asiNfm_QQ6P9tEsU@paquier.xyz |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Fri, Oct 09, 2026 at 12:06:20AM +0500, Ilia Evdokimov wrote:
> ndistinct_for_combination() allocates the items, values and isnull arrays
> for the whole sample, plus the MultiSortSupport, and never frees them.
> statext_ndistinct_build() calls it once per combination of columns, so these
> allocations accumulate until the whole ndistinct object is built.
Nice catch. Indeed, that's annoying.
> The attached patch changes statext_ndistinct_build() to call
> ndistinct_for_combination() in a separate memory context and reset it after
> each combination, the same way as is already done for dependency_degree() in
> statext_dependencies_build().
Bumping default_statistics_target to 10000 and running ANALYZE in a
\watch loop, it looks like your patch is keeping the memory used under
check.
> Similar memory issues in extended statistics were fixed in [0]. The original
> report there actually used ndistinct objects, but the fixes addressed memory
> accumulated across statistics objects and in dependency_degree(); the
> per-combination allocations in ndistinct_for_combination() were not
> discussed there.
This part refers to 83772cc78e03 and ad8a166ca868 (ndistinct with
memory context reset as you are mentioning), and both got backpatched.
It sounds to me that we should do the same here, down to v14.
What you have looks OK at quick glance. I'll probably process that
next week after v19 RC1 is tagged, unless there is an objection of any
kind.
Thanks for the report and the patch!
--
Michael
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | vignesh C | 2026-10-09 06:35:24 | Re: Publication DDL can race with a concurrent UPDATE |