diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2018-12-14 12:52:49 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2018-12-14 12:52:49 -0500 |
commit | 5e09280057a4c3f5db297348ea3e044c9c5f4ef8 (patch) | |
tree | a153ceede13d3b807d48d420896b6763d44c9086 /src/backend/commands/analyze.c | |
parent | 8fb569e978af3995f0dd6b0033758ec571aab0c1 (diff) | |
download | postgresql-5e09280057a4c3f5db297348ea3e044c9c5f4ef8.tar.gz postgresql-5e09280057a4c3f5db297348ea3e044c9c5f4ef8.zip |
Make pg_statistic and related code account more honestly for collations.
When we first put in collations support, we basically punted on teaching
pg_statistic, ANALYZE, and the planner selectivity functions about that.
They've just used DEFAULT_COLLATION_OID independently of the actual
collation of the data. It's time to improve that, so:
* Add columns to pg_statistic that record the specific collation associated
with each statistics slot.
* Teach ANALYZE to use the column's actual collation when comparing values
for statistical purposes, and record this in the appropriate slot. (Note
that type-specific typanalyze functions are now expected to fill
stats->stacoll with the appropriate collation, too.)
* Teach assorted selectivity functions to use the actual collation of
the stats they are looking at, instead of just assuming it's
DEFAULT_COLLATION_OID.
This should give noticeably better results in selectivity estimates for
columns with nondefault collations, at least for query clauses that use
that same collation (which would be the default behavior in most cases).
It's still true that comparisons with explicit COLLATE clauses different
from the stored data's collation won't be well-estimated, but that's no
worse than before. Also, this patch does make the first step towards
doing better with that, which is that it's now theoretically possible to
collect stats for a collation other than the column's own collation.
Patch by me; thanks to Peter Eisentraut for review.
Discussion: https://postgr.es/m/14706.1544630227@sss.pgh.pa.us
Diffstat (limited to 'src/backend/commands/analyze.c')
-rw-r--r-- | src/backend/commands/analyze.c | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c index b8445dc3728..b5a7475db9a 100644 --- a/src/backend/commands/analyze.c +++ b/src/backend/commands/analyze.c @@ -904,11 +904,22 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr) { stats->attrtypid = exprType(index_expr); stats->attrtypmod = exprTypmod(index_expr); + + /* + * If a collation has been specified for the index column, use that in + * preference to anything else; but if not, fall back to whatever we + * can get from the expression. + */ + if (OidIsValid(onerel->rd_indcollation[attnum - 1])) + stats->attrcollid = onerel->rd_indcollation[attnum - 1]; + else + stats->attrcollid = exprCollation(index_expr); } else { stats->attrtypid = attr->atttypid; stats->attrtypmod = attr->atttypmod; + stats->attrcollid = attr->attcollation; } typtuple = SearchSysCacheCopy1(TYPEOID, @@ -1553,6 +1564,11 @@ update_attstats(Oid relid, bool inh, int natts, VacAttrStats **vacattrstats) { values[i++] = ObjectIdGetDatum(stats->staop[k]); /* staopN */ } + i = Anum_pg_statistic_stacoll1 - 1; + for (k = 0; k < STATISTIC_NUM_SLOTS; k++) + { + values[i++] = ObjectIdGetDatum(stats->stacoll[k]); /* stacollN */ + } i = Anum_pg_statistic_stanumbers1 - 1; for (k = 0; k < STATISTIC_NUM_SLOTS; k++) { @@ -1993,9 +2009,8 @@ compute_distinct_stats(VacAttrStatsP stats, firstcount1 = track_cnt; for (j = 0; j < track_cnt; j++) { - /* We always use the default collation for statistics */ if (DatumGetBool(FunctionCall2Coll(&f_cmpeq, - DEFAULT_COLLATION_OID, + stats->attrcollid, value, track[j].value))) { match = true; @@ -2202,6 +2217,7 @@ compute_distinct_stats(VacAttrStatsP stats, stats->stakind[0] = STATISTIC_KIND_MCV; stats->staop[0] = mystats->eqopr; + stats->stacoll[0] = stats->attrcollid; stats->stanumbers[0] = mcv_freqs; stats->numnumbers[0] = num_mcv; stats->stavalues[0] = mcv_values; @@ -2273,8 +2289,7 @@ compute_scalar_stats(VacAttrStatsP stats, memset(&ssup, 0, sizeof(ssup)); ssup.ssup_cxt = CurrentMemoryContext; - /* We always use the default collation for statistics */ - ssup.ssup_collation = DEFAULT_COLLATION_OID; + ssup.ssup_collation = stats->attrcollid; ssup.ssup_nulls_first = false; /* @@ -2567,6 +2582,7 @@ compute_scalar_stats(VacAttrStatsP stats, 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; @@ -2682,6 +2698,7 @@ compute_scalar_stats(VacAttrStatsP stats, stats->stakind[slot_idx] = STATISTIC_KIND_HISTOGRAM; stats->staop[slot_idx] = mystats->ltopr; + stats->stacoll[slot_idx] = stats->attrcollid; stats->stavalues[slot_idx] = hist_values; stats->numvalues[slot_idx] = num_hist; @@ -2725,6 +2742,7 @@ compute_scalar_stats(VacAttrStatsP stats, stats->stakind[slot_idx] = STATISTIC_KIND_CORRELATION; stats->staop[slot_idx] = mystats->ltopr; + stats->stacoll[slot_idx] = stats->attrcollid; stats->stanumbers[slot_idx] = corrs; stats->numnumbers[slot_idx] = 1; slot_idx++; |