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 1lMdl0-0002xr-SS for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Mar 2021 21:31: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 1lMdkz-0006hD-4R for pgsql-hackers@arkaria.postgresql.org; Wed, 17 Mar 2021 21:31:09 +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 1lMdky-0006h6-TF for pgsql-hackers@lists.postgresql.org; Wed, 17 Mar 2021 21:31:08 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lMdku-0000BB-ER for pgsql-hackers@postgresql.org; Wed, 17 Mar 2021 21:31:08 +0000 Received: by mail-ed1-x536.google.com with SMTP id bx7so4054027edb.12 for ; Wed, 17 Mar 2021 14:31:03 -0700 (PDT) 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=7jpWKW1FP4mQ1eX0qID0lZsOAs0Pj0b1VPAzWJ7o3Ho=; b=UskY1uQyWHo4cW+JGJW+J+KKVVL+jdQxqlcmFOu+GcQb8whVWyn/6YpRpOTBtKkwCl J6L4OZRPGtT2u1lkY9rueLY9LNCcvu9b+XAO0gxr1eqfh/D644r/iadP3I1iRXH98ell Mzt/ZLD6CClGPbXCPn9sXfer1NRGr5QngrF5hr6A1PI8FaYr8AMJf2US6sPz0+BICiAX AOhMQFIhHrwdEpC43mgm/Fdc9z41Sr4ZlXVr+6tgx1mHOYfUc0/HDyv/EjAWkKT6utyI zKUJdGTYGNKp2a5UHntm0hLxCZB6iXDYw6YcZvqitQrseylqs1xk1UlAyY0aO15W4YIT 9jdQ== 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=7jpWKW1FP4mQ1eX0qID0lZsOAs0Pj0b1VPAzWJ7o3Ho=; b=Ui89x5bGjL6/pX9/NQi77yb+Ng/r07U6/4o75WPa8bOW3CoqsRDX1KHRZAnMeR8vrc XLyMZYOj3a0erycejCBNlOYCkj4NGdinY3tGr1wTi0nA8McjcT4TqTpDN0wit0CeqPaj DJHMPUKArpN/lZAUTcP15wXuCjY9F9NbUUsvT0KPTtWF5aUyorgSQU4RhffbrR4IjBb+ c+4aSccJCNHUlYaMrVs3EPNtR/P+qlRhYoybJC5pSnCUpMLOK7WRvhyFjofgyoKANy5K Gs2sWDii54btIDYxDERGeV0fEEE7TgzKNVCv1SqYRZcEAiMNalWLQfm8zv+1peh2C+ID Co4Q== X-Gm-Message-State: AOAM53068iofIEPbW+TR8W9WwLYaNv39UkkqX1pKHFUhTKs8+xujKCwN cLyNf8s8f6i/7Lj2G4BtdnVaiv31pEWtR63a6xfr0OtiTGSc7DfUifca+K+qphiUbcGHTcAaS4J A4cJDW/DlgW3RqwlFLrKXYFQBjJ4h5I74wLU/LLrT2U21DHrmif5pBP87LKwAUyMU++3/WwZEIp b44iOwQ3jeVi1UmonyVPEpFtiFazHtO8EySvAc6XnK0zfsfhOAKYH1vgmbg7bod/8g8IyUIYOs2 y1zj5J9dzIpEnfcuoBY8cMsJPn0Hw0nWZtVDQ== X-Google-Smtp-Source: ABdhPJyT5JDmEFQ0WsPutjzzYryMUl3KACkDyoXGIWflqZ++ckC48nhc0mzBhE2TgvEXDmizR88I3A== X-Received: by 2002:aa7:c497:: with SMTP id m23mr44311766edq.74.1616016662322; Wed, 17 Mar 2021 14:31:02 -0700 (PDT) Received: from [10.137.0.20] (ip-86-49-253-127.net.upcbroadband.cz. [86.49.253.127]) by smtp.gmail.com with ESMTPSA id lx6sm88082ejb.64.2021.03.17.14.31.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Mar 2021 14:31:01 -0700 (PDT) Subject: Re: PoC/WIP: Extended statistics on expressions To: Dean Rasheed Cc: Justin Pryzby , PostgreSQL Hackers References: <20210108023537.GA19743@telsasoft.com> <20210122022958.GC27167@telsasoft.com> <2147e075-fef6-b1d5-e5f5-7f92ffefe827@enterprisedb.com> <1913c1fe-13da-7250-adf4-befeda397536@enterprisedb.com> From: Tomas Vondra Message-ID: Date: Wed, 17 Mar 2021 22:30:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.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: Archived-At: Precedence: bulk On 3/17/21 9:58 PM, Dean Rasheed wrote: > On Wed, 17 Mar 2021 at 20:48, Dean Rasheed wrote: >> >> For reference, here is the test case I was using (which isn't really very good for >> catching dependence between columns): >> > > And here's a test case with much more dependence between the columns: > > DROP TABLE IF EXISTS foo; > CREATE TABLE foo (a int, b int, c int, d int); > INSERT INTO foo SELECT x%2, x%5, x%10, x%15 FROM generate_series(1,100000) x; > SELECT COUNT(DISTINCT a) FROM foo; -- 2 > SELECT COUNT(DISTINCT b) FROM foo; -- 5 > SELECT COUNT(DISTINCT c) FROM foo; -- 10 > SELECT COUNT(DISTINCT d) FROM foo; -- 15 > SELECT COUNT(DISTINCT (a+b)) FROM foo; -- 6 > SELECT COUNT(DISTINCT (c+d)) FROM foo; -- 20 > SELECT COUNT(DISTINCT ((a+b),c)) FROM foo; -- 10 > SELECT COUNT(DISTINCT ((a+b),(c+d))) FROM foo; -- 30 > > -- First case: stats on [(a+b),c] > CREATE STATISTICS s1(ndistinct) ON (a+b),c FROM foo; > ANALYSE foo; > EXPLAIN ANALYSE > SELECT (a+b), (c+d) FROM foo GROUP BY (a+b), (c+d); > -- Estimate = 150, Actual = 30 > -- This estimate is ndistinct((a+b),c) * ndistinct(d) = 10*15, > -- which is much better than ndistinct((a+b)) * ndistinct(c) * > ndistinct(d) = 6*10*15 = 900 > -- Estimate with no stats = 1500 > > -- Second case: stats on (c+d) as well > CREATE STATISTICS s2 ON (c+d) FROM foo; > ANALYSE foo; > EXPLAIN ANALYSE > SELECT (a+b), (c+d) FROM foo GROUP BY (a+b), (c+d); > -- Estimate = 120, Actual = 30 > -- This estimate is ndistinct((a+b)) * ndistinct((c+d)) = 6*20 > > Again, I'd say the current behaviour is pretty good. > Thanks! I agree applying at least the [(a+b),c] stats is probably the right approach, as it means we're considering at least the available information about dependence between the columns. I think to improve this, we'll need to teach the code to use overlapping statistics, a bit like conditional probability. In this case we might do something like this: ndistinct((a+b),c) * (ndistinct((c+d)) / ndistinct(c)) Which in this case would be either, for the "less correlated" case 228 * 24 / 12 = 446 (actual = 478, current estimate = 480) or, for the "more correlated" case 10 * 20 / 10 = 20 (actual = 30, current estimate = 120) But that's clearly a matter for a future patch, and I'm sure there are cases where this will produce worse estimates. Anyway, I plan to go over the patches one more time, and start pushing them sometime early next week. I don't want to leave it until the very last moment in the CF. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company