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 1iHa0q-00060a-W9 for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Oct 2019 20:53:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iHa0p-0007P3-Ss for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Oct 2019 20:53:47 +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 1iHa0p-0007Ov-Ft for pgsql-bugs@lists.postgresql.org; Mon, 07 Oct 2019 20:53:47 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iHa0m-0002V0-7Z for pgsql-bugs@lists.postgresql.org; Mon, 07 Oct 2019 20:53:47 +0000 Received: by mail-wr1-x444.google.com with SMTP id o18so16838109wrv.13 for ; Mon, 07 Oct 2019 13:53:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=fTBjq97p/M/mfAUwq4289gBQP/9vZH9jFvvPtnaZrqE=; b=C86OSFmM2ogVe/D5TEPaL9BFzWV8eQ2DnJPCP84vr6ghlevSVh1FZmnVyhPSDNuhSG CZs7tkfGJWaWrUi5MRdfLcu6rWEJzaQbb7FclLxDM0I8yZH1h5yNrKl65QpaK+7u7a3o x97apwfT1NYD/mrT+wTFHwL3puANcWPYq6MncBL435W9a/qnQJYyhH0+Fo/ThH+LXZ9h jC2NHI7lWow3jwFXOZYRnEwlQo4QlJ7E6VW1VN+R7xclUMtBVeeaezZEfcz/jU3Jv4to OJex0iCajmD9eQCeKH2T114TvXHhJ5eY/I0wRB4UFnOsylpz25QWhVnetR3KxyBNWoof TKiw== 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:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=fTBjq97p/M/mfAUwq4289gBQP/9vZH9jFvvPtnaZrqE=; b=nWIKCnyNMJMIGVS0AFGk1TgfdkmIUtwssjqfqgvEck/cP+UOr8ps6zbbjumINSJcCJ 2U9uNLP7VwHnZqEkI9KN45DbQwqCjmGYwJ17xGhuKKHLY5TG1coJmb4kjaQi8LtboTGZ DF7iimaA8TOKTNyyzlcYa2tpDw/qg9Y3OwoX5clrfXDCa/xkvmO/LKp8iKgHfS4FMYxp cgPfYrCRadp6mMFmO4mg9aDwEF+hclK6R7UrLaOiMAOP+2DfPHU+3ss+HlYICNf1ldjY eyLrrVRzaOr0KJGU9qJ4jzPje0WZDl9R7SVF2iXF1L5Yjvt0nkPSbPx1niZ/KqkMgXfI 26Ow== X-Gm-Message-State: APjAAAUxRMTJYinwVPGa8/SGDcoVXUCyoT8usR5VTULzqkXHlFKTwFMR 7jhtWJMWvh0u3gSQCm48fn2Q4DYSdxo= X-Google-Smtp-Source: APXvYqxA8n8DR7cjbbBNGgo2sr4QyjJHSkaZvtkr5G1vG28bf0L7yLBZzCEb6/tsgy69KKd2RsNghQ== X-Received: by 2002:adf:f0ce:: with SMTP id x14mr10331995wro.263.1570481623062; Mon, 07 Oct 2019 13:53:43 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id t123sm491320wma.40.2019.10.07.13.53.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 13:53:42 -0700 (PDT) Date: Mon, 7 Oct 2019 22:53:41 +0200 From: Tomas Vondra To: David Raymond Cc: Andrew Gierth , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #16031: Group by returns duplicate groups Message-ID: <20191007205341.koxjl5etfoqg4y7l@development> References: <871rvxm4je.fsf@news-spur.riddles.org.uk> <87sgoclcux.fsf@news-spur.riddles.org.uk> <87h84sl1b5.fsf@news-spur.riddles.org.uk> <87a7ae0x11.fsf@news-spur.riddles.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On Mon, Oct 07, 2019 at 06:29:04PM +0000, David Raymond wrote: >Downloaded and installed 12.0, created a nice shiny new cluster, and >confirmed that it's still doing it. Now in 12 you have to force it to >materialize the CTE, which was why I had used a CTE in 11 in the first >place. > I've tried running this on a 12.0 cluster too (on Linux though), using the data set linked by Andrew, and I haven't observed any duplicate rows either. I do have some observations, though ... > >testing=> select version(); > version >------------------------------------------------------------ > PostgreSQL 12.0, compiled by Visual C++ build 1914, 64-bit >(1 row) > >Time: 0.148 ms >testing=> \d+ weird_grouping > Table "name_stuff.weird_grouping" > Column | Type | Collation | Nullable | Default | Storage | Stats target | Description >--------+------------------------+-----------+----------+---------+----------+--------------+------------- > name | character varying(254) | | not null | | extended | | >Access method: heap > >testing=> analyze verbose weird_grouping; >INFO: analyzing "name_stuff.weird_grouping" >INFO: "weird_grouping": scanned 8316 of 8316 pages, containing 1297265 live rows and 0 dead rows; 30000 rows in sample, 1297265 estimated total rows >ANALYZE >Time: 206.577 ms >testing=> select count(*), count(distinct name) from weird_grouping; > count | count >-----------+----------- > 1,297,265 | 1,176,103 >(1 row) > I do get this: count | count ---------+--------- 1297265 | 1176101 (1 row) >Time: 6729.011 ms (00:06.729) >testing=> with foo as (select name from weird_grouping group by name) select name from foo group by name having count(*) > 1; > name >------ >(0 rows) > >Time: 7289.128 ms (00:07.289) >testing=> explain (analyze, verbose, costs, settings, buffers, timing, summary) with foo as (select name from weird_grouping group by name) select name from foo group by name having count(*) > 1; > QUERY PLAN >-------------------------------------------------------------------------------------------------------------------------------------------------------- > GroupAggregate (cost=179613.72..200268.04 rows=67 width=20) (actual time=6203.868..6203.868 rows=0 loops=1) > Output: weird_grouping.name > Group Key: weird_grouping.name > Filter: (count(*) > 1) > Rows Removed by Filter: 1176103 > Buffers: shared hit=2464 read=5852, temp read=7800 written=7830 > -> Group (cost=179613.72..186100.05 rows=944366 width=20) (actual time=4769.781..5985.111 rows=1176103 loops=1) > Output: weird_grouping.name > Group Key: weird_grouping.name > Buffers: shared hit=2464 read=5852, temp read=7800 written=7830 > -> Sort (cost=179613.72..182856.89 rows=1297265 width=20) (actual time=4769.779..5844.350 rows=1297265 loops=1) > Output: weird_grouping.name > Sort Key: weird_grouping.name > Sort Method: external merge Disk: 39048kB > Buffers: shared hit=2464 read=5852, temp read=7800 written=7830 > -> Seq Scan on name_stuff.weird_grouping (cost=0.00..21288.65 rows=1297265 width=20) (actual time=0.059..102.772 rows=1297265 loops=1) > Output: weird_grouping.name > Buffers: shared hit=2464 read=5852 > Settings: search_path = 'name_stuff' > Planning Time: 0.048 ms > Execution Time: 7115.761 ms >(21 rows) > I'd like to point this may not be actually correct either. It does not produce any rows, i.e. it does not see any "duplicate groups", but it does this: -> Group (cost=179613.72..186100.05 rows=944366 width=20) (actual time=4769.781..5985.111 rows=1176103 loops=1) Notice that it has 1176103 groups, but is that actually correct? On my machine I only see 1176101 groups, and the difference (2) would match the DCL and DELTA duplicates with the next query. >Time: 7116.170 ms (00:07.116) >testing=> with foo as materialized (select name from weird_grouping group by name) select name from foo group by name having count(*) > 1; > name >------- > DCT > DELTA >(2 rows) > >Time: 8850.833 ms (00:08.851) >testing=> explain (analyze, verbose, costs, settings, buffers, timing, summary) with foo as materialized (select name from weird_grouping group by name) select name from foo group by name having count(*) > 1; > QUERY PLAN >---------------------------------------------------------------------------------------------------------------------------------------------------------- > HashAggregate (cost=209709.20..209711.70 rows=67 width=516) (actual time=6676.811..6768.094 rows=2 loops=1) > Output: foo.name > Group Key: foo.name > Filter: (count(*) > 1) > Rows Removed by Filter: 1176099 > Buffers: shared hit=2528 read=5788, temp read=7800 written=12363 > CTE foo > -> Group (cost=179613.72..186100.05 rows=944366 width=20) (actual time=4774.681..6004.725 rows=1176103 loops=1) > Output: weird_grouping.name > Group Key: weird_grouping.name > Buffers: shared hit=2528 read=5788, temp read=7800 written=7830 > -> Sort (cost=179613.72..182856.89 rows=1297265 width=20) (actual time=4774.678..5860.270 rows=1297265 loops=1) > Output: weird_grouping.name > Sort Key: weird_grouping.name > Sort Method: external merge Disk: 39048kB > Buffers: shared hit=2528 read=5788, temp read=7800 written=7830 > -> Seq Scan on name_stuff.weird_grouping (cost=0.00..21288.65 rows=1297265 width=20) (actual time=0.065..101.141 rows=1297265 loops=1) > Output: weird_grouping.name > Buffers: shared hit=2528 read=5788 > -> CTE Scan on foo (cost=0.00..18887.32 rows=944366 width=516) (actual time=4774.683..6228.002 rows=1176103 loops=1) > Output: foo.name > Buffers: shared hit=2528 read=5788, temp read=7800 written=12363 > Settings: search_path = 'name_stuff' > Planning Time: 0.054 ms > Execution Time: 8786.597 ms >(25 rows) > >Time: 8787.011 ms (00:08.787) >testing=> > Can you try running this with enable_hashagg = off? That should give you another Sort on the CTE Scan, and a GroupAggregate at the top. I wonder if that makes the issue go away ... regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services