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 1lvgqA-00042g-65 for pgsql-general@arkaria.postgresql.org; Tue, 22 Jun 2021 13:53:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lvgq8-00036i-5r for pgsql-general@arkaria.postgresql.org; Tue, 22 Jun 2021 13:53:20 +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 1lvgq7-00036Y-Pv for pgsql-general@lists.postgresql.org; Tue, 22 Jun 2021 13:53:19 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lvgq5-0004Ry-Bt for pgsql-general@lists.postgresql.org; Tue, 22 Jun 2021 13:53:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 15MDrF091714208; Tue, 22 Jun 2021 09:53:16 -0400 From: Tom Lane To: Nicolas Seinlet cc: "pgsql-general@lists.postgresql.org" Subject: Re: second CTE kills perf In-reply-to: References: Comments: In-reply-to Nicolas Seinlet message dated "Tue, 22 Jun 2021 08:20:08 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1714206.1624369995.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 22 Jun 2021 09:53:15 -0400 Message-ID: <1714207.1624369995@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nicolas Seinlet writes: > I'm trying to understand this behaviour and the limits of CTE, when they= reach the limits, when they cannot receive parameters from a caller, ... = I'm running a query on PostgreSQL 10 with a cte. the query runs in ~ 10 se= conds. If I add a second CTE with the same query as the previous one and s= elect * from second_cte as query, it now runs in ~ 10 minutes. > oversimplified example: > 10 seconds version: > | WITH cte1 AS (SELECT x,y,z FROM table) SELECT row_number() over(),x,y,= z FROM cte1 WHERE x=3D32; > 10 minutes version: > | WITH cte1 AS (SELECT x,y,z FROM table), cte2 AS (SELECT row_number() o= ver(),x,y,z FROM cte1) SELECT * FROM cte2 WHERE x=3D32; [ shrug... ] You are asking for two different computations, and the second one is far more expensive. In the first case, the WHERE x=3D32 clause is applied before the window function, so we can (indeed must) filter out all rows not having x=3D32 before doing the window function. In the second case, WHERE x=3D32 is applied above/after the window function. We cannot push down the WHERE to before the window function. (In this case, filtering beforehand would obviously change the results of row_number, but in general we don't know enough about window function behavior to risk such changes.) So row_number has to be computed over the entire contents of the "table", and that's not cheap. It does surprise me a bit that row_number is quite *that* expensive, but if you are expecting equivalent results from these two queries, you're simply wrong. regards, tom lane