pgsql: Release memory allocated by ndistinct_for_combination()

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Release memory allocated by ndistinct_for_combination()
Date: 2026-10-14 01:17:42
Message-ID: E1xGnck-00000001WJA-1W4D@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Release memory allocated by ndistinct_for_combination()

Building extended statistics of type "ndistinct" calls
ndistinct_for_combination() once for every combination of columns. Each
call allocates arrays for the whole sample and never frees them, with
memory accumulating until the whole statistics object is built. The
sample has 300 multiplied by target rows. With large statistics targets
(default_statistics_target up to 10k), this can add up to gigabytes
worth of memory, useless once a combination is computed.

ndistinct_for_combination() is now called in a dedicated memory context,
reset after each combination is computed. We only need the estimated
number of distinct values, so there is no need to copy anything out of
the memory context.

Similar fixes have been done in 83772cc78e03 and ad8a166ca868 for other
areas of the extended stats computations.

Author: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
Discussion: https://postgr.es/m/20d6689e-f490-4ca7-a92f-e92f691ba07e@tantorlabs.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/205f40f5b27392b54a7ffba3059522ccc2a5d269

Modified Files
--------------
src/backend/statistics/mvdistinct.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Fujii Masao 2026-10-14 02:13:36 pgsql: Add missing FreeDir() in CheckTablespaceDirectory()
Previous Message Tom Lane 2026-10-13 21:53:49 pgsql: Delay qp_callback processing till after join removal.