Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eHWci-0000nA-MA for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:07:36 +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 1eHWch-0002dx-IY for pgsql-performance@arkaria.postgresql.org; Wed, 22 Nov 2017 15:07:35 +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 1eHWch-0002dE-90 for pgsql-performance@lists.postgresql.org; Wed, 22 Nov 2017 15:07:35 +0000 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eHWcc-0005g5-FS for pgsql-performance@postgresql.org; Wed, 22 Nov 2017 15:07:34 +0000 Received: by mail-wm0-x233.google.com with SMTP id r68so11084054wmr.3 for ; Wed, 22 Nov 2017 07:07:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=j8vXsN8aU3QKBNtLYSFZ7Y6KxqBjEnmliWAs/aGKNw0=; b=PiEEwjNEo2VcHgluLBJfJGbFsoRA6HUm6rEfZug/roQLGsjxEEaN8yDMg4+J9/oTxV 13aEbT3J6GkkXuDP08X/8EtY1EChYHTm4TgfEMOVewvGN8vv/Nby4FO9PD5Tz4R5XDv3 1Phz0SZlT578YazZwQG+8mZPhyECC0e+sGKlK0X7Y5Yye5dDntz4eazCZtXSIk93ELYa N8rnEpnld7rQEUYzlpO+xZH3TcIT8jWqveJcCYWzdjtVgaWOd/tCNi+D8yWVaeJcAuEb klE2u37fN+Hm5Eo6IN2D0ffHIcoD+CrOtQp2RR3DFqqvOp2whWSNCrGQW3I9ZMfDUN1e eSyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=j8vXsN8aU3QKBNtLYSFZ7Y6KxqBjEnmliWAs/aGKNw0=; b=ZaYSnO74fADoWoL/gNWr7fY3jJxFncU9Y4hcIEpkY7BFUG3akVurZCGgpP5YBu6cHp IGd0t9l103OQWov9qOYVwTJNoN5UOoX7h+WdNYs1UY6QGqvKSfzbmQrWzzvURhz/DmVI iUlNYSvRWcZSMLnR+UeA5blhkWp/lqfCm0tHNaStKWGse1HwnG20Zj2gMylTbwMTGvy+ UElJeVz+ci12RH1eoyZteIadDfwlACFVKNDmpnPMZ8wHXwm/x/XiLszQ/JtxMBZMPTxP +OGAh+Z2mYZ2HqIgoRycsYpRufss2+XYoxppGLHi7GSD86dpBT1SfB2+jEuWu4iwlrKX COIA== X-Gm-Message-State: AJaThX48Gr3TjIaxJ7klwEq+Xuv3NE9xyh3y/DdjDxfcFwdeHsgiiTqW bL3R+UrqOYWA8MvSlF6aPHJ2k/2nsVhRhOLdIS5ayGYNk/igB+HsK5+/Hr8W7HJDisxbMyfpxNp ASxEM1OsBHAIxorUVjlIAj3hJzU+50Op13DZsDJPy+/ppxcKBU3b7MTMEN8ytL/HfSSizZoH1+N /KYIO3XS/VrRltyw== X-Google-Smtp-Source: AGs4zMaoBgwjjav5ISdYlxo+Oz2iEr26x2/4s7qE3ecNl3DDXr1mEksOzoZw+aYPPO9MuDOgg3x0lA== X-Received: by 10.80.173.56 with SMTP id y53mr29733421edc.202.1511363248654; Wed, 22 Nov 2017 07:07:28 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id 3sm11010829edv.50.2017.11.22.07.07.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Nov 2017 07:07:27 -0800 (PST) Subject: Re: Query became very slow after 9.6 -> 10 upgrade To: Dmitry Shalashov , Alex Ignatov Cc: "pgsql-performance@postgresql.org" References: <1379201d3639d$8b61bcd0$a2253670$@postgrespro.ru> <137da01d363a0$603cc970$20b65c50$@postgrespro.ru> From: Tomas Vondra Message-ID: Date: Wed, 22 Nov 2017 16:07:26 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk IMHO the problems here are due to poor cardinality estimates. For example in the first query, the problem is here: -> Nested Loop (cost=0.42..2.46 rows=1 width=59) (actual time=2.431..91.330 rows=3173 loops=1) -> CTE Scan on b (cost=0.00..0.02 rows=1 width=40) (actual time=2.407..23.115 rows=3173 loops=1) -> Index Scan using domains_pkey on domains d (cost=0.42..2.44 rows=1 width=19) (actual time=0.018..0.018 rows=1 loops=3173) That is, the database expects the CTE to return 1 row, but it returns 3173 of them, which makes the nested loop very inefficient. Similarly for the other query, where this happens: 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) That is, about 1:3000 difference in both cases. Those estimation errors seem to be caused by a condition that is almost impossible to estimate, because in both queries it does this: groups->0->>'provider' ~ '^something' That is, it's a regexp on an expression. You might try creating an index on the expression (which is the only way to add expression statistics), and reformulate the condition as LIKE (which I believe we can estimate better than regular expressions, but I haven't tried). So something like CREATE INDEX ON adroom ((groups->0->>'provider')); WHERE groups->0->>'provider' LIKE 'something%'; regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services