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 1iSfbR-0003JN-NT for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Nov 2019 11:05:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iSfbO-0003Yh-Vy for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Nov 2019 11:05:22 +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 1iSfbO-0003YO-FG for pgsql-hackers@lists.postgresql.org; Thu, 07 Nov 2019 11:05:22 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iSfbK-0001Yn-J3 for pgsql-hackers@postgresql.org; Thu, 07 Nov 2019 11:05:21 +0000 Received: by mail-wm1-x32b.google.com with SMTP id l17so1199681wmh.0 for ; Thu, 07 Nov 2019 03:05:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=a0m1MUgyNJv0WgSFmaA8BrDolKVJgHRks3YElsMZu9U=; b=AeSl7l62Ym4DGd2csWXgTOFwib3BvrYo06gYE8m237bBK/ykdmzU+uonrUS6U6rCR/ rP+f//+xXCKCNC2OcOn71QOrXfphLRyR4R1sFIor9DKSmUmZx/Tos/ddMJ/J/qtMoMCi J11KcWEV5G3uoAeWhwiBTwQ+Eu0zHk7U0bp2eE7wjQ8IOH6RWgTlnBRIpbQdJYN2vGH3 92ZAnqTlVlStrzRDcyuqFhQCX0Zc13ihqpUot+sMIRgE0b3nSldjsMfm0xU5/pJGW683 MQQ2JOuRGDJKXIa5x/XxxLWjgidJQqIea0VTYX30jyGekoFSRiPbZ/aAlJa4Gw3r7EQn R9kg== 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:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=a0m1MUgyNJv0WgSFmaA8BrDolKVJgHRks3YElsMZu9U=; b=ctxKRZxJHEVrrkhlcN6TYQthTtn0jhBmO7NMfZuImNumxHbCv78Ypw7vcr0qq+7wyQ E3KlyqKZxTN0jtMESZ516khnOqVCzsMFmSEdzCwHNrRfWrxrgdPUbi5RNeU3keozYcua xXqabssFKvJ8BHPHNfCsnHQnnCBi0LRH2TDSdKWIa1QcAzA1MqD5UrcyksBcAv062msd iD2pQOmA4BwDEwQVLm2wk0XMyO7j5F2UMKkbo2x8glhMpJSqe1VLHfD1nwkuhv6LAdL4 yWx2hqHiWt3MJwsWHQ2sPOeZ2DF2HPMsUQFqYxxoUfJaT6a7j0QLXSgCk+w9mqt0W2ZF yC8w== X-Gm-Message-State: APjAAAUJW5kjoRp986zFPUCfERMqTtwH1khBGkisFWX+cxr/Wx7b68KQ PwOUfALrdimg5Ndt0Cm72Ww4GA== X-Google-Smtp-Source: APXvYqw3JXjfA2QyMGRZB0h30W96g1gehSGAX27lvdy1Ba1IQaWmpMa2Y3t33ZJ5/2Ox1/TXqeRFSA== X-Received: by 2002:a7b:c24b:: with SMTP id b11mr2342481wmj.125.1573124716844; Thu, 07 Nov 2019 03:05:16 -0800 (PST) Received: from localhost (cst-prg-7-135.cust.vodafone.cz. [46.135.7.135]) by smtp.gmail.com with ESMTPSA id 205sm6145686wmb.3.2019.11.07.03.05.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 03:05:15 -0800 (PST) Date: Thu, 7 Nov 2019 12:05:14 +0100 From: Tomas Vondra To: Kyotaro Horiguchi Cc: pgsql-hackers@postgresql.org Subject: Re: Using multiple extended statistics for estimates Message-ID: <20191107110514.ythldmnq4tvjm6we@development> References: <20191028152048.jc6pqv5hb7j77ocp@development> <20191106195440.wtxsyhiwdidl37hv@development> <20191106195849.odhhc66xd23hw6hf@development> <20191107.133820.1542779476322302789.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20191107.133820.1542779476322302789.horikyota.ntt@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Nov 07, 2019 at 01:38:20PM +0900, Kyotaro Horiguchi wrote: >Hello. > >At Wed, 6 Nov 2019 20:58:49 +0100, Tomas Vondra wrote in >> >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 ;-) > >This patch is a kind of straight-forward, which repeats what the >previous statext_mcv_clauselist_selectivity did as long as remaining >clauses matches any of MV-MCVs. Almost no regression in the cases >where zero or just one MV-MCV applies to the given clause list. > >It applies cleanly on the current master and seems working as >expected. > > >I have some comments. > >Could we have description in the documentation on what multiple >MV-MCVs are used in a query? And don't we need some regression tests? > Yes, regression tests are certainly needed - I though I've added them, but it seems I failed to include them in the patch. Will fix. I agree it's probably worth mentioning we can consider multiple stats, but I'm a bit hesitant to put the exact rules how we pick the "best" statistic to the docs. It's not 100% deterministic and it's likely we'll need to tweak it a bit in the future. I'd prefer showing the stats in EXPLAIN, but that's a separate patch. > >+/* >+ * statext_mcv_clause_attnums >+ * Recalculate attnums from compatible but not-yet-estimated clauses. > >It returns attnums collected from multiple clause*s*. Is the name OK >with "clause_attnums"? > >The comment says as if it checks the compatibility of each clause but >the work is done in the caller side. I'm not sure such strictness is >required, but it might be better that the comment represents what >exactly the function does. > But the incompatible clauses have the pre-computed attnums set to NULL, so technically the comment is correct. But I'll clarify. > >+ */ >+static Bitmapset * >+statext_mcv_clause_attnums(int nclauses, Bitmapset **estimatedclauses, >+ Bitmapset **list_attnums) > >The last two parameters are in the same type in notation but in >different actual types.. that is, one is a pointer to Bitmapset*, and >another is an array of Bitmaptset*. The code in the function itself >suggests that, but it would be helpful if a brief explanation of the >parameters is seen in the function comment. > OK, will explain in a comment. >+ /* >+ * 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), > >Couldn't we avoid calling this function twice with the same parameters >at the first round in the loop? > Hmmm, yeah. That's a good point. >+ 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); >+ } > >The loop runs through all clauses every time. I agree that that is >better than using a copy of the clauses to avoid to step on already >estimated clauses, but maybe we need an Assertion that the listidx is >not a part of estimatedclauses to make sure no clauses are not >estimated twice. > Well, we can't really operate on a smaller "copy" of the list anyway, because that would break the precalculation logic (the listidx value would be incorrect for the new list), and tweaking it would be more expensive than just iterating over all clauses. The assumption is that we won't see extremely large number of clauses here. Adding an assert seems reasonable. And maybe a comment why we should not see any already-estimated clauses here. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services