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 1kjTqg-00047O-Oz for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Nov 2020 21:03:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kjTqA-0003Zx-1H for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Nov 2020 21:02:38 +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 1kjTq9-0003Zp-I9 for pgsql-hackers@lists.postgresql.org; Sun, 29 Nov 2020 21:02:37 +0000 Received: from mail-ej1-x644.google.com ([2a00:1450:4864:20::644]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kjTq2-0003Ck-1N for pgsql-hackers@postgresql.org; Sun, 29 Nov 2020 21:02:36 +0000 Received: by mail-ej1-x644.google.com with SMTP id pg6so6664901ejb.6 for ; Sun, 29 Nov 2020 13:02:29 -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=nyZvxKA24Q3TGnkxn0Ia6cAVkbmq8veTA9zX1ij6h+A=; b=ThrzfyY4lWDgNmXVI5TbLHvJ77+hxIU2zYcj4f0DOjtUAY85rDjYuWKzUGrygsQP/X EeOZfDbSjgkaM0XTp6JNzYkVplT4eF4JZiMKbla7BeM7ghlSjbyu2tKlfEh5Tm0DRaZf B7hLy5U3FunfhLBtFFSDu8gzPNwTcS1LctVZug2W2FkQoo2m3S7BOcQuihmK08n6Yn3c 0C2DrhXjRRjTnS/TioHnEfnAuVwk+UPlIKUipo3JZ+CnVqYd0E3fNZlrTVwEEOtX8ht3 iYVC5E7ci7Z9maUcy1y+RWqmCOakN68/SaLXlTzyurfAFnPmoNS/mV2vwCDiCwZVBzli qCgg== 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=nyZvxKA24Q3TGnkxn0Ia6cAVkbmq8veTA9zX1ij6h+A=; b=XUpg+N9VpP+l7hh58OQ0eQw8wj1sm8fzoI+GYgXA6BjKFg7Al7omDQSUwuVqVbigv1 JcQKZbpjxRGVq8kteLUE8wpKva7X7JEJQrNdcpLL8XCRvlNsIPj8dvh/1bmZf87lG9Ke pIRfy8R8CrjS8Fo4IvZzcvfO6GDO555SXxf1tRqZRLrl1rM10L7Rx0ahK/Qct7600Ilb 7BZCHWEmmsyZp+yWZMSe3KP4803di7rng3OYX6LDmn7CDXJP/K1NoJzMQSsA1Vh96Psf imDRfrnxJspdFOhrnxwIhP2eU3mtJ7CcZfq+aJK4lV1PF37TrTNKKDwvW7JyPnxSTAsH 1GEA== X-Gm-Message-State: AOAM531IBSjSQggaD1pY0Tz8ieHWJt4zJEQf+zRoCnztmd3KnYj+HmuY QcYpCc0PIEZArsiKejTthNqBBvCWvG8/E+FoDNMzFGoQIuBZh4xld+TUiPgtFoArasWDVyHNnjJ imcTOW+tQqIgTk5AgZcNbxrxMh4sQHJz/FklfIQFqa8y/Sl98w8yhfLKp8kKlqcye7/kjD96JO9 KLEYahRs+pBzZ46lc4ykpnu+aGOKld2xKBPez9DyzSaDUGTtIJ9j00eRoeDsO+nfI1cz7S3u+Rk xaEclVmnidj+yFtQE8FKKT5meHZTV1ni+Sh2iYh4Kw= X-Google-Smtp-Source: ABdhPJzNfBu+Yasm065yl4AfzapO6kCh8+8e+PVgK6OpLfRECe0IPGvOCrqptBbBFSwIog/R1cYJuQ== X-Received: by 2002:a17:906:ecad:: with SMTP id qh13mr12488759ejb.443.1606683747400; Sun, 29 Nov 2020 13:02:27 -0800 (PST) Received: from [10.137.0.18] (ip-86-49-245-76.net.upcbroadband.cz. [86.49.245.76]) by smtp.gmail.com with ESMTPSA id l27sm7675080eja.20.2020.11.29.13.02.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Nov 2020 13:02:26 -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: Date: Sun, 29 Nov 2020 22:02:22 +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 11/29/20 3:57 PM, Dean Rasheed wrote: >> On Wed, 18 Nov 2020 at 22:37, Tomas Vondra >> wrote: >>> >>> Seems fine to me, although the "_opt_ext_stats" is rather cryptic. >>> AFAICS we use "_internal" for similar functions. >>> > > I have been thinking about this some more. The one part of this that I > still wasn't happy with was the way that base frequencies were used to > compute the selectivity correction to apply. As noted in [1], using > base frequencies in this way isn't really appropriate for clauses > combined using "OR". The reason is that an item's base frequency is > computed as the product of the per-column selectivities, so that (freq > - base_freq) is the right correction to apply for a set of clauses > combined with "AND", but it doesn't really work properly for clauses > combined with "OR". This is why a number of the estimates in the > regression tests end up being significant over-estimates. > > I speculated in [1] that we might fix that by tracking which columns > of the match bitmap actually matched the clauses being estimated, and > then only use those base frequencies. Unfortunately that would also > mean changing the format of the stats that we store, and so would be a > rather invasive change. > > It occurred to me though, that there is another, much more > straightforward way to do it. We can rewrite the "OR" clauses, and > turn them into "AND" clauses using the fact that > > P(A OR B) = P(A) + P(B) - P(A AND B) > > and then use the multivariate stats to estimate the P(A AND B) part in > the usual way. > OK, that seems quite reasonable. > Attached is the resulting patch doing it that way. The main change is > in the way that statext_mcv_clauselist_selectivity() works, combined > with a new function mcv_clause_selectivity_or() that does the > necessary MCV bitmap manipulations. > > Doing it this way also means that clausesel.c doesn't need to export > clauselist_selectivity_or(), and the new set of exported functions > seem a bit neater now. > Nice. I agree this looks way better than the version I hacked together. 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 noticed this outdated comment: /* Always compute the selectivity using clause_selectivity */ s2 = clause_selectivity_ext(root, clause, varRelid, jointype, sjinfo, 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 ... */ Those are fairly minor issues. I don't have any deeper objections, and it seems committable. Do you plan to do that sometime soon? > A handful of regression test results change, and in all cases except > one the new estimates are much better. One estimate is made worse, but > in that case we only have 2 sets of partial stats: > > SELECT * FROM mcv_lists_multi WHERE a = 0 OR b = 0 OR c = 0 OR d = 0 > > with stats on (a,b) and (c,d) so it's not surprising that combining (a > = 0 OR b = 0) with (c = 0 OR d = 0) mis-estimates a bit. I suspect the > old MV stats estimate was more down to chance in this case. > Yeah, that's quite possible - we're multiplying two estimates, but there's a clear correlation. So it was mostly luck we had over-estimated the clauses before, which gave us higher product and thus accidentally better overall estimate. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company