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 1kegQD-0005Uq-EH for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Nov 2020 15:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kegQ6-0008Ld-IS for pgsql-hackers@arkaria.postgresql.org; Mon, 16 Nov 2020 15:27:54 +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 1kegQ6-0008LM-6S for pgsql-hackers@lists.postgresql.org; Mon, 16 Nov 2020 15:27:54 +0000 Received: from mail-ej1-x642.google.com ([2a00:1450:4864:20::642]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kegQ2-00083P-JH for pgsql-hackers@postgresql.org; Mon, 16 Nov 2020 15:27:53 +0000 Received: by mail-ej1-x642.google.com with SMTP id s25so24969925ejy.6 for ; Mon, 16 Nov 2020 07:27:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=6pYkDN9nhrjZ7ArE1E1xr1+/M2nS/1XMu0j4f3H/6QU=; b=CFfa8w0wDkgkuPp70GL91yoziAyRRsv9M8p+HrJGn98bmcZyWJzb9Qko0R+T7Ia1/Y XnDIM7H5s1eyNKLiCu81QHygKYiesfbBWXxFIPSe2EX7jZtoPc2PB30+MZpMnjqsmcu1 7TIJS+wS6gDHJVVNbPcpB9hc6gH/lG6HNFr/8kHyxzBIgid/SFYOiBvtCtfzvWVnv6Ro PcJdq/0yvv9U6xmXtkZPDo3WnGYOl/71inPv6Qs4jLUd9AaWiHIHdDdSuLDaGxDNyhuD AKV9MB1koH286YiIfadvCg4JgBrcQcLm9CJX/p4CJufGhSr0bCPJNsGIwuh0fwaWz7Ji L7kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=6pYkDN9nhrjZ7ArE1E1xr1+/M2nS/1XMu0j4f3H/6QU=; b=GjJcxL3om80WgqqkdeTjFXqwk2cBQsVaMdG9SRPGIKaVYGr/Fvetb0aEtwAfC5Y+OO VeosvmWBlZAHW9pQSyj87oylWFf3w7XCKyGzm8XXxvxsgOZ8GeQ5FmhncXvVr18/cPEJ T2rQLgYi6B8KUhOBrdrUnNAZr9KP37VZLKR8/a93Cgi3XPLrsCKlXktphxC1PqUtgojs keAh1QW1AnWP1b4tlqLlRTQRsV0CGV3yHQhkyIh+Co8xMY10RIS1DcsZSaHj9fmMNgHU NyIGaXYaur/OOBD/gQe33yHY0KZIqH6zM0jTUE3Wk0zZrWXh1zRAJo5jcbyonnq4rkdx sTBA== X-Gm-Message-State: AOAM531wkMsgmSK995GtrP0tQbvkithyOs3odLDPBx21LflGwtkoSlVb mrNIXjOSMjRi05Ghz/Yyz4iXREmFUv+v3i8uW7Dfw7v5bZjAsSoR7GB00v3fcKdW8n45EL5i8On p9iVy+eVFTt0TedApGTBOA9sCtuo9YXEn4ubdmwfTqZuFSWf2jog3Z9taNC3yATrvQgPaNTVkgA ec5lhADgJgv9/MMgVimX7rJdnO4nF4pX5ba4v8M3yUHlE+3R0/va1FH1OpoCZul7epil5ivqycU /O43AH1p24o8lN3V2A/8xchFQk75xsbYeDDaipW X-Google-Smtp-Source: ABdhPJzOHDjjnGDlFHO9tbVhI6n2l+fwBNof3aVeuOpG70R/sn6yKbyKGYlJiOefltlkeaom1XtrVA== X-Received: by 2002:a17:907:c05:: with SMTP id ga5mr14699871ejc.212.1605540467928; Mon, 16 Nov 2020 07:27:47 -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 f25sm10722008edr.53.2020.11.16.07.27.47 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Nov 2020 07:27:47 -0800 (PST) Subject: Re: PoC/WIP: Extended statistics on expressions From: Tomas Vondra To: pgsql-hackers References: Message-ID: <3fbbd68b-0b52-cb5a-d468-7e7280c33e6c@enterprisedb.com> Date: Mon, 16 Nov 2020 16:27:46 +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/16/20 2:49 PM, Tomas Vondra wrote: > Hi, > > ... > > 4) apply the statistics > > This is the hard part, really, and the exact state of the support > depends on type of statistics. > > For ndistinct coefficients, it generally works. I'm sure there may be > bugs in estimate_num_groups, etc. but in principle it works. > > For MCV lists, it generally works too - you can define statistics on > the expressions and the estimates should improve. The main downside > here is that it requires at least two expressions, otherwise we can't > build/apply the extended statistics. So for example > > SELECT * FROM t WHERE mod(a,100) = 10 AND mod(b,11) = 0 > > may be estimated "correctly", once you drop any of the conditions it > gets much worse as we don't have stats for individual expressions. > That's rather annoying - it does not break the extended MCV, but the > behavior will certainly cause confusion. > > For functional dependencies, the estimation does not work yet. Also, > the missing per-column statistics have bigger impact than on MCV, > because while MCV can work fine without it, the dependencies heavily > rely on the per-column estimates. We only apply "corrections" based > on the dependency degree, so we still need (good) per-column > estimates, which does not quite work with the expressions. > > > Of course, the lack of per-expression statistics may be somewhat > fixed by adding indexes on expressions, but that's kinda expensive. > FWIW after re-reading [1], I think the plan to build pg_statistic rows for expressions and stash them in pg_statistic_ext_data is the way to go. I was thinking that maybe we'll need some new statistics type to request this, e.g. CREATE STATISTICS s (expressions) ON ... but on second thought I think we should just build this whenever there are expressions in the definition. It'll require some changes (e.g. we require at least two items in the list, but we'll want to allow building stats on a single expression too, I guess), but that's doable. Of course, we don't have any catalogs with composite types yet, so it's not 100% sure this will work, but it's worth a try. regards [1] https://www.postgresql.org/message-id/flat/6331.1579041473%40sss.pgh.pa.us#5ec6af7583e84cef2ca6a9e8a713511e -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company