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 1kmcOX-0005Nh-9o for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Dec 2020 12:47:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmcOW-0006Rs-6g for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Dec 2020 12:47:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmcOV-0006Rl-W4 for pgsql-hackers@lists.postgresql.org; Tue, 08 Dec 2020 12:47:03 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmcOT-00070x-QL for pgsql-hackers@postgresql.org; Tue, 08 Dec 2020 12:47:03 +0000 Received: by mail-ed1-x541.google.com with SMTP id u19so17461081edx.2 for ; Tue, 08 Dec 2020 04:47:01 -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=EeIaQx2LLgPjTnF07Q3u/J3ofKljY7KaHhM/IxSnhQ8=; b=gMr0cTBJG/K5/zw8wmSUkr1nB9LGX37caSyQhTlPucvj6TIEuMGVCY2YNCo13Fb5nC loIQQsFFLqdXOFm3CxKJUt91fL9byI9Am7skFXtpqNACH0cAscXSyvISXt12nc3IBtIc +oLpyNHF8U0ZW6CVQREZGOaVLcGwriYKxF6ff6RFDJu/BagWnXVEAh/YsPx0wS2kKe54 aj22QOvwZG+k9ZXcGlKmhfx+a8j2vdUe9a3Rwef1PSvtd8crisnS3wS+2FBwE6/BL6Nc QrrwQpPW/L6k9CXcJRkodWIFZPcJrTEa+qgwKz/hg1ib6dB6zwQ+gZ0O3f7Wb+PjI3iQ 6WxA== 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=EeIaQx2LLgPjTnF07Q3u/J3ofKljY7KaHhM/IxSnhQ8=; b=suH8J/yJED0VV3LlW3HZr46OwAGg60kRlScgjhzwdsMVEu9m1TXzOz0A024HcUIxVA o7bS3uFrGikfqVOLiEeZWtHhLlomrPxl6Mg0PDaLn352qt536V2t+bDpx2ZITFkVXOWh zj455hhcxzPGo5y3lqroGszZWf6Mq+WEl+5AYPwMHc5lzSLzpB1TK1O7uTfmCxAJNvxu Z1LA8/yZB4F2OLu4gUonIEvwCC1H6nnsjBCGBE6zsuwmp6cdl0pB7UIPfMTjVf6mQRYJ nrstcal3WTkeaTi3/BmDji/6mn2AqhEx93hUZrCiaOmrfpdFyXLAgl+Oru3PCBoFcr8k dGNw== X-Gm-Message-State: AOAM533yrFw7jwG421U+xHjl4Zl4NsG86z0geKs7xwDxQzSs78+UNFNu ZPq920t7x5GQc/6CwWeNFG2g6KkBtcIbRQjJsPTeBWPkQCa6zLBdaa7Og6sKitzyAVQC+5P6Ad+ xZOWVZuvFZm2PlA+24VPvW52g6KMPC8tC0FEgt1hFzzVztCUTvAV3NfJdRXur22Uu9HqiJ1FUJE O2PMETVIOnG6x/XQ7/r7+hZZoILaLGs9dq89rQ04rX4ayRVKRBrw4gGeNjNIdQ1grvSSCkTIeCF 4AmXEtXFX4Ncg/zbfQIx9Jea+JaZ8qlHeMQkg== X-Google-Smtp-Source: ABdhPJxcM8njLFDXrRMjRlpBQHZfOqznnXBSLhBPUzloe0Eh/RCJEqtxSyjc966vH/LAWj6I9r5XIQ== X-Received: by 2002:a05:6402:312c:: with SMTP id dd12mr24859230edb.327.1607431620084; Tue, 08 Dec 2020 04:47:00 -0800 (PST) Received: from [10.137.0.4] (ip-86-49-249-71.net.upcbroadband.cz. [86.49.249.71]) by smtp.gmail.com with ESMTPSA id bg4sm15521259ejb.24.2020.12.08.04.46.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 08 Dec 2020 04:46:59 -0800 (PST) Subject: Re: Additional improvements to extended statistics To: Dean Rasheed Cc: Tomas Vondra , Daniel Gustafsson , PostgreSQL Hackers References: <20200315000809.ruwpx7ktnpnzly7c@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: <4ba455c6-a0fa-cae7-7bc3-4aa5b6cd11d4@enterprisedb.com> Date: Tue, 8 Dec 2020 13:46:57 +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/7/20 5:15 PM, Dean Rasheed wrote: > On Wed, 2 Dec 2020 at 15:51, Dean Rasheed wrote: >> >> The sort of queries I had in mind were things like this: >> >> WHERE (a = 1 AND b = 1) OR (a = 2 AND b = 2) >> >> 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. > > Attached is a patch doing that, which improves a couple of the > estimates for queries with AND clauses underneath OR clauses, as > expected. > > This also revealed a minor bug in the way that the estimates for > multiple statistics objects were combined while processing an OR > clause -- the estimates for the overlaps between clauses only apply > for the current statistics object, so we really have to combine the > estimates for each set of clauses for each statistics object as if > they were independent of one another. > > 0001 fixes the multiple-extended-stats issue for OR clauses, and 0002 > improves the estimates for sub-AND clauses underneath OR clauses. > Cool! Thanks for taking time to investigate and fixing those. Both patches seem fine to me. > These are both quite small patches, that hopefully won't interfere > with any of the other extended stats patches. > I haven't tried, but it should not interfere with it too much. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company