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 1ldzgs-0007cp-T7 for pgsql-general@arkaria.postgresql.org; Tue, 04 May 2021 18:22:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ldzgr-0008FQ-I8 for pgsql-general@arkaria.postgresql.org; Tue, 04 May 2021 18:22:37 +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 1ldzgr-0008FH-9z for pgsql-general@lists.postgresql.org; Tue, 04 May 2021 18:22:37 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ldzgn-0005YJ-Gx for pgsql-general@lists.postgresql.org; Tue, 04 May 2021 18:22:36 +0000 Received: by mail-wr1-x432.google.com with SMTP id v12so10437325wrq.6 for ; Tue, 04 May 2021 11:22:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=8Z8QzbyCNl4T7aDSVJnBQ+fWFGh/Oc0PU2vLSDUNybE=; b=uXdMOfSb7MJOC7Y7jEIexTL+wScmKpqGECDfNxZmxhVaNwopzl7edyHa2sl8z3MfoC a/6rrgqBsmAwKSkvyV3lYXPFHx3AHnBrvzK6hxGk8ja2NjQ4sTPGTzvb/MImGgWaKVpN GAJyzDOeLbU6nl06lZAwrPkbo9uantrNxJNl7lUKl/AaASfmW9X6b0fWWBQ5F8wHV514 NlqZ/rqAihgjTfbUzWcU9gA9uMLMhl08WoHbk8BhsKsdgGBZqX1z0LpwLZ15naJB7Rad haCaK7AdRqWpLfa/nCDUGlOXPXHwvdVhlzsDvc29DPMuXzXQIM/i37cfYQQKBCYGH2eB 8QxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=8Z8QzbyCNl4T7aDSVJnBQ+fWFGh/Oc0PU2vLSDUNybE=; b=XO0QP4MyBffxzEP+Dr5AI3cFFpVz2NwxvYnfrSXgB10sog548D2PzA3GlSCupMBMkN tM8MQ0RI287SUHj5JYhmhoPrqJyhLyUOEaTegUL+hQOvOMECNvo6zbwy+hym8wksjkBh F+5b/aPyTp3fGnLHPLN0scf9Bti/qSUbehMjzU0S4ilCk5dbdSQ94Gubcovwo9dJUINp 4TxXwVIRDoDyXbVAh3AbtJfONXWou8tBdVGYqr95Q2GcfRVamn8IOAqRtYSVLBvgQO9K 2BYt0D324Z+zuaEDF0JUigHIP7MB+j76iMUoFM3f2HnSyyV/tCdJ263krgUxLOXiGoVV +FVg== X-Gm-Message-State: AOAM530JrPWvR3go92aBYSHdl3NIUNIKvSTpk5A0kEnNxMuFSMWKlvuh GyU8GQkeYqkyo64yK2/w2D/UEiz3CYvaSEKpAgxGTKL4vLaoR2Wy/x5uOOGeMJ39znJBal8Vw+T n1F1XQYOms1iV3HuU5owSLIVKfpK2/tvQAVajh6WuRTYJAYvBOmfOeovCttzSDBDiysGLzJQG2K a0Xfcb5ePOw+NnGI+/u8zaYDIEyOfb3xSkKuSnFkYUr13mugE1eyJBEhGk5Rkzh6QHJnMC3/600 DHKhV8CrxIDA7YZCTYl1BoBUhqVSZ9DvHqq5IwbG7N1FXwEmRxvjchA X-Google-Smtp-Source: ABdhPJxfZ9ycFeV1nsHECLR4zP3n/Domp9uJV0CKF9DXGBIwNDIrhzkZIBKKwQnQZZe4ukxYmfD+JQ== X-Received: by 2002:adf:ba54:: with SMTP id t20mr33738752wrg.353.1620152550665; Tue, 04 May 2021 11:22:30 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id b20sm3760667wmj.3.2021.05.04.11.22.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 May 2021 11:22:30 -0700 (PDT) Subject: Re: Extended stats - value not in MCV list To: =?UTF-8?Q?Pedro_Luis_Guzm=c3=a1n_Hern=c3=a1ndez?= , pgsql-general@lists.postgresql.org References: From: Tomas Vondra Message-ID: <1e48e3a0-acef-b552-adda-8f5403d6ca86@enterprisedb.com> Date: Tue, 4 May 2021 20:22:28 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit 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: Archived-At: Precedence: bulk Hi, this topic would probably be a better fit for pgsql-hackers, as it's about the internal implementation, but let's continue the discussion here for now. On 5/2/21 6:52 AM, Pedro Luis Guzmán Hernández wrote: > Hi there, > > I've just started using extended stats cause the planner was giving me > terrible estimates for a certain table. MCV extended stats solved my > problem when values are in the extended MCV list, but estimates are > still terrible when they are not in the MCV list. Limited as my > knowledge of database internals is, I dug into the source code and found > an important difference on how these not-MCV values are handled in the > single-column and multi-column cases. > Hmm, so which which PostgreSQL version are you testing this on? I'm not sure which code branch you're looking at. Can you share a reproducer, i.e. a simple example demonstrating the issue? That'd be very helpful. > For single columns, the estimate is calculated as follows: > selectivity = (1 - sum(MCV_frequencies)) / (distinct_values - count(MCV)) > Which seems to assume a uniform distribution of non-MCV values and looks > like a sensible guess, at least to my amateur eyes. > > For multi-column statistics it seems to me that the estimate is > calculated instead as: > selectivity = 1 - sum(MCV_frequencies) > Which instead seems to assume that the value could potentially be > present in all the rows not covered by the MCV. This seems like an > adequate upper bound, but is also quite conservative compared to the > single-column estimate. In my specific case this yields a selectivity > even higher than some of the least frequent MCV values, which is a > condition that is actually checked for and compensated in the > single-column estimate as an additional check. I have MCV and > distinct extended stats, so I know the distinct_values  stats is available. > It shouldn't behave like that, and some quick experiments suggest it does not (at least on master). I can't rule out a bug, of course. A reproducer would be helpful. > So I hope my question is clear from the above. How come the estimates > are calculated with such different approaches? I insist I have no > experience with database/query planner development, so excuse me if I am > overlooking some obvious conceptual difference between single-column and > multi-column stats. The single-column estimate is actually described in > the documentation, but the multi-column estimate is not. If there is > indeed a good reason for this difference, I think it should be documented. > As for the ndistinct estimates and multi-column MCV lists, it's not all that simple - there may be conditions on only some of the columns, in which case we don't know how many groups we actually matched, etc. TBH I'm not sure how much of those implementation details we want to put into the user docs - it may be a bit too much, and we may need to change some of it. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company