Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZCdlo-0000wX-1u for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Jul 2015 01:03:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZCdln-000756-FK for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Jul 2015 01:03:27 +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_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZCdll-00074D-90 for pgsql-hackers@postgresql.org; Wed, 08 Jul 2015 01:03:25 +0000 Received: from mail-wi0-f169.google.com ([209.85.212.169]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZCdlh-0007g4-Ot for pgsql-hackers@postgresql.org; Wed, 08 Jul 2015 01:03:24 +0000 Received: by widjy10 with SMTP id jy10so203244176wid.1 for ; Tue, 07 Jul 2015 18:03:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type :content-transfer-encoding; bh=CluAkjHcw9uVE6FyedUg0CWBLus5wkMqRumAMfXn00w=; b=kOwdq3/gSUTOEtEaKqhChvBmeUNExTxQx5cRH0316jsyMbkKvZyZHOtxe3Kdpse6HI U8+0C9i+pHduq6t+k4B7QOXk1exCzwf35G7KA2afidPtL7z4axxcv1/QR6MBv9TZMByj D/XONegc6poRgjwquhlE095rX4bxdsxeqO+HGeJVX8FutKv3k84l6xMz2lOZjDDf3Qqf Rvb5yGxuYMkC2UbMo9ygVX4dN/MRxM6z7pJeGvKwTGnQ8d1BZbMlGDkZK98WViiznolb +BpwNERV3e8yp+I5XxtsX6IwR2uiA5Uwmo07xhryezOBpC8LYC6KahC5yG8YoyUmd08r yKjw== X-Gm-Message-State: ALoCoQn4JmPuNx1zvR1B7Gq/tX7nrdETSciHFmRQU3iYH1fmAdLvEaGs98U1bX8KDmafymBh8tzJ X-Received: by 10.180.13.199 with SMTP id j7mr46739176wic.40.1436317399915; Tue, 07 Jul 2015 18:03:19 -0700 (PDT) Received: from [10.137.2.12] (ip-78-45-136-74.net.upcbroadband.cz. [78.45.136.74]) by smtp.gmail.com with ESMTPSA id b4sm139644wic.7.2015.07.07.18.03.18 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Jul 2015 18:03:18 -0700 (PDT) Message-ID: <559C76D4.2030805@2ndquadrant.com> Date: Wed, 08 Jul 2015 03:03:16 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Kyotaro HORIGUCHI CC: pgsql-hackers@postgresql.org, jeff.janes@gmail.com, sfrost@snowman.net Subject: Re: multivariate statistics / patch v7 References: <55638982.40904@2ndquadrant.com> <20150703.143034.132752108.horiguchi.kyotaro@lab.ntt.co.jp> <55982ED6.7000807@2ndquadrant.com> <20150707.150554.43512301.horiguchi.kyotaro@lab.ntt.co.jp> <559C2BD7.6040508@2ndquadrant.com> In-Reply-To: <559C2BD7.6040508@2ndquadrant.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org Hello Horiguchi-san! On 07/07/2015 09:43 PM, Tomas Vondra wrote: > -- histograms > ALTER TABLE t ADD STATISTICS (histogram) on (a,b,c); > ANALYZE t; > > EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 and c < 0.3; > Seq Scan on t (cost=0.00..23870.00 rows=267033 width=24) > (actual time=0.021..330.487 rows=273897 loops=1) > > EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 or c > 0.3; > Seq Scan on t (cost=0.00..23870.00 rows=14317 width=24) > (actual time=0.027..906.321 rows=966870 loops=1) > > EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 or c > 0.9; > Seq Scan on t (cost=0.00..23870.00 rows=20367 width=24) > (actual time=0.028..452.494 rows=393528 loops=1) > > This seems wrong, because the estimate for the OR queries should not be > lower than the estimate for the first query (with just AND), and it > should not increase when increasing the boundary. I'd bet this is a bug > in how the inequalities are handled with histograms, or how the AND/OR > clauses are combined. I'll look into that. FWIW this was a stupid bug in update_match_bitmap_histogram(), which initially handled only AND clauses, and thus assumed the "match" of a bucket can only decrease. But for OR clauses this is exactly the opposite (we assume no buckets match and add buckets matching at least one of the clauses). With this fixed, the estimates look like this: EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 and c < 0.3; Seq Scan on t (cost=0.00..23870.00 rows=267033 width=24) (actual time=0.102..321.524 rows=273897 loops=1) EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 or c < 0.3; Seq Scan on t (cost=0.00..23870.00 rows=319400 width=24) (actual time=0.103..386.089 rows=317533 loops=1) EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 or c > 0.3; Seq Scan on t (cost=0.00..23870.00 rows=956833 width=24) (actual time=0.133..908.455 rows=966870 loops=1) EXPLAIN ANALYZE select * from t where a < 0.3 and b < 0.3 or c > 0.9; Seq Scan on t (cost=0.00..23870.00 rows=393633 width=24) (actual time=0.105..440.607 rows=393528 loops=1) IMHO pretty accurate estimates - no issue with OR clauses. I've pushed this to github [1] but I need to do some additional fixes. I also had to remove some optimizations while fixing this, and will have to reimplement those. That's not to say that the handling of OR-clauses is perfectly correct. After looking at clauselist_selectivity_or(), I believe it's a bit broken and will need a bunch of fixes, as explained in the FIXMEs I pushed to github. [1] https://github.com/tvondra/postgres/tree/mvstats kind regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers