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 1iTZHc-0006TH-G5 for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Nov 2019 22:32:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iTZHa-0001E0-6M for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Nov 2019 22:32:38 +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 1iTZHZ-0001Dt-R4 for pgsql-hackers@lists.postgresql.org; Sat, 09 Nov 2019 22:32:37 +0000 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iTZHT-0006z8-G4 for pgsql-hackers@postgresql.org; Sat, 09 Nov 2019 22:32:37 +0000 Received: by mail-pg1-x542.google.com with SMTP id h4so6476405pgv.8 for ; Sat, 09 Nov 2019 14:32:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=JGyi2OY97MbJOl2IK+vGeMFVmeBRn8Z++KYzeUg2f7I=; b=SOZjP7EJGeNJqwUQLOaRhc46BxfhNTUGsNEleNmsVNwTd7ts3Q8Jimh0roSAzcirt6 J0+CXenClfevcx78t9f2v5Q3840CnCLmd3P7bCwbUAVI6SANoAGhcsMvIVMzMZjTZZ+q S99K/iiHz8Buf74ihE4GGetNJAU6E6631gYFUYRBPWrxflaCUbHFqLHYGhdOoGqxTgy/ Tdh8JToQP3iOqvTsvG+Y1epLWGzXBI2X0JV/H7hBCcO/OSjw0pOsM1rAZQdRnLrN05Kn PVUHEm49PUcpK+Fh5zVzUfxaVTVpPpvwVoAcEDiMd4HXHwQGb5ar3H0njnDfXT7MA6tu UcKA== 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; bh=JGyi2OY97MbJOl2IK+vGeMFVmeBRn8Z++KYzeUg2f7I=; b=dP4t5umjOjQ2A6XdekBj/DwTWmTnPE3yxvduRHVmq2c/ArJE2t7+5dsis3nevJSVPW 6oZkBTk5c4PiH4an7tYa07ZQK6ozyKCX4KkIEvYO4Hm/7XAAsKyDqVdQu1wFw2lexYKX gRWGLdT5ajpchZt9JkZobJ3+A2TSmLVK8kTfG9wapjBw/1IOQy119Xen72OMPwY0X81Y pACQcQVuwCrntw3sW+H0LspmKQjWK6IIL9EYCKPIsqwEchROlFevBug8TD2bWJzB3OVH WEnUsASTaMZ1QiGToy7Bed6dIPRRO3c9UBppLABC+QUSYROTtDodFwwOZRZHBLSNWx7B fopA== X-Gm-Message-State: APjAAAV9CqmUhIojkG8YQydjGz6e5ivj4TmWbDp9koiQKhVOoPhd2EEE 2Yo9a3ZAwG6WLHMT7U0tpzcXNFvR X-Google-Smtp-Source: APXvYqzsORyhVH/wP+p4oRpF5g8pikh/jHNS4kxtkzy6UQvW0IlfXjIKlFd6FlDdIzHl/TELv8U0+w== X-Received: by 2002:a17:90a:170a:: with SMTP id z10mr24674830pjd.86.1573338748912; Sat, 09 Nov 2019 14:32:28 -0800 (PST) Received: from [192.168.0.17] (24-113-193-150.wavecable.com. [24.113.193.150]) by smtp.gmail.com with ESMTPSA id w5sm10918081pfd.31.2019.11.09.14.32.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 09 Nov 2019 14:32:28 -0800 (PST) Subject: Re: Using multiple extended statistics for estimates From: Mark Dilger To: Tomas Vondra , PostgreSQL Hackers References: <20191028152048.jc6pqv5hb7j77ocp@development> <20191106195440.wtxsyhiwdidl37hv@development> <20191106195849.odhhc66xd23hw6hf@development> <97932e1d-53f1-bad3-efc2-1d98cab70727@gmail.com> Message-ID: <0da76f1f-5f9c-3ce3-3ac6-042d2a18430a@gmail.com> Date: Sat, 9 Nov 2019 14:32:27 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: <97932e1d-53f1-bad3-efc2-1d98cab70727@gmail.com> Content-Type: multipart/mixed; boundary="------------C20E82EBE3D91DB162A29120" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------C20E82EBE3D91DB162A29120 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit On 11/9/19 12:33 PM, Mark Dilger wrote: > > > On 11/6/19 11:58 AM, Tomas Vondra wrote: >> On Wed, Nov 06, 2019 at 08:54:40PM +0100, Tomas Vondra wrote: >>> 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. > > Tomas, > > Your patch compiles and passes the regression tests for me on debian > linux under master. > > Since your patch does not include modified regression tests, I wrote a > test that I expected to improve under this new code, but running it both > before and after applying your patch, there is no change. Ok, the attached test passes before applying your patch and fails afterward owing to the estimates improving and no longer matching the expected output. To be clear, this confirms your patch working as expected. I haven't seen any crashes in several hours of running different tests, so I think it looks good. -- Mark Dilger --------------C20E82EBE3D91DB162A29120 Content-Type: text/plain; charset=UTF-8; name="stats-ext-test.patch.2" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="stats-ext-test.patch.2" diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index b65228fa07..e89865e3ee 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -353,6 +353,48 @@ SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, d 500 | 50 (1 row) +-- muliple extended statistics +CREATE TABLE multistats ( + a INTEGER, + b INTEGER, + c INTEGER, + d INTEGER +); +-- Insert unique values +INSERT INTO multistats (a, b, c, d) + SELECT i, i*2, i*3, i*4 + FROM generate_series(1,10000) s(i); +-- Duplidate one set of values 10000 times +INSERT INTO multistats (a, b, c, d) + SELECT 0, 0, 0, 0 + FROM generate_series(1,10000) s(i); +-- estimates without mcv statistics +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + estimated | actual +-----------+-------- + 1250 | 10000 +(1 row) + +-- create some mcv statistics +CREATE STATISTICS ms_ab (mcv) ON a, b FROM multistats; +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + estimated | actual +-----------+-------- + 2500 | 10000 +(1 row) + +-- create some mcv statistics +CREATE STATISTICS ms_cd (mcv) ON c, d FROM multistats; +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + estimated | actual +-----------+-------- + 2500 | 10000 +(1 row) + +DROP TABLE multistats; -- functional dependencies tests CREATE TABLE functional_dependencies ( filler1 TEXT, diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index 040ee97a1e..ba802d7490 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -224,6 +224,41 @@ SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY b, c SELECT * FROM check_estimated_rows('SELECT COUNT(*) FROM ndistinct GROUP BY a, d'); +-- muliple extended statistics +CREATE TABLE multistats ( + a INTEGER, + b INTEGER, + c INTEGER, + d INTEGER +); + +-- Insert unique values +INSERT INTO multistats (a, b, c, d) + SELECT i, i*2, i*3, i*4 + FROM generate_series(1,10000) s(i); +-- Duplidate one set of values 10000 times +INSERT INTO multistats (a, b, c, d) + SELECT 0, 0, 0, 0 + FROM generate_series(1,10000) s(i); + +-- estimates without mcv statistics +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + +-- create some mcv statistics +CREATE STATISTICS ms_ab (mcv) ON a, b FROM multistats; + +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + +-- create some mcv statistics +CREATE STATISTICS ms_cd (mcv) ON c, d FROM multistats; + +ANALYZE multistats; +SELECT * FROM check_estimated_rows('SELECT * FROM multistats WHERE a = 0 AND b = 0 AND c = 0 AND d = 0'); + +DROP TABLE multistats; + -- functional dependencies tests CREATE TABLE functional_dependencies ( filler1 TEXT, --------------C20E82EBE3D91DB162A29120--