Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQXl5-0006PI-9L for pgsql-general@arkaria.postgresql.org; Wed, 06 Jun 2018 12:41:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQXl0-0006ll-1h for pgsql-general@arkaria.postgresql.org; Wed, 06 Jun 2018 12:41:42 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQXkz-0006lb-Mf for pgsql-general@lists.postgresql.org; Wed, 06 Jun 2018 12:41:41 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fQXks-0003Yo-36 for pgsql-general@postgresql.org; Wed, 06 Jun 2018 12:41:40 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1fQXii-0004yL-97 for pgsql-general@postgresql.org; Wed, 06 Jun 2018 14:39:20 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-general@postgresql.org From: Thomas Kellerer Subject: Using distinct in an aggregate prevents parallel execution? Date: Wed, 6 Jun 2018 14:41:25 +0200 Lines: 33 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 X-Mozilla-News-Host: news://news.gmane.org:119 Content-Language: en-GB X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Consider this simplified example: select c.id, count(*) as total_orders, sum(p.price) as total_value from customer c join orders o ON c.id = o.customer_id join order_line ol ON o.id = ol.order_id join product p ON ol.product_id = p.id group by c.id; This uses parallel execution quite nicely: https://explain.depesz.com/s/aSPNn However, the query is incorrect as it does not count the number of orders, but (essentially) the number of order_lines. This can easily be fixed using: select c.id, count(distinct o.id) as total_orders, sum(p.price) as total_value from customer c join orders o ON c.id = o.customer_id join order_line ol ON o.id = ol.order_id join product p ON ol.product_id = p.id group by c.id; But in that case Postgres 10.4 decides to no longer use parallel execution: https://explain.depesz.com/s/7Ua3 Which increases the query execution time quite a bit (from 3 to 8 seconds). Is this a known limitation? Thomas