Previously, examine_simple_variable() would return early when a subquery or CTE used GROUP BY or DISTINCT. It could detect uniqueness for single-column cases, but for multi-column GROUP BY or DISTINCT, selectivity estimation fell back on 1/DEFAULT_NUM_DISTINCT (1/200). This produced wildly inaccurate estimates for filters and joins on such columns, often leading the planner to choose nested loop joins where hash joins would be far better. This was a significant factor in poor TPC-DS benchmark performance. For DISTINCT or GROUP BY key columns that are simple Vars, we now recurse into the subquery to obtain the base table's stadistinct, which remains valid after grouping (the set of distinct values is preserved). However, MCV frequencies, histograms, and correlation data are not valid because GROUP BY and DISTINCT change the frequency distribution of key columns. So we strip all stats slots from the copied stats tuple, causing callers like var_eq_const() to use the 1/ndistinct estimate instead. If stadistinct is stored as a negative value (a fraction of the base table's row count), we convert it to an absolute count so it is not misinterpreted relative to the subquery's output row count. stanullfrac is adjusted too, since grouping collapses NULLs. For a single grouping key, at most one NULL group survives, so the null fraction is 1/(ndistinct+1). For multiple grouping keys the null fraction depends on the joint distribution of the keys, which we don't have, so we approximate it as zero; NULLs collapse far more aggressively than non-NULLs, so the real fraction is well below the base table's, and erring low keeps estimates on the hash-join-favoring side. Non-key columns (e.g., aggregate outputs) continue to get no stats, same as before. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com> Discussion: https://postgr.es/m/CAMbWs49rWYrecgreDhKsfx3VSDW=qo35s+iAmgGu=wpARrM8_g@mail.gmail.com |
||
|---|---|---|
| .github | ||
| config | ||
| contrib | ||
| doc | ||
| src | ||
| .dir-locals.el | ||
| .editorconfig | ||
| .git-blame-ignore-revs | ||
| .gitattributes | ||
| .gitignore | ||
| .mailmap | ||
| aclocal.m4 | ||
| configure | ||
| configure.ac | ||
| COPYRIGHT | ||
| GNUmakefile.in | ||
| HISTORY | ||
| Makefile | ||
| meson.build | ||
| meson_options.txt | ||
| README.md | ||
PostgreSQL Database Management System
This directory contains the source code distribution of the PostgreSQL database management system.
PostgreSQL is an advanced object-relational database management system that supports an extended subset of the SQL standard, including transactions, foreign keys, subqueries, triggers, user-defined types and functions. This distribution also contains C language bindings.
Copyright and license information can be found in the file COPYRIGHT.
General documentation about this version of PostgreSQL can be found at https://www.postgresql.org/docs/devel/. In particular, information about building PostgreSQL from the source code can be found at https://www.postgresql.org/docs/devel/installation.html.
The latest version of this software, and related software, may be obtained at https://www.postgresql.org/download/. For more information look at our web site located at https://www.postgresql.org/.