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 1ehCJz-0000pY-6X for pgsql-performance@arkaria.postgresql.org; Thu, 01 Feb 2018 10:42:23 +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 1ehCJx-0005UD-Lb for pgsql-performance@arkaria.postgresql.org; Thu, 01 Feb 2018 10:42:21 +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 1ehCJx-0005U4-Bh for pgsql-performance@lists.postgresql.org; Thu, 01 Feb 2018 10:42:21 +0000 Received: from smtp-4.sys.kth.se ([2001:6b0:1:1300:250:56ff:fea6:2de3]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ehCJq-0000xM-NS for pgsql-performance@lists.postgresql.org; Thu, 01 Feb 2018 10:42:20 +0000 Received: from smtp-4.sys.kth.se (localhost.localdomain [127.0.0.1]) by smtp-4.sys.kth.se (Postfix) with ESMTP id 876B0276F for ; Thu, 1 Feb 2018 11:42:12 +0100 (CET) X-Virus-Scanned: by amavisd-new at kth.se Received: from smtp-4.sys.kth.se ([127.0.0.1]) by smtp-4.sys.kth.se (smtp-4.sys.kth.se [127.0.0.1]) (amavisd-new, port 10024) with LMTP id f4lByNH39gzB for ; Thu, 1 Feb 2018 11:42:08 +0100 (CET) X-KTH-Auth: eskil [2001:6b0:1:11f0:6e62:6dff:fe6a:ffa3] DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kth.se; s=default; t=1517481728; bh=s411DUrv5jQ5ZMKhhbY4xHrZHmacaxyip7eZ6HfO48I=; h=Subject:From:To:Date; b=Qqep8qTBuT4vcq5EXpxcLNS4d2kesrKIkcT7lQGkkiaUeN1PtrgvO3QAa6N483ks8 JPKuE8XpytLt/0luxn8J36Bk/Zq3gphE8gggwbYxgIosJEmNOQ/0iHd9pucTO3pQ3C XE90B9zwyG1uyYGwNSo3LdjkrfGiZKTlnfpWISmc= X-KTH-mail-from: eskil@kth.se X-KTH-rcpt-to: pgsql-performance@lists.postgresql.org Received: from tiny.ite.kth.se (tiny.ite.kth.se [IPv6:2001:6b0:1:11f0:6e62:6dff:fe6a:ffa3]) by smtp-4.sys.kth.se (Postfix) with ESMTPSA id 3C20E2619 for ; Thu, 1 Feb 2018 11:42:07 +0100 (CET) Message-ID: <1517481727.23675.3.camel@kth.se> Subject: bad plan using nested loops From: Johan Fredriksson To: pgsql-performance@lists.postgresql.org Date: Thu, 01 Feb 2018 11:42:07 +0100 Organization: KTH Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.22.6 (3.22.6-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello! I brought this issue up about two years ago but without getting any real explanation or solution. The problem is that PostgreSQL does really bad plans using nested loops. With "enable_nestloop = 0" the same query is run about 20 times faster. The sugested solution I got back then was to upgrade to the latest version of PostgreSQL (then 9.5). It did not help. The solution we finally applied was a horribly ugly patch to the perl-module SearchBuilder that recognized queries that would perform badly and put them inside transaction blocks with "SET LOCAL enable_nestloop = 0". Last week I upgraded PostgreSQL for this application (Request Tracker) to version 10.1 and just for fun I decied to test to remove the patch to see if the problem still persisted. For two cases it did not. The planner handled them just fine. For one case however, the same problem still remains. 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? / Eskil