Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkV53-00023k-2x for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Dec 2020 16:34:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkV52-0004Fh-0S for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Dec 2020 16:34:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kkV51-0004Fa-LK for pgsql-hackers@lists.postgresql.org; Wed, 02 Dec 2020 16:34:11 +0000 Received: from mail-ej1-x643.google.com ([2a00:1450:4864:20::643]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kkV4y-00041Q-4S for pgsql-hackers@postgresql.org; Wed, 02 Dec 2020 16:34:11 +0000 Received: by mail-ej1-x643.google.com with SMTP id bo9so5230327ejb.13 for ; Wed, 02 Dec 2020 08:34:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=MhIBx2r3DQJhQJkKLbJ2TcSZ7WJ+NXpCacGmI5A34/A=; b=ror11s4VwM9DrvINKrJMRoBVxVtLMesckyZ5j76HeTyCw3rxzDA+4IMaT773HierUC vzBHskT3aPWM02AGtiKCAaL5f0zINfHDGiyptF34Th0s8FRl1kpEDx/OJXRNFVi6134T UOR+2xnoIQOybcRnWNDWThvwBwMC1ZzJq/xknhsfD0/Ztf1+B/0HIU6Wk0pKMMsg99g5 6Qjco+cWioSx16RXGp87wgYXmfcdJT3ys+re7HB+1ocI9oqduLFIO2W2rIZGlkKRV/i1 aYPFh7qzgJi0/lZWAVFA6i4gN5cb9UUfsMq75L8Bv4f5etrE1GZUIWXm8kjq48z6QERI rcug== 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 :content-transfer-encoding; bh=MhIBx2r3DQJhQJkKLbJ2TcSZ7WJ+NXpCacGmI5A34/A=; b=XTq1JtdGf7q4h4gwTErvez+CGdCiNEnwL6F2ysaroXxCy+X6MmVg/ws4VC+CMD/vB+ Qk48QHXQqXL3Sr/uj6fxfFr4y6MTepKtfPEEsTj1ScZ7LzoqrmdzcoMPlmkNF57krxA0 yBYlwA5YTt9/OBAyOAIZWEa8QdDQRdY3GdczZH+tHBroPWMcfjlNW6rHqwBpqW0SJCwZ /67px08dYxb9MQPhqxS4DCOGm+XeB8UUMNDGLFXD53wL+HMYBrea6XS+cz9tCEF144hO sAkbyddrGpOuGHgDJ/ZDvIwcCtpNNH0mx+vIoiX94g80h1IvRMttceVrIvHXJTcyrpxN /alQ== X-Gm-Message-State: AOAM532ZA0pQaAabzEf+H62lgFpReTBwl49ha4ZO4uYSrh/LGZXHTXfV SyaNlWdEt609/dNORsnZrq9lfOZjGt2g5VfgEZYweHPwjW8xwtCjk1L2wcmES9H1K02ZGX6Ye/e zgwHX2b/9F3tB9/9fJmk9o+sYd/AWL8abfUjXnPMCBT8WRg+8DQzkNhPtbRFpa7ob5o/py/TMo4 O4vdwy3/yQC4iGlqzySCsTaLyWHozZk9mdWMIpqZYSp9xyxbx5m46TbBco+Bsg7M4B3hZc1uUdj dQReJ0ePnc5rtaIDqk8SgGQmvqe0kEroK//6Rqv X-Google-Smtp-Source: ABdhPJw6lzwg8JuDess9T62qDxB9R67VYj1kjDDufjM/HsDzMiBtVamKYE0+FmeNrJ1VkxujThzV6A== X-Received: by 2002:a17:906:1e0e:: with SMTP id g14mr607737ejj.128.1606926846169; Wed, 02 Dec 2020 08:34:06 -0800 (PST) Received: from [10.137.0.4] (ip-86-49-245-76.net.upcbroadband.cz. [86.49.245.76]) by smtp.gmail.com with ESMTPSA id z2sm294561edr.47.2020.12.02.08.34.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Dec 2020 08:34:05 -0800 (PST) Subject: Re: Additional improvements to extended statistics To: Dean Rasheed Cc: Tomas Vondra , Daniel Gustafsson , PostgreSQL Hackers References: <20200315000809.ruwpx7ktnpnzly7c@development> <20200318193128.53soruotqdqnlaoy@development> <20200321215946.epx3dfi74stlwiab@development> <20200324010838.hbv5gzhexcv5cilg@development> <20200324143306.rz5siohm4jsoveqm@development> <20200703011030.smzijlphrjjodhxx@development> <57eb142c-d5a4-cfca-675b-0dfd9ef7c2e2@2ndquadrant.com> <56df91cd-c7dc-f46b-6bc3-cb36982ff9ae@enterprisedb.com> From: Tomas Vondra Message-ID: <01a57d32-e442-b57f-6154-fd42129d6307@enterprisedb.com> Date: Wed, 2 Dec 2020 17:34:04 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/2/20 4:51 PM, Dean Rasheed wrote: > On Sun, 29 Nov 2020 at 21:02, Tomas Vondra > wrote: >> >> I wonder how much of the comment before clauselist_selectivity should >> move to clauselist_selectivity_ext - it does talk about range clauses >> and so on, but clauselist_selectivity does not really deal with that. >> But maybe that's just an implementation detail and it's better to keep >> the comment the way it is. > > I think it's better to keep it the way it is, so that the entirety of > what clauselist_selectivity() does (via clauselist_selectivity_ext()) > can be read in one place, but I have added separate comments for the > new "_ext" functions to explain how they differ. That matches similar > examples elsewhere. > +1 > >> I noticed this outdated comment: >> >> /* Always compute the selectivity using clause_selectivity */ >> s2 = clause_selectivity_ext(root, clause, varRelid, jointype, sjinfo, > > Updated. > > >> Also, the comment at clauselist_selectivity_or seems to not follow the >> usual pattern, which I think is >> >> /* >> * function name >> * short one-sentence description >> * >> * ... longer description ... >> */ > > Hmm, it seems OK to me. The first part is basically copied from > clauselist_selectivity(). The "longer description" doesn't really have > much more to say because it's much simpler than > clauselist_selectivity(), but it seems neater to keep the two roughly > in sync. > I see. In that case it's OK, I guess. > > I've been hacking on this a bit more and attached is an updated > (hopefully final) version with some other comment improvements and > also a couple of other tweaks: > > The previous version had duplicated code blocks that implemented the > same MCV-correction algorithm using simple_sel, mcv_sel, base_sel, > other_sel and total_sel, which was quite messy. So I refactored that > into a new function mcv_combine_selectivities(). About half the > comments from statext_mcv_clauselist_selectivity() then move over to > mcv_combine_selectivities(). I also updated the comments for > mcv_clauselist_selectivity() and mcv_clause_selectivity_or() to > explain how their outputs are expected to be used by > mcv_combine_selectivities(). That hopefully makes for a clean > separation of concerns, and makes it easier to tweak the way MCV stats > are applied on top of simple stats, if someone thinks of a better > approach in the future. > > In the previous version, for an ORed list of clauses, the MCV > correction was only applied to the overlaps between clauses. That's OK > as long as each clause only refers to a single column, since the > per-column statistics ought to be the best way to estimate each > individual clause in that case. However, if the individual clauses > refer to more than one column, I think the MCV correction should be > applied to each individual clause as well as to the overlaps. That > turns out to be pretty straightforward, since we're already doing all > the hard work of computing the match bitmap for each clause. The sort > of queries I had in mind were things like this: > > WHERE (a = 1 AND b = 1) OR (a = 2 AND b = 2) > > I added a new test case along those lines and the new estimates are > much better than they are without this patch, but not for the reason I > thought --- the old code consistently over-estimated queries like that > because it actually applied the MCV correction twice (once while > processing each AND list, via clause_selectivity(), called from > clauselist_selectivity_simple(), and once for the top-level OR clause, > contained in a single-element implicitly-ANDed list). The way the new > code is structured avoids any kind of double application of extended > stats, producing a much better estimate, which is good. > Nice. > However, the new code doesn't apply the extended stats directly using > clauselist_selectivity_or() for this kind of query because there are > no RestrictInfos for the nested AND clauses, so > find_single_rel_for_clauses() (and similarly > statext_is_compatible_clause()) regards those clauses as not > compatible with extended stats. So what ends up happening is that > extended stats are used only when we descend down to the two AND > clauses, and their results are combined using the original "s1 + s2 - > s1 * s2" formula. That actually works OK in this case, because there > is no overlap between the two AND clauses, but it wouldn't work so > well if there was. > > I'm pretty sure that can be fixed by teaching > find_single_rel_for_clauses() and statext_is_compatible_clause() to > handle BoolExpr clauses, looking for RestrictInfos underneath them, > but I think that should be left for a follow-in patch. I have left a > regression test in place, whose estimates ought to be improved by such > a fix. > Yeah, I agree with leaving this for a separate patch. We can't do everything at the same time. > The upshot of all that is that the new code that applies the MCV > correction to the individual clauses in an ORed list doesn't help with > queries like the one above at the moment, and it's not obvious whether > it is currently reachable, but I think it's worth leaving in because > it seems more principled, and makes that code more future-proof. I > also think it's neater because now the signature of > mcv_clause_selectivity_or() is more natural --- it's primary return > value is now the clause's MCV selectivity, as suggested by the > function's name, rather than the overlap selectivity that the previous > version was returning. Also, after your "Var Op Var" patch is applied, > I think it would be possible to construct queries that would benefit > from this, so it would be good to get that committed too. > > Barring any further comments, I'll push this sometime soon. > +1 regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company