Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iRU79-0007zo-82 for pgsql-performance@arkaria.postgresql.org; Mon, 04 Nov 2019 04:37:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iRU76-0006q8-RG for pgsql-performance@arkaria.postgresql.org; Mon, 04 Nov 2019 04:37:12 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iRU76-0006q1-C7 for pgsql-performance@lists.postgresql.org; Mon, 04 Nov 2019 04:37:12 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iRU6y-00084h-ER for pgsql-performance@lists.postgresql.org; Mon, 04 Nov 2019 04:37:10 +0000 Received: by mail-wm1-x343.google.com with SMTP id 8so7540883wmo.0 for ; Sun, 03 Nov 2019 20:37:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=LpQcIVbFsUvgdljQS4usmTEJdpkd2U4cWICScBas0LM=; b=KZA19qZYEfn41PrSFuRcIju6i1ukBFwRPL1zoyo5i4xOB0TWl2B6fX+QuIVCXBF+We CpoFfgUMugkqQEHq2OKIWPa8MLpaeImSwhIrj5do4FT0DI3C2H1LfeG52xqsS1WIhiY4 q71Al+D30RUqFszu+f88ut3zD74rJQfEBds76Jh2bEa1IFPFz4/UcHrbHEIj+x1cSSGM IQttsN3OSOxxt9CHl3SDeC2dAT1JIxkYMdQpjWVCnnr6N0DdnDhUPt1AdG1uEYC5smgo KEDf7wLWL/jUw7NWrYFQeFMEUUQFtKlEUuV96NR+KggO902DhJUlsKT40OMa5Qpau5v0 /SWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=LpQcIVbFsUvgdljQS4usmTEJdpkd2U4cWICScBas0LM=; b=lhvbV1j6jEfCm3KH5Kc0abL7AkpUykVrBxs10+F8XauOzpl07VsrhOct8UY3nctWqD xn+K18teT504cGHBq5Kv8FKRZuPCZ70gMyxUv1q4r7uJWtrieiIsm3pBOPY4ZFL0ki0j YXlWqlQxfeKICPDfY2c37GbX+Q9AVHbUcOF4T6gNpa1vur1t1ACeX2ibTLz3CkXCsRtp QC8pC1kPQXf3rhrRJUx+ZglxSxHdGutWTjAoy7G6uWgMDIn+xbhu1TVk84m1Ui+IK9WV FrX0Ej38I3PyLWRqKVI8zyGsQX4TztxqY1KRgcYLwh5zuNh6C7+NZ5KvVaeNrKzF9woB wBwQ== X-Gm-Message-State: APjAAAXg/+1i36sWZMPlC3C7S36SQxSysaiTCANTcibaoTGcGzCGl0Pf uEif3Xz/iUuyfBER3YKs1eNAVQ== X-Google-Smtp-Source: APXvYqwPyfzKZFM4vlzmqcjA/k1mOCoXQg8n0k8lC1j4y6jN4ooyxu/+UtypPBiu1lBVJ7RqXAWvFA== X-Received: by 2002:a7b:cbd9:: with SMTP id n25mr13988866wmi.64.1572842221915; Sun, 03 Nov 2019 20:37:01 -0800 (PST) Received: from localhost.localdomain (217-149-168-182.nat.highway.telekom.at. [217.149.168.182]) by smtp.gmail.com with ESMTPSA id u1sm12243816wrp.56.2019.11.03.20.37.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Nov 2019 20:37:01 -0800 (PST) Message-ID: <2e94543c5f92f6902ae51a5e6f4f569558fb548a.camel@cybertec.at> Subject: Re: Slow planning, fast execution for particular 3-table query From: Laurenz Albe To: David Wheeler , "pgsql-performance@lists.postgresql.org" Cc: Cameron Redpath Date: Mon, 04 Nov 2019 05:37:00 +0100 In-Reply-To: <175BE386-F861-4064-8B5D-93E63BD90F13@dgitsystems.com> References: <175BE386-F861-4064-8B5D-93E63BD90F13@dgitsystems.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.4 (3.32.4-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, 2019-11-04 at 03:04 +0000, David Wheeler wrote: > We’re having trouble working out why the planning time for this particular query is slow > (~2.5s vs 0.9ms execution time). As you can see below, there are only 3 tables involved > so it’s hard to imagine what decisions the planner has to make that take so long. After > 5 runs the prepared-statement code kicks in and it becomes quick, but it’s quite > infuriating for the first 5 runs given the execution is so much faster. > > Are you able to give any tips what might be taking so long (and how we might improve it)? > [...] > Planning time: 2429.682 ms > > Execution time: 0.914 ms Strange. Are any of your catalog tables unusually large? SELECT pg_relation_size(t.oid), t.relname FROM pg_class AS t JOIN pg_namespace AS n ON t.relnamespace = n.oid WHERE t.relkind = 'r' ORDER BY pg_relation_size(t.oid) DESC LIMIT 10; Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com