Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iSROM-0006Jv-Rk for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2019 19:54:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iSROL-0004eH-LN for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Nov 2019 19:54:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iSROL-0004eA-CM for pgsql-hackers@lists.postgresql.org; Wed, 06 Nov 2019 19:54:57 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iSROI-0008GX-CM for pgsql-hackers@postgresql.org; Wed, 06 Nov 2019 19:54:57 +0000 Received: by mail-wr1-x42c.google.com with SMTP id r10so781237wrx.3 for ; Wed, 06 Nov 2019 11:54:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=6KCdnBOAEVr38f7Ucl4xr2JpltUU3qxOwGqCUJKSt5Y=; b=1nAcFWyWIK4p5F7tr8g0vGxxygWDJzlY4WckJ2viuU4Uh+KWH8EAh3vAFED4dixfso /XFhVmLVtTrbPchN3u5d6WlKFRz4aj+nRAWWLZHnjKjAI6hmB9pRRcE1JRBnBH0uBNZD afIDakK6hz73we4Ue6kzG1WCWiJqc5uVKaBi5zM/gvc8CwACWBKIv6UpGHOBOJwAHKnW e2bJ7iNXsim6PX0mnqOa+DWNt/z+e+TQuzFLTDt+56j5/DJFm2BFaAB8IOlP3OdwkZrq WTrX523LPTShi54ArwIor2Jii0jFnGmesJxZ5fTnyVVkFkaWjnj/dTmMYHBZVOGJpNDJ oRsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=6KCdnBOAEVr38f7Ucl4xr2JpltUU3qxOwGqCUJKSt5Y=; b=Kuo//fqjxOze0jiAjthoTxr9VjR0/jR3E4W/uS2xugaOELS20vG9JDnL+Lj1W5th68 lNYy4eUrozlHgrvZbdXhGPwYfVjR0FHzk2PJw7hcXjq+oZvpsnKb4a5FIDcWK6RuAsu/ 3B+EZjhXdK8oFdTU+ynyvRsIK2txIQ1RScLCHS1m2pfBf4lZNRKTCx5HUdahTTfeaj7t IxAEXFjNJ4AG25fPSTmTI21fc9/0gE1/M8IWgCMtgeqRXfBeMLSp4+TT0X2UmrSkNE7d C3WS+2PLFbyMQ442ivJh64hFlR++xkEniaLaFQcQWOhx48LpZOvGYTbAJVwyZy4yKXe0 napA== X-Gm-Message-State: APjAAAU7W9CsFC9iIAoqM48gbtJWTsrKpnWBgY3WkBKnnHArr4kfe7yn k45/SnUwEipHU4nExRHyDdnEq5qjYsiDuIoC6XpVXyrR9L3vlv+K0Sk9k0I1dWlA9YOFcWVEfiX mEDA/yh2jfK2YivpXSjQYMIZb7hKOpMJVqeh+bH9bcgC+bowFdQMa415EapMhMpYGs9z8Ar8ig+ f0XCjkXQ2w7ZM= X-Google-Smtp-Source: APXvYqxVJRpU6/bx8bCGRtG6bGa+IfVW0icf/lY9oeI4S3hcDwLibO0bP07uyzvhCZeTQkthyi6azw== X-Received: by 2002:adf:c105:: with SMTP id r5mr4396910wre.125.1573070092701; Wed, 06 Nov 2019 11:54:52 -0800 (PST) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id v81sm4104095wmg.4.2019.11.06.11.54.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Nov 2019 11:54:44 -0800 (PST) Date: Wed, 6 Nov 2019 20:54:40 +0100 From: Tomas Vondra To: PostgreSQL Hackers Subject: Re: Using multiple extended statistics for estimates Message-ID: <20191106195440.wtxsyhiwdidl37hv@development> References: <20191028152048.jc6pqv5hb7j77ocp@development> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20191028152048.jc6pqv5hb7j77ocp@development> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Oct 28, 2019 at 04:20:48PM +0100, Tomas Vondra wrote: >Hi, > >PostgreSQL 10 introduced extended statistics, allowing us to consider >correlation between columns to improve estimates, and PostgreSQL 12 >added support for MCV statistics. But we still had the limitation that >we only allowed using a single extended statistics per relation, i.e. >given a table with two extended stats > > CREATE TABLE t (a int, b int, c int, d int); > CREATE STATISTICS s1 (mcv) ON a, b FROM t; > CREATE STATISTICS s2 (mcv) ON c, d FROM t; > >and a query > > SELECT * FROM t WHERE a = 1 AND b = 1 AND c = 1 AND d = 1; > >we only ever used one of the statistics (and we considered them in a not >particularly well determined order). > >This patch addresses this by using as many extended stats as possible, >by adding a loop to statext_mcv_clauselist_selectivity(). In each step >we pick the "best" applicable statistics (in the sense of covering the >most attributes) and factor it into the oveall estimate. > >All this happens where we'd originally consider applying a single MCV >list, i.e. before even considering the functional dependencies, so >roughly like this: > > while () > { > ... apply another MCV list ... > } > > ... apply functional dependencies ... > > >I've both in the loop, but I think that'd be wrong - the MCV list is >expected to contain more information about individual values (compared >to functional deps, which are column-level). > Here is a slightly polished v2 of the patch, the main difference being that computing clause_attnums was moved to a separate function. This is a fairly simple patch, and it's not entirely new functionality (applying multiple statistics was part of the very first patch seris, although of course in a very different form). So unless there are objections, I'd like to get this committed sometime next week. There's room for improvement, of course, for example when handling overlapping statistics. Consider a table with columns (a,b,c) and two extended statistics on (a,b) and (b,c), and query with one clause per column SELECT * FROM t WHERE a = 1 AND b = 1 AND c = 1 In this case the patch does not help, because we apply (a,b) and then we have just a single clause remaining. What we could do is still apply the (b,c) statistic, using the already-estimated clause on b as a condition. So essentially we'd compute P(a=1 && b=1) * P(c=1 | b=1) But that'll require larger changes, and I see it as an evolution of the current patch. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services