Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eimEV-0000Nc-Gz for pgsql-performance@arkaria.postgresql.org; Mon, 05 Feb 2018 19:15:15 +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 1eimEU-0001TL-5E for pgsql-performance@arkaria.postgresql.org; Mon, 05 Feb 2018 19:15:14 +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 1eimET-0001T3-UW for pgsql-performance@lists.postgresql.org; Mon, 05 Feb 2018 19:15:13 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eimEP-00046Y-TU for pgsql-performance@lists.postgresql.org; Mon, 05 Feb 2018 19:15:13 +0000 Received: by mail-wm0-x244.google.com with SMTP id 143so28153714wma.5 for ; Mon, 05 Feb 2018 11:15:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=9HdEPlvBNLkPRE9ue54TmcWttA7XJfAGQmumTAcaXfA=; b=c9HwIPK+56X23FvAy39X6/+oKZrijOlvQFwLqKyBD6d0sW+LIsqmw4+gv6vo14vzLm GwNpLiTqMFMnh8thalGRqGRbl2j9VIXhng3EljB/WmiKeT2a4h3LBTSFQF7/P8V06kdm GrejSYZNCNzNLxnHJivnqD4l2DJQuGyr3YBA/jtt0fUYEai0abWbpDT0pidRzzlca08K PlTSnPzr/xPo7b4LaSHZe5e2/nTwrVPj83ENQoOEM6am+GRkHFZDj8+xdnF3PHbWhsmH KgDbiMfFf0TRp9KaxtepLQZhR3Xw/YrrQl9x1JdYvGWjqjuoJH1hfblSu6tInJkEvSoQ gj7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=9HdEPlvBNLkPRE9ue54TmcWttA7XJfAGQmumTAcaXfA=; b=QWqxawHzSIg6gg3qORPVpoPs939d3Ns1owqfl1LYFH1/WyO5Z1hN58Z0cF+QEr1Z1k HLkgRDpslaP6wTar66LT7T3fgbuFsw5zTPoNHxQKuYpgKNbX3F/rRA5Y4Q+ddv5CUqpo CzCrGm3iNRyMyyS22+tb1dsfMfaFVFq5wEQOBnjIcwIeEhaJ948JxWwrgk7TGx081maz 7+LsfRf2KHdgfipSRHDAbec16Zm7iqpghObuJiG/kovGa7zeeY5wsVlOndXLYq8HzKmb ZHa/mBVIqudqLQ5PBuVRZObCUk/OGCE3BZ2Pg5jgfuLDvnnZRjepOzQ0gK4KY7HAxDSA SS/w== X-Gm-Message-State: APf1xPCtJtN33+I0dv4GOFyGo7bqV9KBwd+SoiI3ESE7a6zQ19EOmNi2 lQ0UhBTdBXeDxgG5L/gxByeAR6xqdq3wM2SG3d4lBZMQzpezGMk/3HoZppYfxz41uyg7Z+6JrdH ptYc1WODaLp1PHhq6E/5NSD5gMhsCppPNZ8QCYeSKaaKzhKLSVL6zl7VEP1GybbPgpEiAg3s7wD HRiw+xTZeCX//1xBybVs0fJyGM X-Google-Smtp-Source: AH8x224VveN9dTZ+/jzjeAywWYSK3I5ff2Kwlk+dih4DUGlWwJrFD22Q0rnkrSzpfAJPby0ct60SGw== X-Received: by 10.28.203.135 with SMTP id b129mr247375wmg.155.1517858107950; Mon, 05 Feb 2018 11:15:07 -0800 (PST) Received: from [10.137.2.17] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id r189sm13317290wmd.39.2018.02.05.11.15.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 11:15:06 -0800 (PST) Subject: Re: SV: bad plan using nested loops To: Johan Fredriksson , pgsql-performance@lists.postgresql.org References: <1517481727.23675.3.camel@kth.se> <23735.1517497241@sss.pgh.pa.us> <1517517264764.92820@kth.se> <1517562127.315.1.camel@kth.se> From: Tomas Vondra Message-ID: Date: Mon, 5 Feb 2018 20:15:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <1517562127.315.1.camel@kth.se> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/02/2018 10:02 AM, Johan Fredriksson wrote: > tor 2018-02-01 klockan 20:34 +0000 skrev Johan Fredriksson: >>> Johan Fredriksson writes: >>>> Bad plan: https://explain.depesz.com/s/avtZ >>>> Good plan: https://explain.depesz.com/s/SJSt >>>> Any suggestions on how to make the planner make better decisions >>>> for >>>> this query? >>> >>> Core of the problem looks to be the misestimation here: >>> >>>        Index Only Scan using shredder_cgm1 on >>> public.cachedgroupmembers cachedgroupmembers_4 >>> (cost=0.43..2.33 rows=79 width=8) (actual time=0.020..0.903 >>> rows=1492 loops=804) >>>          Output: cachedgroupmembers_4.memberid, >>> cachedgroupmembers_4.groupid, >>> cachedgroupmembers_4.disabled >>>          Index Cond: ((cachedgroupmembers_4.memberid = >>> principals_1.id) AND >>> (cachedgroupmembers_4.disabled = 0)) >>>          Heap Fetches: 5018 >>> >>> Probably, memberid and disabled are correlated but the planner >>> doesn't >>> know that, so it thinks the index condition is way more selective >>> than it >>> actually is.  In PG 10, you could very possibly fix that by >>> installing >>> extended statistics on that pair of columns.  See >>> >>> https://www.postgresql.org/docs/current/static/planner-stats.html#P >>> LANNER-STATS-EXTENDED >> >> I'm not sure what you mean by correlated, but there are only a >> handful (164 when I check it) disabled groupmembers out of total 7.5 >> million. >> I'll give CREATE STATISTICS on those columns a shot and see if it >> gets any better. > > It looks like you are right, Tom. There actually exists full > correlation between memberid, groupid and disabled. > > rt4=# SELECT stxname, stxkeys, stxdependencies FROM pg_statistic_ext; >  stxname  | stxkeys |   stxdependencies     > -----------+---------+---------------------- >  cgm_stat2 | 2 6     | {"2 > => 6": 1.000000} >  cgm_stat1 | 3 6     | {"3 => 6": 1.000000} > (2 rows) > > However, this does not help the planner. It still picks the bad plan. > Yeah :-( Unfortunately, we're not using the extended statistics to improve join cardinality estimates yet. PostgreSQL 10 can only use them to improve estimates on individual tables, and judging by the progress on already submitted improvements, it doesn't seem very likely to change in PostgreSQL 11. regards Tomas -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services