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 1iYEj0-00088X-5g for pgsql-performance@arkaria.postgresql.org; Fri, 22 Nov 2019 19:36:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iYEiy-0003JH-H7 for pgsql-performance@arkaria.postgresql.org; Fri, 22 Nov 2019 19:36:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iYEiy-0003IK-1R for pgsql-performance@lists.postgresql.org; Fri, 22 Nov 2019 19:36:12 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iYEis-00015u-Re for pgsql-performance@lists.postgresql.org; Fri, 22 Nov 2019 19:36:10 +0000 Received: by mail-wm1-x344.google.com with SMTP id n188so7001472wme.1 for ; Fri, 22 Nov 2019 11:36:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=28pR/j45kFGdjqKiSzQ7R/eb13U1jJraSHkuyHyCL3A=; b=F3VuCvAnKfoHOlc8gw9mK+OAsw97s/wjmpiUa0i3LQ1KTWuZo0QRjrflC9mUfLQxNl uAiOKXouXlCrBaPG0wcq++0Rd0dsqFlzYDhvlkdtEFP7iiXCG35utHLtvV5KKhSC7+ig AgyVNjUmpcZCkK2vLMwwT69I2hFe36rLelZl9xTQ1N7saHVSgpALWU/PAYIx28JbkGE/ EPEEDaieW4HfrfMLZPnf1KaPKPc1F8YSLrDRIpRqq79Wxoh5aY/TPBKyCqADK5/SaM2T nrNNXPiXvTfriqhpNqsmjGz9CPUTjMcsQMHD8qKCFZXPY9ilkKdiNE5DTYEjXWXHx03i GT0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=28pR/j45kFGdjqKiSzQ7R/eb13U1jJraSHkuyHyCL3A=; b=QILHBQbCsW3f8xqLQevRYNSV3MqjqYmKAMqtz1K0WzCcVsCeaPYdRQgVl1XUIccYYK VoRAoEpAD7Yl/dXRafLGgR/oAsISWJLeFYMlINNt36wEc5jL58AgMaHmgbo9ZIwoNDlD BZhHWj5v72DpSXeETNZRiak+nyhoV6W1ma1xVx8+anNUt6DEXDF2jMP+zVj9pKPxeg7u 4U0FS5ZIR4vSAeGYkd76x2dUPKvdZLYy+2WrZm/JDckjGysJ4WgWkOmow2BdYtcqxrA3 iPKA4TyBYq8JQ6kwg9h0dKu5+AJgloHEf7tbBzwBft2TOnS/KXJ/+HWqWhB0cVInCkvj Dovg== X-Gm-Message-State: APjAAAWtO0gbCwtrbT8uDDr6j4AouX6UZx1LbA6n47fEvPsd9v32l5yP Q5FLTngoDYP4FModLHuyypVGD35hAGC4Pg== X-Google-Smtp-Source: APXvYqzXIFMcruz05dZsGE+lV0nwlkLu+0U2fEWbUtACrpgnLnMyN2kWxXEXyKqoOUFTZCmtYPyNxw== X-Received: by 2002:a1c:c28a:: with SMTP id s132mr17864093wmf.162.1574451365160; Fri, 22 Nov 2019 11:36:05 -0800 (PST) Received: from localhost ([217.8.174.100]) by smtp.gmail.com with ESMTPSA id x8sm8737465wrm.7.2019.11.22.11.36.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Nov 2019 11:36:04 -0800 (PST) Date: Fri, 22 Nov 2019 20:36:02 +0100 From: Tomas Vondra To: Sterpu Victor Cc: =?utf-8?B?RsSxcmF0IEfDvGxlw6c=?= , pgsql-performance@lists.postgresql.org Subject: Re: Postgresql planning time too high Message-ID: <20191122193602.ljivcdonmmww3nm3@development> References: <7ca4228bd9ea8ee1b119123679f340ff@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Nov 22, 2019 at 11:44:51AM +0000, Sterpu Victor wrote: >No rows should be returned, DB is empty. >I'm testing now on a empty DB trying to find out how to improve this. > I'm a bit puzzled why you're doinf tests on an empty database, when in production it'll certainly contain data. I guess you're assuming that this way you isolate planning time, which should remain about the same even with data loaded, but I'm not entirely sure that's true - all this planning is done with no statistics (histograms, MCV lists, ...) and maybe it's forcing the planner to do more work? I wouldn't be surprised if having those stats would allow the planner to take some shortcuts, cutting the plannnig time down. Not to mention that we don't know if the plan is actually any good, for all what we know it might take 10 years on real data, making the planning duration irrelevant. Let's put that aside, though. Let's assume it's because of expensive join order planning. I don't think you have a lot of options, here, unfortunately. One option is to try reducing the planner options that determine how much effort should be spent on join planning, e.g. join_collapse_limit and geqo_threshold. If this is the root cause, you might even rewrite the query to use optimal join order and set join_collapse_limit=1. You'll have to play with it. The other option is using CTEs with materialization, with the same effect, i.e. prevention of optimization across CTEs, reducing the total effort. >In this query I have 3 joins like this: > >SELECT t1.id, t2.valid_from >FROM t1 >JOIN t2 ON (t1.id_t1 = t1.id) >LEFT JOIN t3 ON (t3.id_t1 = t1.id AND t3.valid_fromWHERE t3.id IS NULL > >If I delete these 3 joins than the planning time goes down from 5.482 >ms to 754.708 ms but I'm not sure why this context is so demanding on >the planner. >I'm tryng now to make a materialized view that will allow me to stop >using the syntax above. > >I reattached the same files, they should be fine like this. > It'd be useful to have something others can use to reproduce the issue, and investigate locally. SQL script that creates the whole schema and runs the query, for example. What I'd like to see is a perf profile from the planning, so that we can see where exactly is the bottleneck. Maybe there actually is a bug that makes it muych more expensive than it should be, in some corner case? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services