Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kif8V-0002zv-1B for pgsql-sql@arkaria.postgresql.org; Fri, 27 Nov 2020 14:54:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kif8T-0000K5-Uo for pgsql-sql@arkaria.postgresql.org; Fri, 27 Nov 2020 14:54:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kif8T-0000Jy-4A for pgsql-sql@lists.postgresql.org; Fri, 27 Nov 2020 14:54:09 +0000 Received: from [64.147.123.20] (helo=wout4-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kif8M-0003Kh-HF for pgsql-sql@lists.postgresql.org; Fri, 27 Nov 2020 14:54:08 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 08109D92; Fri, 27 Nov 2020 09:53:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Fri, 27 Nov 2020 09:53:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=3oOthj8bgOHkOwDIhWwNfxPxKZOvUAD1bHVr32DpsQQ=; b=UwIvAO6O oQ0mOBlILw4wwyROdzfgmr2byp4MGti4hGIkAMS0AU88Uq2qzifVYkQy4WVi/mYq BFjXW31rB4PaebdeJXOSNPRBlALtfIhC8q7WjtCMjhWnGlStOqrrv4WuHNNcCUIj 3Ps75ATiK6pCcsVuy/T0e+8GA4mEjHffSjRPdQJNENNDFmW5yZokbV8RtZf79oWS IMbqDWEPhNdEz6q75DfSeWuLs+dJ38EmWHzGboG7Jc1K0PDvjgxchHIGr97igDG5 ZvhQ1XBe4NxGAbmU9XJeS8CZIFS9IiwGeaVE/cYAJbY+v9drwbv94LdTgc5hZvXN Z5LNW4vHJMXoGg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudehgedgjedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggugfgjfgesthekredttderudenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepleettefgteeifeejheefhedvkeejueelkeetvdejledu uefhtdeuffejuedvuedvnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucfkphepudeltddrleehrddukedrjeelnecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdroh hrgh X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.18.79]) by mail.messagingengine.com (Postfix) with ESMTPA id BE5A23280060; Fri, 27 Nov 2020 09:53:49 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 27FDE2A0D0C; Fri, 27 Nov 2020 11:53:47 -0300 (-03) Date: Fri, 27 Nov 2020 11:53:47 -0300 From: Alvaro Herrera To: "Voillequin, Jean-Marc" Cc: "pgsql-sql@lists.postgresql.org" Subject: Re: CTE materialized/not materialized Message-ID: <20201127145347.GA17941@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.20 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-Nov-26, Voillequin, Jean-Marc wrote: > The problem I have now, is that I have to decide the plan that PG must > follow. It's a kind of Oracle hint. > > Is it possible, for PG, to decide, by its own, the best way to go > (materialized or inlined) for each CTE encountered in the query, > rather than deciding to materialized as soon as the CTE is found more > than once in the outer query? Am I clear? Actually, the planner does make a choice. Only if it's wrong then it is necessary to mark the CTE for materialization. Previously, it always materialized, causing many queries to have bad performance with no recourse. One could argue that we could do better at making the choice, but that seems an unimplemented feature, not a bug. Quoting src/backend/optimizer/plan/subselect.c: * We cannot inline if any of these conditions hold: * * 1. The user said not to (the CTEMaterializeAlways option). * * 2. The CTE is recursive. * * 3. The CTE has side-effects; this includes either not being a plain * SELECT, or containing volatile functions. Inlining might change * the side-effects, which would be bad. * * 4. The CTE is multiply-referenced and contains a self-reference to * a recursive CTE outside itself. Inlining would result in multiple * recursive self-references, which we don't support. * * Otherwise, we have an option whether to inline or not. That should * always be a win if there's just a single reference, but if the CTE * is multiply-referenced then it's unclear: inlining adds duplicate * computations, but the ability to absorb restrictions from the outer * query level could outweigh that. We do not have nearly enough * information at this point to tell whether that's true, so we let * the user express a preference. Our default behavior is to inline * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever * will be inlined even if multiply referenced. -- Álvaro Herrera https://www.EnterpriseDB.com