Re: Fix var_eq_const: sum selectivity of all matching MCV entries instead of stopping at first match

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>, Damil Shahzad <shahzaddamil(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Fix var_eq_const: sum selectivity of all matching MCV entries instead of stopping at first match
Date: 2026-08-19 07:52:07
Message-ID: tencent_59DEC6387881B60C5ACE09AE6F26CB1AEA07@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

HI,  hackers
  So far, I have only found compute_scalar_stats() and compute_distinct_stats() that
generate STATISTIC_KIND_MCV statistics. The way they allocate space for and populate
most_common_vals and most_common_freqs, as well as determine their number of elements,
is the same in both cases. In other words, these two arrays always have the same number of elements.

The relevant code is briefly shown below:
```C
/* Generate MCV slot entry */

                  mcv_values = palloc_array(Datum, num_mcv);
                  mcv_freqs = palloc_array(float4, num_mcv);
                  for (i = 0; i < num_mcv; i++)
                  {
                        mcv_values[i] = datumCopy(values[track[i].first].value,
                                                              stats->attrtype->typbyval,
                                                              stats->attrtype->typlen);
                        mcv_freqs[i] = (double) track[i].count / (double) samplerows;
                  }
                  MemoryContextSwitchTo(old_context);

                  stats->stakind[slot_idx] = STATISTIC_KIND_MCV;
                  stats->staop[slot_idx] = mystats->eqopr;
                  stats->stacoll[slot_idx] = stats->attrcollid;
                  stats->stanumbers[slot_idx] = mcv_freqs;
                  stats->numnumbers[slot_idx] = num_mcv;
                  stats->stavalues[slot_idx] = mcv_values;
                  stats->numvalues[slot_idx] = num_mcv;

regards,
--
ZizhuanLiu (X-MAN) 
44973863(at)qq(dot)com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2026-08-19 07:57:55 Re: InvalidateConstraintCacheCallBack() can free fpmeta while it's in use
Previous Message Fujii Masao 2026-08-19 07:49:27 Re: pg_control_checkpoint(): add "data_checksum_version" (Pg19)?