Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aZ6kw-0000M2-82 for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Feb 2016 00:59:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aZ6kv-0005tA-QJ for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Feb 2016 00:59:41 +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 1aZ6ku-0005sW-Kh for pgsql-hackers@postgresql.org; Fri, 26 Feb 2016 00:59:40 +0000 Received: from mail-wm0-x22c.google.com ([2a00:1450:400c:c09::22c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aZ6kr-0005Ox-3h for pgsql-hackers@postgresql.org; Fri, 26 Feb 2016 00:59:40 +0000 Received: by mail-wm0-x22c.google.com with SMTP id c200so52446505wme.0 for ; Thu, 25 Feb 2016 16:59:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=rhRNhcw9urjXkB9CFo3suV8GcGX4NboBP8UPJXF6Juk=; b=ekCVHOD/QoQLCxUgvlvlwz+Tf5Al4dP7HEtIGoa7XX6Pf6ztknb+dg1DB/2x3kwu0N b0JEmLajdspXRnr4ld5Nem+vfML64+W49e5VbL8vt4ZFT7I6VBo7iYPENFmPzdglpDe3 SBvU56z1FMI7FSE9Nrr5VsVR1TFgyYBfz0maEtkOhflIgNBz2guzO9Ujccdvum4hWK4L fB/f+2znvFZTCwYY5bMAv1hQiS1oYkWecI1VY5WQ1mmpzgnX+bRcwCJFy834rTRoo4pB Uwum1EMvXcE/Hj/fqQEl2A3ovPqGUZyMeirgsM6t4DHr8wJP2Gme6sRVZvQFqqsn+ML7 M0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=rhRNhcw9urjXkB9CFo3suV8GcGX4NboBP8UPJXF6Juk=; b=I/KQsZ9YQ8MQL3nFLC1snOBzDbjRP/lhtdu6XYnh+3z9oKpihES5AvXMsUzjnw9Ees EZN4GaPa+kxHBFukZh37kTXQBjrJsiwEPyINi1Am+TJAxZgwhcul4zwgotq0DHAKjnFZ 3I+gDkNocgmHJib6ODvChg33SbbiTm1TfW9tIUAmv0GjR+dtjjCwA8Gb+ffSCnWSUP8Q Ypsf8fsudywFaX0acbEoKr2g9CglAHRjyGl7BtGXaFhb4viD34e8pHOiciQ5iJzbLFiX xoDCb9dxeYYGFUeTEIHci2oxsMy2qbvlPgIgp/NljLyOoSqPdPnLaCVNTuHAWvbbWHBt TJJw== X-Gm-Message-State: AD7BkJKv8tA+IYOzB8VyQGtnJbH40nyfemxUgdxXW4ndccij7O5uPuKrV/VPvTY98GCdnvGwyRoRWbfSHiRHrq+OMwBfiCbD02HXYWCiLQDyrr7oZHszLAtrQdil2YskHG8YNfiyhSVh8pPzEORzpViz3ZyWJzAxNtqzfHRGCWH2nn7LdS7sj794O4dgQ8OhUkHV4KBa8J8X X-Received: by 10.28.60.11 with SMTP id j11mr177559wma.99.1456448375602; Thu, 25 Feb 2016 16:59:35 -0800 (PST) Received: from [10.137.1.13] (ip-78-45-216-26.net.upcbroadband.cz. [78.45.216.26]) by smtp.gmail.com with ESMTPSA id jo6sm10225371wjb.48.2016.02.25.16.59.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 Feb 2016 16:59:34 -0800 (PST) Subject: Re: improving GROUP BY estimation To: Mark Dilger References: <56CD0381.5060502@2ndquadrant.com> <03A3B58D-6A2D-44FE-9B03-E21948CFCBAA@gmail.com> Cc: PostgreSQL-development From: Tomas Vondra Message-ID: <56CFA373.3060105@2ndquadrant.com> Date: Fri, 26 Feb 2016 01:59:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: <03A3B58D-6A2D-44FE-9B03-E21948CFCBAA@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) 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 Hi, On 02/26/2016 12:16 AM, Mark Dilger wrote: > > It is not hard to write test cases where your patched version overestimates > the number of rows by a very similar factor as the old code underestimates > them. My very first test, which was not specifically designed to demonstrate > this, happens to be one such example: > > > CREATE TABLE t (a INT, b int); > INSERT INTO t SELECT sqrt(gs)::int, gs FROM generate_series(1,10000000) gs; > ANALYZE t; > EXPLAIN SELECT a FROM t WHERE b < 1000 GROUP BY a; > QUERY PLAN > --------------------------------------------------------------- > HashAggregate (cost=169250.21..169258.71 rows=850 width=4) > Group Key: a > -> Seq Scan on t (cost=0.00..169247.71 rows=1000 width=4) > Filter: (b < 1000) > (4 rows) > > SELECT COUNT(*) FROM (SELECT a FROM t WHERE b < 1000 GROUP BY a) AS ss; > count > ------- > 32 > (1 row) > > > > So, it estimates 850 rows where only 32 are returned . Without > applying your patch, it estimates just 1 row where 32 are returned. > That's an overestimate of roughly 26 times, rather than an > underestimate of 32 times. The culprit here is that the two columns are not independent, but are (rather strongly) correlated due to the way you've generated the data. In cases like this (with correlated columns), it's mostly just luck how accurate estimates you get, no matter which of the estimators you use. It's pretty easy to generate arbitrary errors by breaking the independence assumption, and it's not just this particular place of the planner. And it won't change until we add some sort of smartness about dependencies between columns. I think over-estimates are a bit more acceptable in this case, e.g. because of the HashAggregate OOM risk. Also, I've seen too many nested loop cascades due to under-estimates recently, but that's a bit unrelated. > As a patch review, I'd say that your patch does what you claim it > does, and it applies cleanly, and passes the regression tests with > my included modifications. I think there needs to be some discussion > on the list about whether the patch is agood idea. Yes, that's why I haven't bothered with fixing the regression tests in the patch, actually. 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