Re: Poor row estimates from planner, stat `most_common_elems` sometimes missing for a text[] column
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Matt Long <matt@mattlong.org>
Cc: Mark Frost <FROSTMAR@uk.ibm.com>,
"pgsql-performance@lists.postgresql.org"
<pgsql-performance@lists.postgresql.org>
Date: 2025-09-08T23:37:01Z
Lists: pgsql-performance
Attachments
- v2-0001-Track-the-maximum-possible-frequency-of-non-MCE-a.patch (text/x-diff) patch v2-0001
Matt Long <matt@mattlong.org> writes: > Not to let perfect be the enemy of better, but we're facing a variant of > this issue that would not be addressed by the proposed patch. > ... > In this case, the effects of the proposed patch are not applied since the > most_common_elems array is not empty. I'm not a statistician, so maybe this > wouldn't be valid, but it seems like using the highest frequency of an > element that did not qualify for the mce list instead of the 0.5% default > frequency could be an elegant, but more invasive solution. Yeah, I think you are quite right: we can apply this idea not only when the MCE list is empty, but whenever we didn't have to truncate the MCE list. In that case we know there are no additional element values that exceed the cutoff frequency, and that's what the selectivity functions want to know. Nosing around in the code that uses STATISTIC_KIND_MCELEM entries, I spotted two additional issues that the attached v2 patch addresses: * ts_typanalyze/ts_selfuncs have code essentially identical to the array case, and should receive the same treatment. * The selectivity functions believe that the upper bound on the frequency of non-MCEs is minfreq / 2, not the stored minfreq. This seems like complete brain fade: there could easily be elements with frequency just less than minfreq, and probably are if the data distribution follows Zipf's law. I did not dig into the git history, but I wonder if the divide-by-two business predates the introduction of the lossy-counting algorithm, and if so whether it was less insane with the original collection algorithm. In any case, this patch removes the divisions by 2, and makes some nearby cosmetic improvements. Many thanks for the suggestion! regards, tom lane
Commits
-
Track the maximum possible frequency of non-MCE array elements.
- 261f89a976bf 19 (unreleased) landed
-
Collect and use element-frequency statistics for arrays.
- 0e5e167aaea4 9.2.0 cited