From: Mark Dilger <hornschnorter@gmail.com>
To: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: Re: Using multiple extended statistics for estimates
Date: Wed, 13 Nov 2019 10:04:36 -0800
Message-ID: <2e183f8d-1304-6024-90de-1017a861a235@gmail.com> (raw)
In-Reply-To: <20191113152823.jdpertc73gfo2mk7@development>
References: <20191028152048.jc6pqv5hb7j77ocp@development>
<20191106195440.wtxsyhiwdidl37hv@development>
<20191106195849.odhhc66xd23hw6hf@development>
<97932e1d-53f1-bad3-efc2-1d98cab70727@gmail.com>
<0da76f1f-5f9c-3ce3-3ac6-042d2a18430a@gmail.com>
<20191110173428.w5klequcmb6gawaa@development>
<20191113152823.jdpertc73gfo2mk7@development>
On 11/13/19 7:28 AM, Tomas Vondra wrote:
> Hi,
>
> here's an updated patch, with some minor tweaks based on the review and
> added tests (I ended up reworking those a bit, to make them more like
> the existing ones).
Thanks, Tomas, for the new patch set!
Attached are my review comments so far, in the form of a patch applied
on top of yours.
--
Mark Dilger
Attachments:code_review.patch (text/x-patch, 5.8K) downloaddiff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.cindex a6ca11c675..ccf6e41b9c 100644--- a/src/backend/statistics/dependencies.c+++ b/src/backend/statistics/dependencies.c@@ -850,7 +850,8 @@ dependency_is_compatible_clause(Node *clause, Index relid, AttrNumber *attnum)
* find the strongest dependency on the attributes
*
* When applying functional dependencies, we start with the strongest
- * dependencies. That is, we select the dependency that:+ * dependencies. That is, we select the best dependency in the following+ * descending order of preference:
*
* (a) has all attributes covered by equality clauses
*
@@ -860,6 +861,9 @@ dependency_is_compatible_clause(Node *clause, Index relid, AttrNumber *attnum)
*
* This guarantees that we eliminate the most redundant conditions first
* (see the comment in dependencies_clauselist_selectivity).
+ *+ * TODO: Rename 'dependencies', as that name is what you'd expect for an+ * argument of type (MVDependencies *), not (MVDependencies **)
*/
static MVDependency *
find_strongest_dependency(MVDependencies **dependencies, int ndependencies,
@@ -897,11 +901,14 @@ find_strongest_dependency(MVDependencies **dependencies, int ndependencies,
/* also skip weaker dependencies when attribute count matches */
if (strongest->nattributes == dependency->nattributes &&
- strongest->degree > dependency->degree)+ strongest->degree > dependency->degree) /* TODO: Why not ">=" here? */
continue;
}
/*
+ * TODO: As coded, this dependency is "at least as strong as", not+ * necessarily "stronger".+ *
* this dependency is stronger, but we must still check that it's
* fully matched to these attnums. We perform this check last as it's
* slightly more expensive than the previous checks.
@@ -942,7 +949,7 @@ find_strongest_dependency(MVDependencies **dependencies, int ndependencies,
*/
Selectivity
dependencies_clauselist_selectivity(PlannerInfo *root,
- List *clauses,+ const List *clauses,
int varRelid,
JoinType jointype,
SpecialJoinInfo *sjinfo,
@@ -1084,6 +1091,11 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
* to a different Const in another clause then no rows will match
* anyway. If it happens to be compared to the same Const, then
* ignoring the additional clause is just the thing to do.
+ *+ * TODO: Improve this code comment. Specifically, why would we+ * ignore that no rows will match? It seems that such a discovery+ * would allow us to return an estimate of 0 rows, and that would+ * be useful.
*/
if (dependency_implies_attribute(dependency,
list_attnums[listidx]))
diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.cindex b55799b8b1..a1798a6b91 100644--- a/src/backend/statistics/extended_stats.c+++ b/src/backend/statistics/extended_stats.c@@ -1202,7 +1202,7 @@ statext_mcv_remaining_attnums(int nclauses, Bitmapset **estimatedclauses,
* already have a bit set.
*/
static Selectivity
-statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,+statext_mcv_clauselist_selectivity(PlannerInfo *root, const List *clauses, int varRelid,
JoinType jointype, SpecialJoinInfo *sjinfo,
RelOptInfo *rel, Bitmapset **estimatedclauses)
{
@@ -1340,7 +1340,7 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli
* Estimate clauses using the best multi-column statistics.
*/
Selectivity
-statext_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid,+statext_clauselist_selectivity(PlannerInfo *root, const List *clauses, int varRelid,
JoinType jointype, SpecialJoinInfo *sjinfo,
RelOptInfo *rel, Bitmapset **estimatedclauses)
{
diff --git a/src/include/statistics/statistics.h b/src/include/statistics/statistics.hindex 588b6738b2..ebac910d72 100644--- a/src/include/statistics/statistics.h+++ b/src/include/statistics/statistics.h@@ -104,14 +104,14 @@ extern int ComputeExtStatisticsRows(Relation onerel,
int natts, VacAttrStats **stats);
extern bool statext_is_kind_built(HeapTuple htup, char kind);
extern Selectivity dependencies_clauselist_selectivity(PlannerInfo *root,
- List *clauses,+ const List *clauses,
int varRelid,
JoinType jointype,
SpecialJoinInfo *sjinfo,
RelOptInfo *rel,
Bitmapset **estimatedclauses);
extern Selectivity statext_clauselist_selectivity(PlannerInfo *root,
- List *clauses,+ const List *clauses,
int varRelid,
JoinType jointype,
SpecialJoinInfo *sjinfo,
diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.outindex a35d8f2c2e..1892a856f9 100644--- a/src/test/regress/expected/stats_ext.out+++ b/src/test/regress/expected/stats_ext.out@@ -451,7 +451,7 @@ CREATE TABLE functional_dependencies_multi (
c INTEGER,
d INTEGER
);
--- +--
INSERT INTO functional_dependencies_multi (a, b, c, d)
SELECT
mod(i,7),
@@ -812,7 +812,7 @@ CREATE TABLE mcv_lists_multi (
c INTEGER,
d INTEGER
);
--- +--
INSERT INTO mcv_lists_multi (a, b, c, d)
SELECT
mod(i,5),
diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sqlindex 69c3f726e6..bf700204d2 100644--- a/src/test/regress/sql/stats_ext.sql+++ b/src/test/regress/sql/stats_ext.sql@@ -296,7 +296,7 @@ CREATE TABLE functional_dependencies_multi (
d INTEGER
);
--- +--
INSERT INTO functional_dependencies_multi (a, b, c, d)
SELECT
mod(i,7),
@@ -532,7 +532,7 @@ CREATE TABLE mcv_lists_multi (
d INTEGER
);
--- +--
INSERT INTO mcv_lists_multi (a, b, c, d)
SELECT
mod(i,5),
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: hornschnorter@gmail.com, tomas.vondra@2ndquadrant.com
Subject: Re: Using multiple extended statistics for estimates
In-Reply-To: <2e183f8d-1304-6024-90de-1017a861a235@gmail.com>
References: <20191028152048.jc6pqv5hb7j77ocp@development> <20191106195440.wtxsyhiwdidl37hv@development> <20191106195849.odhhc66xd23hw6hf@development> <97932e1d-53f1-bad3-efc2-1d98cab70727@gmail.com> <0da76f1f-5f9c-3ce3-3ac6-042d2a18430a@gmail.com> <20191110173428.w5klequcmb6gawaa@development> <20191113152823.jdpertc73gfo2mk7@development> <2e183f8d-1304-6024-90de-1017a861a235@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox