| From: | "Matheus Alcantara" <matheusssilv97(at)gmail(dot)com> |
|---|---|
| To: | "Alexandra Wang" <alexandra(dot)wang(dot)oss(at)gmail(dot)com>, "Tomas Vondra" <tomas(at)vondra(dot)me> |
| Cc: | "Corey Huinker" <corey(dot)huinker(at)gmail(dot)com>, <ilya(dot)evdokimov(at)tantorlabs(dot)com>, "jian he" <jian(dot)universality(at)gmail(dot)com>, <pgsql-hackers(at)lists(dot)postgresql(dot)org>, "Andrei Lepikhov" <lepihov(at)gmail(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <hs(at)cybertec(dot)at>, "Jeff Davis" <pgsql(at)j-davis(dot)com> |
| Subject: | Re: Is there value in having optimizer stats for joins/foreignkeys? |
| Date: | 2026-11-23 14:52:53 |
| Message-ID: | DN2OC5JYLPJS.7LNIFLGD6YD@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Wed Nov 18, 2026 at 5:44 AM -03, Alexandra Wang wrote:
> Hi there,
>
> Thanks to Tomas for the v9 review, to Chengpeng for the 0002 report,
> and to Ilia for the ANALYZE timings. I have addressed, or partly
> addressed, all of the feedback, and here is a brief summary of what
> changed in v10.
>
Hi Alex,
Thanks for the new version. I spent some time reviewing and testing v10,
mostly 0004, but also the preparatory patches. I didn't find any crashes
or assertion failures so far. I have some comments below:
0001
----
Looks good to me. FWIW the same code (and XXX comment) is also present
on the older branches. The patch applies cleanly to REL_19_STABLE, but
not to REL_18_STABLE and older, where the context line still has a
"(Datum) datum" cast, so we will need a different patch for older
branches.
0002
----
I couldn't find estimation problems here. MCV dimensions and stxdexpr
indexing gave identical estimates across different orderings of mixed
columns/expressions, and dump/restore round-trips the stats. A few
things though:
1) Planning regression: to your question, I think caching the parsed
list in the relcache is necessary. On optimized builds I see numbers
similar to yours, about -16% tps with 8 column-only statistics objects
and -9% with 8 (column, expression) objects.
Another minor comment: the list is also run through eval_const_expressions()
twice on this path (with a NULL root in statext_get_stxexprs() and again in
plancat.c). That's cheap compared to the parsing, but the first pass seems
unnecessary for the planner, or am I missing something?
2) pg_get_statisticsobjdef_columns() changes its return type from text
to text[] while keeping the same OID, so an older psql running \d
against a patched server prints:
"public.z2_e" ON {a,"(b + 1)"} FROM z2
This could also break user scripts that call the function. I'm wondering
if we should add a new function rather than change the existing one.
0003
----
Looks good to me.
0004
----
1) Wrong estimate with self-joins: join_mcv_pair_selectivities() checks
the other side with list_member_oid(stat->joinrels, other_reloid), which
also matches the anchor's own OID, and then join conditions and filters
are matched by attnum only. So a statistics object on t1 JOIN t2 is
applied to a t1 self-join:
create table t1 (a int, b int);
create table t2 (d int, e int);
insert into t1 select i, i % 100 from generate_series(1,100000) i;
insert into t2 select i, case when i <= 50000 then 1 else i % 100 end
from generate_series(1,100000) i;
create index on t1(a); create index on t2(d);
analyze t1, t2;
create statistics s12 (mcv) on t1.b, t2.e from t1 join t2 on t1.a = t2.d;
analyze t1;
explain analyze select * from t1 x join t1 y on x.a = y.a where y.b = 1;
-- Hash Join: rows=29596 estimated vs 1000 actual; 1010 on master
I think we need to map each planner rel to a stat position and require
joinrels[pos] to match the RTE relid on both sides.
2) Inheritance: v10 rejects tables with inheritance children, but
children can be added later. The planner never compares stat->inherit
with rte->inh, and statext_mcv_load() is called with inh=false
unconditionally. If the stats were built when the anchor had no
children and a child is added later, ANALYZE skips the object with a
WARNING but keeps the old data, which is then applied to the appendrel:
create table d (id int primary key, cat int);
insert into d select i, case when i <= 10 then 1 else 2 end
from generate_series(1,1000) i;
create table f (did int);
insert into f select (i % 10) + 1 from generate_series(1,100000) i;
analyze d, f;
create statistics sf (mcv) on d.cat from f join d on f.did = d.id;
analyze f;
create table fchild () inherits (f);
insert into fchild select (i % 990) + 11 from generate_series(1,900000) i;
analyze fchild; analyze f;
explain analyze select * from f join d on f.did = d.id where d.cat = 1;
-- rows=1000000 estimated vs 100000 actual
3) Collations: I didn't reproduce this one, I just found it by reading
the code, but find_index_for_operator() doesn't look at
rd_indcollation. With a nondeterministic collation on the join
condition and only a COLLATE "C" index on the probed column, the probes
would only find exact matches and bias the sample. This make sense?
4) A virtual generated column in the join condition is accepted, and ANALYZE
then silently produces no MCV data at all. Using the same column in the
statistics column list is rejected, so I think we should also reject virtual
generated columns in the join condition.
5) lock_join_stats_rels() in parse_utilcmd.c takes
ShareUpdateExclusiveLock on the referenced tables before the permission
check. So a user with only SELECT on someone else's table t2 can do
begin;
create statistics ... from t join t2 on ...;
-- keep the transaction open
and block VACUUM, ANALYZE and CREATE INDEX on t2, and make autovacuum
skip it, while the same user's LOCK TABLE t2 IN SHARE UPDATE EXCLUSIVE
MODE is denied. I'm wondering if the non-anchor tables only need
AccessShareLock, and the permission check should happen before locking
(RangeVarGetRelidExtended() with a callback).
6) A permanent statistics object can reference a temp table. While the
session owning the temp table is alive, ANALYZE of the anchor in any
other session fails with "cannot access temporary tables of other
sessions".
7) If the other table belongs to another user, the statistics owner
can't see their own data in pg_stats_ext, because the view requires
pg_has_role(USAGE) on the owner of every joined table, while CREATE
STATISTICS and ANALYZE only require SELECT on the non-anchor tables.
Since pg_dump reads the data from that view, a dump taken by the
statistics owner silently omits it.
8) "ON ca.*" reports "statistics creation on system columns is not
supported".
This is all that I have for now. I'll keep studying and testing the
patch. Thanks for working on this!
--
Matheus Alcantara
EDB: https://www.enterprisedb.com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Ilia Evdokimov | 2026-11-23 15:00:46 | Re: Incorrect selectivity estimates for NOT on nullable boolean columns |
| Previous Message | Bharath Rupireddy | 2026-11-23 14:22:27 | Re: Postmaster PANIC on a standby with slot synchronization enabled |