Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWoO-0001Ro-3W for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:19:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWoN-00029E-Lk for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:19:39 +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_2) (envelope-from ) id 1eHWoN-000294-G0 for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 15:19:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eHWoB-0005y4-T8 for pgsql-performance@postgresql.org; Wed, 22 Nov 2017 15:19:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id vAMFJNCU013427; Wed, 22 Nov 2017 10:19:23 -0500 From: Tom Lane To: Dmitry Shalashov cc: "pgsql-performance@postgresql.org" Subject: Re: Query became very slow after 9.6 -> 10 upgrade In-reply-to: References: Comments: In-reply-to Dmitry Shalashov message dated "Wed, 22 Nov 2017 17:13:39 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13425.1511363963.1@sss.pgh.pa.us> Date: Wed, 22 Nov 2017 10:19:23 -0500 Message-ID: <13426.1511363963@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Dmitry Shalashov writes: > BUT if I'll add to 3rd query one additional condition, which is basically > 2nd query, it will ran same 12 minutes: > SELECT adroom_id, domain_id, shows, clicks FROM adroom_stat WHERE day > between date_trunc('day', current_timestamp - interval '1 week') and > date_trunc('day', current_timestamp) AND domain_id IN (select distinct > unnest(domain_ids) FROM (select title, id, groups->0->>'provider' provider, > domain_ids from adroom where groups->0->>'provider' ~ '^target_mail_ru' and > not is_paused and current_timestamp between start_ts and stop_ts) t1) > Plan of last query: > Nested Loop (cost=88.63..25617.31 rows=491 width=16) (actual > time=3.512..733248.271 rows=1442797 loops=1) > -> HashAggregate (cost=88.06..88.07 rows=1 width=4) (actual > time=3.380..13.561 rows=3043 loops=1) > Group Key: (unnest(adroom.domain_ids)) > -> HashAggregate (cost=88.03..88.04 rows=1 width=4) (actual > time=2.199..2.607 rows=3043 loops=1) > Group Key: unnest(adroom.domain_ids) > -> ProjectSet (cost=0.28..87.78 rows=100 width=4) (actual > time=0.701..1.339 rows=3173 loops=1) Hm, seems like the problem is that that lower HashAggregate is estimated as having only one row out, which is way off and doesn't sound like a particularly bright default estimate anyway. (And then we're doing an additional HashAggregate on top of that, which is useless --- implies that something isn't realizing that the output of the SELECT DISTINCT is already distinct.) I'm suspicious that this is breakage from the work that was done on targetlist SRFs in v10, but that's just a guess at this point. Trying simple test queries involving WHERE x IN (SELECT DISTINCT unnest(foo) FROM ...), I do not see a behavior like this, so there is some not-very-obvious contributing factor in your situation. Can you put together a self-contained test case that produces a bogus one-row estimate? Extra points if it produces duplicate HashAgg steps. regards, tom lane