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 1iSRSK-0006RP-NU for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2019 19:59:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iSRSJ-0008Me-4w for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2019 19:59:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iSRSI-0008Ir-Qe for pgsql-hackers@lists.postgresql.org; Wed, 06 Nov 2019 19:59:02 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iSRSF-0008IY-Op for pgsql-hackers@postgresql.org; Wed, 06 Nov 2019 19:59:02 +0000 Received: by mail-wr1-x444.google.com with SMTP id a11so27556607wra.6 for ; Wed, 06 Nov 2019 11:58:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=GRbrvbLoOAyEFm31qLWVQCvDUY7lWj/l4HzRLaoU24M=; b=TQYnnYnYS3wfaqeE0U2phBkM04PXf9e9vk1e8mIw+eK95UEuGBPOb5zGGRPbjhZaKG rg7k8aGgNnQqPWMdR117sBS/c9b3o5Xn8Vn+RuhOcjOhttBoY+vCu0yz2imi9vJ7am5f T9u4CBQb2ZLr8uYpvpA7knvQObYED2LeNIFo2dH8HcRHKiWVpYbHUCA81SeOnGXDnrnr EROqJZcLUXsXGi5zHe6tUK1pOqAV0l3TH5nd00irODpVPOKi3DOu8MnM3QWFuMFvL5O2 6kuwNqj51qM4mV3hABmKZLC98yyOcttAfwe60ACjdjiSImAehY0WfrHChtG9bMYl/TTt LHbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=GRbrvbLoOAyEFm31qLWVQCvDUY7lWj/l4HzRLaoU24M=; b=ABewKdZnpKvTp8chYLfGohJNFp87tMkOsa466WML0VzrJ4AuwEf7Vi+iOpECI25YuR JGyME2Ibh/l9VHjtLhTXWrG+Docn794GC4MP/YkpPMDqvAfjncAjxL66CQeydiyEI187 ZAkWVo8UnIhG+jcW10/WTOP6dpOj5FkhQuT4DhKhYis6e2mAymh5IhQTsA+wU2Z6D7Sh 1Su/HFsCnQfLhXIWtjrgE/eAyNChReiSSF6d4luywBSXY1ge+UeZjUFP5TDBhAo51dwc hw2phKF368GB/Of1Oj5BtE35L9ePQR/tNLYVSnjXKlX7UxRbxg9T9xOno+TETAsBbt0v fEKw== X-Gm-Message-State: APjAAAX4Ha7rlb62qcWDzQgVOdFCNGG2r0V++zQHimLcbZp6HLi8rXYu sdCrG6LAOYbA9uyo8qWMT8oIdB0E545P2tye1iC6nygwl8QwB7yFF4+fWBRRtQc0eC4nKgk4P6w mbd+kPdNNl1ChhEQWdmFufFgLLv197hixFJlWZb2FeTyi90+WQV23vJivyVzPYPL8hYoOZ/rY3y 0E+aQ8cP7Fybs= X-Google-Smtp-Source: APXvYqxGyX8lHkYd8HYyltMm6BzDMWy3TAdcI+a72GmmfjH0VltRsiR/NPU1NVLb+5ItF4vgvd7Mmw== X-Received: by 2002:adf:f744:: with SMTP id z4mr4393583wrp.205.1573070338680; Wed, 06 Nov 2019 11:58:58 -0800 (PST) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id 62sm31055533wre.38.2019.11.06.11.58.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Nov 2019 11:58:50 -0800 (PST) Date: Wed, 6 Nov 2019 20:58:49 +0100 From: Tomas Vondra To: PostgreSQL Hackers Subject: Re: Using multiple extended statistics for estimates Message-ID: <20191106195849.odhhc66xd23hw6hf@development> References: <20191028152048.jc6pqv5hb7j77ocp@development> <20191106195440.wtxsyhiwdidl37hv@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="3b4idrqtsfsg2ygd" Content-Disposition: inline In-Reply-To: <20191106195440.wtxsyhiwdidl37hv@development> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --3b4idrqtsfsg2ygd Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Wed, Nov 06, 2019 at 08:54:40PM +0100, Tomas Vondra wrote: >On Mon, Oct 28, 2019 at 04:20:48PM +0100, Tomas Vondra wrote: >>Hi, >> >>PostgreSQL 10 introduced extended statistics, allowing us to consider >>correlation between columns to improve estimates, and PostgreSQL 12 >>added support for MCV statistics. But we still had the limitation that >>we only allowed using a single extended statistics per relation, i.e. >>given a table with two extended stats >> >> CREATE TABLE t (a int, b int, c int, d int); >> CREATE STATISTICS s1 (mcv) ON a, b FROM t; >> CREATE STATISTICS s2 (mcv) ON c, d FROM t; >> >>and a query >> >> SELECT * FROM t WHERE a = 1 AND b = 1 AND c = 1 AND d = 1; >> >>we only ever used one of the statistics (and we considered them in a not >>particularly well determined order). >> >>This patch addresses this by using as many extended stats as possible, >>by adding a loop to statext_mcv_clauselist_selectivity(). In each step >>we pick the "best" applicable statistics (in the sense of covering the >>most attributes) and factor it into the oveall estimate. >> >>All this happens where we'd originally consider applying a single MCV >>list, i.e. before even considering the functional dependencies, so >>roughly like this: >> >> while () >> { >> ... apply another MCV list ... >> } >> >> ... apply functional dependencies ... >> >> >>I've both in the loop, but I think that'd be wrong - the MCV list is >>expected to contain more information about individual values (compared >>to functional deps, which are column-level). >> > >Here is a slightly polished v2 of the patch, the main difference being >that computing clause_attnums was moved to a separate function. > This time with the attachment ;-) -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --3b4idrqtsfsg2ygd Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="use-multiple-extended-stats-v2.patch" diff --git a/src/backend/statistics/extended_stats.c b/src/backend/statistics/extended_stats.c index 207ee3160e..12093151f6 100644 --- a/src/backend/statistics/extended_stats.c +++ b/src/backend/statistics/extended_stats.c @@ -1123,6 +1123,33 @@ statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, return true; } +/* + * statext_mcv_clause_attnums + * Recalculate attnums from compatible but not-yet-estimated clauses. + */ +static Bitmapset * +statext_mcv_clause_attnums(int nclauses, Bitmapset **estimatedclauses, + Bitmapset **list_attnums) +{ + int listidx; + Bitmapset *clauses_attnums = NULL; + + for (listidx = 0; listidx < nclauses; listidx++) + { + /* + * Skip clauses that have no precalculated attnums, which means it is + * either incompatible or was already used by some other statistic. + */ + if (!list_attnums[listidx]) + continue; + + if (!bms_is_member(listidx, *estimatedclauses)) + clauses_attnums = bms_add_members(clauses_attnums, list_attnums[listidx]); + } + + return clauses_attnums; +} + /* * statext_mcv_clauselist_selectivity * Estimate clauses using the best multi-column statistics. @@ -1173,11 +1200,6 @@ statext_is_compatible_clause(PlannerInfo *root, Node *clause, Index relid, * 'estimatedclauses' is an input/output parameter. We set bits for the * 0-based 'clauses' indexes we estimate for and also skip clause items that * already have a bit set. - * - * XXX If we were to use multiple statistics, this is where it would happen. - * We would simply repeat this on a loop on the "remaining" clauses, possibly - * using the already estimated clauses as conditions (and combining the values - * using conditional probability formula). */ static Selectivity statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varRelid, @@ -1188,14 +1210,7 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli Bitmapset *clauses_attnums = NULL; Bitmapset **list_attnums; int listidx; - StatisticExtInfo *stat; - List *stat_clauses; - Selectivity simple_sel, - mcv_sel, - mcv_basesel, - mcv_totalsel, - other_sel, - sel; + Selectivity sel = 1.0; /* check if there's any stats that might be useful for us. */ if (!has_stats_of_kind(rel->statlist, STATS_EXT_MCV)) @@ -1221,80 +1236,113 @@ statext_mcv_clauselist_selectivity(PlannerInfo *root, List *clauses, int varReli Node *clause = (Node *) lfirst(l); Bitmapset *attnums = NULL; + list_attnums[listidx] = NULL; + if (!bms_is_member(listidx, *estimatedclauses) && statext_is_compatible_clause(root, clause, rel->relid, &attnums)) - { list_attnums[listidx] = attnums; - clauses_attnums = bms_add_members(clauses_attnums, attnums); - } - else - list_attnums[listidx] = NULL; listidx++; } + /* + * Collect attnums from compatible clauses that were not already used + * when applying some other extended statistic. + */ + clauses_attnums = statext_mcv_clause_attnums(list_length(clauses), + estimatedclauses, + list_attnums); + /* We need at least two attributes for multivariate statistics. */ if (bms_membership(clauses_attnums) != BMS_MULTIPLE) return 1.0; - /* find the best suited statistics object for these attnums */ - stat = choose_best_statistics(rel->statlist, clauses_attnums, STATS_EXT_MCV); + /* apply as many extended statistics as possible */ + while (true) + { + StatisticExtInfo *stat; + List *stat_clauses; + Selectivity simple_sel, + mcv_sel, + mcv_basesel, + mcv_totalsel, + other_sel, + stat_sel; - /* if no matching stats could be found then we've nothing to do */ - if (!stat) - return 1.0; + /* + * Recompute attnums in the remaining clauses (we simply use the bitmaps + * computed earlier, so that we don't have to inspect the clauses again). + */ + clauses_attnums = statext_mcv_clause_attnums(list_length(clauses), + estimatedclauses, + list_attnums); - /* Ensure choose_best_statistics produced an expected stats type. */ - Assert(stat->kind == STATS_EXT_MCV); + /* We need at least two attributes for multivariate statistics. */ + if (bms_membership(clauses_attnums) != BMS_MULTIPLE) + break; - /* now filter the clauses to be estimated using the selected MCV */ - stat_clauses = NIL; + /* find the best suited statistics object for these attnums */ + stat = choose_best_statistics(rel->statlist, clauses_attnums, STATS_EXT_MCV); - listidx = 0; - foreach(l, clauses) - { - /* - * If the clause is compatible with the selected statistics, mark it - * as estimated and add it to the list to estimate. - */ - if (list_attnums[listidx] != NULL && - bms_is_subset(list_attnums[listidx], stat->keys)) + /* if no (additional) matching stats could be found then we've nothing to do */ + if (!stat) + break; + + /* Ensure choose_best_statistics produced an expected stats type. */ + Assert(stat->kind == STATS_EXT_MCV); + + /* now filter the clauses to be estimated using the selected MCV */ + stat_clauses = NIL; + + listidx = 0; + foreach(l, clauses) { - stat_clauses = lappend(stat_clauses, (Node *) lfirst(l)); - *estimatedclauses = bms_add_member(*estimatedclauses, listidx); + /* + * If the clause is compatible with the selected statistics, mark it + * as estimated and add it to the list to estimate. + */ + if (list_attnums[listidx] != NULL && + bms_is_subset(list_attnums[listidx], stat->keys)) + { + stat_clauses = lappend(stat_clauses, (Node *) lfirst(l)); + *estimatedclauses = bms_add_member(*estimatedclauses, listidx); + } + + listidx++; } - listidx++; - } + /* + * First compute "simple" selectivity, i.e. without the extended + * statistics, and essentially assuming independence of the + * columns/clauses. We'll then use the various selectivities computed from + * MCV list to improve it. + */ + simple_sel = clauselist_selectivity_simple(root, stat_clauses, varRelid, + jointype, sjinfo, NULL); - /* - * First compute "simple" selectivity, i.e. without the extended - * statistics, and essentially assuming independence of the - * columns/clauses. We'll then use the various selectivities computed from - * MCV list to improve it. - */ - simple_sel = clauselist_selectivity_simple(root, stat_clauses, varRelid, - jointype, sjinfo, NULL); + /* + * Now compute the multi-column estimate from the MCV list, along with the + * other selectivities (base & total selectivity). + */ + mcv_sel = mcv_clauselist_selectivity(root, stat, stat_clauses, varRelid, + jointype, sjinfo, rel, + &mcv_basesel, &mcv_totalsel); - /* - * Now compute the multi-column estimate from the MCV list, along with the - * other selectivities (base & total selectivity). - */ - mcv_sel = mcv_clauselist_selectivity(root, stat, stat_clauses, varRelid, - jointype, sjinfo, rel, - &mcv_basesel, &mcv_totalsel); + /* Estimated selectivity of values not covered by MCV matches */ + other_sel = simple_sel - mcv_basesel; + CLAMP_PROBABILITY(other_sel); - /* Estimated selectivity of values not covered by MCV matches */ - other_sel = simple_sel - mcv_basesel; - CLAMP_PROBABILITY(other_sel); + /* The non-MCV selectivity can't exceed the 1 - mcv_totalsel. */ + if (other_sel > 1.0 - mcv_totalsel) + other_sel = 1.0 - mcv_totalsel; - /* The non-MCV selectivity can't exceed the 1 - mcv_totalsel. */ - if (other_sel > 1.0 - mcv_totalsel) - other_sel = 1.0 - mcv_totalsel; + /* Overall selectivity is the combination of MCV and non-MCV estimates. */ + stat_sel = mcv_sel + other_sel; + CLAMP_PROBABILITY(stat_sel); - /* Overall selectivity is the combination of MCV and non-MCV estimates. */ - sel = mcv_sel + other_sel; - CLAMP_PROBABILITY(sel); + /* Factor the estimate from this MCV to the oveall estimate. */ + sel *= stat_sel; + } return sel; } --3b4idrqtsfsg2ygd--