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 1kih9O-0007xr-CW for pgsql-sql@arkaria.postgresql.org; Fri, 27 Nov 2020 17:03:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kih9N-0003nO-6j for pgsql-sql@arkaria.postgresql.org; Fri, 27 Nov 2020 17:03:13 +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 1kih9M-0003mr-W4 for pgsql-sql@lists.postgresql.org; Fri, 27 Nov 2020 17:03:12 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kih9K-0004K0-Qh for pgsql-sql@lists.postgresql.org; Fri, 27 Nov 2020 17:03:12 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.west.internal (Postfix) with ESMTP id 5F73DA93; Fri, 27 Nov 2020 12:03:09 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 27 Nov 2020 12:03:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=NA7WWmocvqKGYONf0 Zo/hyb552+0N5ZOfucsW1oIjwk=; b=Pm/0SnO/k6ffSqNNspdYWQ2D5HGIW3faC F1AlrkQcVlpRUcfxWxgaroT9/Z7/NgOQNtqQ6MnOgTGbauC4E9i99zyy9JGsBfuk 6+7fSBVTMHo8RMEgjN5UuWTWZvn0rWeErmJPXu3LEqmTUpUE4MNreWMirvkgCxP7 hDtxCZhbCCRGiufz9HMIeQRlr/WXX5AFG3CsUkZZRe5VzRBHeIlD+caSwWYiOpl2 7oTb3PZM4KZalGYpeBCADhWOYFu++INq5oE1iMIypVuvhScbt/G73cqShTqA8/iq FNgimaI1IWNzwYI4wg4dd/7juLAtZY1t+lFRRT7t5IqOKZHaFCZzg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudehgedgleejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfggtggujggfsehttdertddtredvnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpeevgfelveegueejgefhgeetgefhgeehieefheehleetteei udeiiefhgffgheffteenucfkphepudeltddrleehrddukedrjeelnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhv hhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Received: from perhan.alvh.no-ip.org (unknown [190.95.18.79]) by mail.messagingengine.com (Postfix) with ESMTPA id 57B463064AA7; Fri, 27 Nov 2020 12:03:07 -0500 (EST) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 06AA12A0D0C; Fri, 27 Nov 2020 14:03:03 -0300 (-03) Date: Fri, 27 Nov 2020 14:03:03 -0300 From: Alvaro Herrera To: "Voillequin, Jean-Marc" Cc: Tom Lane , "pgsql-sql@lists.postgresql.org" Subject: Re: CTE materialized/not materialized Message-ID: <20201127170303.GA14088@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-Nov-27, Voillequin, Jean-Marc wrote: > Thank you Tom & Alvaro. > I'm still dreaming of such query planner! An idea mentioned several times is that of making more expensive optimizer passes for certain plans -- either at the user's request or merely based on the initial estimated cost being very large. A query that's seems more expensive to execute would also seem to warrant a greater optimization effort. (But if it's automated and we get it wrong, then we'd get complaints that we spend pointless time in the optimizer.) Your sample plan has 3 CTEs, so we would have to plan eight queries for the brute-force way -- one per combination of each CTE inlined or materialized. Maybe it's worth the optimizer cost, maybe not. Now maybe there's a smarter approach than brute-forcing it.