Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUx0V-0008Gv-9A for pgsql-hackers@arkaria.postgresql.org; Wed, 13 Nov 2019 18:04:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iUx0U-0007uS-4G for pgsql-hackers@arkaria.postgresql.org; Wed, 13 Nov 2019 18:04:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iUx0T-0007uL-Mz for pgsql-hackers@lists.postgresql.org; Wed, 13 Nov 2019 18:04:41 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iUx0R-0001oA-6u for pgsql-hackers@postgresql.org; Wed, 13 Nov 2019 18:04:40 +0000 Received: by mail-pf1-x435.google.com with SMTP id s5so2156263pfh.9 for ; Wed, 13 Nov 2019 10:04:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=+15wEa1GLnxiBIAySaEIREggU74dLqLcZDB6DNitRJY=; b=Xj+G/NGcKseOo96P3SMvOhIzpwZpMTKQJ54uN2XJxQwKU882m44KomNYqB5bp1o0qy KYYRUW8G1wFLMrez6/LOEGz9I9ebPzPTvO+7CBl4wVzTkHb3m+K5fiflLzz7nI0kWrbC DPu0IWxtPNDASWkz5nYtP3bVwer8veHdEiPkMNBPWcyBO9f+6h1ghelV09g8h5999vNy SmrXhR2ZX2Rwhvnaon1hgDnIX2LagSig5V+7u7athiX9c2+8WlSZNQGAaURoCdjzRGgU urLwZxbd8pDNHGoqVdAa84lhHHAln8NWcasRDdfTXJIg6VVcf1MAfep+KNL46otdR0Z+ AqYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=+15wEa1GLnxiBIAySaEIREggU74dLqLcZDB6DNitRJY=; b=FYW5sSL8FU0u1dFPIJ9vV3HfkEnCwLuSYHk22FOx1WtrvaS5LJpvw3f4qh1FI318Ak 7siupTt12af9cA5uiIj2tvAci+HbO8qMiE5DD1KaHXhlNPSp4MpER4F2Mx/Q4QXyO92m HaHimwf60CNSpmm4wlfCtmpSBKdsKsL0I5UGUr500AzkFJ+jJiasl1beRX9VGNqSfeLK iOFDR0xynqoLbm4qMBYmHIK4AvIEOTpzTSM0ikkS/I954Nc8PKHhBS26IoUN3e8MPphQ 63wzPu65sW2o/dPsUiWXOTJ89dNXd3Y1uGNeH51oGkhkfUuNQ/y8TltLGvdGajRah58n KUqw== X-Gm-Message-State: APjAAAVnvcNN7AW2SI7/oHT9VDlH+SCLZazF+HgzWlTRdc5fmpMFNK+H Q04ee5Kr0K+xE6HFJS31g2HZsyqD X-Google-Smtp-Source: APXvYqwGVaFelUb1CY1G8bz/M/NNnTwqGN10XIN+YPKPWNtCxpEPAooYEp+nsgN7L5lpw4EXgeWZ3g== X-Received: by 2002:a17:90a:b28c:: with SMTP id c12mr4444052pjr.22.1573668277733; Wed, 13 Nov 2019 10:04:37 -0800 (PST) Received: from [192.168.0.17] (24-113-193-150.wavecable.com. [24.113.193.150]) by smtp.gmail.com with ESMTPSA id 23sm2998781pgw.8.2019.11.13.10.04.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Nov 2019 10:04:37 -0800 (PST) Subject: Re: Using multiple extended statistics for estimates To: Tomas Vondra Cc: PostgreSQL Hackers 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> From: Mark Dilger Message-ID: <2e183f8d-1304-6024-90de-1017a861a235@gmail.com> Date: Wed, 13 Nov 2019 10:04:36 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: <20191113152823.jdpertc73gfo2mk7@development> Content-Type: multipart/mixed; boundary="------------67324CD71823A96DE20BF6E8" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------67324CD71823A96DE20BF6E8 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit 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 --------------67324CD71823A96DE20BF6E8 Content-Type: text/x-patch; name="code_review.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="code_review.patch" diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c index 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.c index 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.h index 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.out index 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.sql index 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), --------------67324CD71823A96DE20BF6E8--