Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rMWSI-0051ag-JZ for pgsql-performance@arkaria.postgresql.org; Sun, 07 Jan 2024 16:56:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rMWRJ-00Gfwc-1t for pgsql-performance@arkaria.postgresql.org; Sun, 07 Jan 2024 16:55:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rMWRI-00GfwU-Nx for pgsql-performance@lists.postgresql.org; Sun, 07 Jan 2024 16:55:56 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rMWRF-000Lpy-Ma for pgsql-performance@postgresql.org; Sun, 07 Jan 2024 16:55:55 +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 407Gtp5q1606645; Sun, 7 Jan 2024 11:55:51 -0500 From: Tom Lane To: Clemens Eisserer cc: pgsql-performance@postgresql.org Subject: Re: Selection not "pushed down into" CTE In-reply-to: References: Comments: In-reply-to Clemens Eisserer message dated "Sun, 07 Jan 2024 08:37:17 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1606643.1704646551.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 07 Jan 2024 11:55:51 -0500 Message-ID: <1606644.1704646551@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Clemens Eisserer writes: > running postgresql 15.5 I was recently surpised postgresql didn't > perform an optimization which I thought would be easy to apply. It is not. > running the following query results in a full sort (caused by lead > over order by) as the ts > '2024-01-04' selection doesn't seem to be > applied to the CTE but only later: > with cte as (select ts, lead(ts, 1) over (order by ts) as ts2 from smart= meter) > select ts, ts2 from cte where ts > '2024-01-04' and extract(epoch > from ts2) - extract(epoch from ts) > 9; The ts restriction is not pushed down because of the rules in allpaths.c: * 4. If the subquery has any window functions, we must not push down qual= s * that reference any output columns that are not listed in all the subque= ry's * window PARTITION BY clauses. We can push down quals that use only * partitioning columns because they should succeed or fail identically fo= r * every row of any one window partition, and totally excluding some * partitions will not change a window function's results for remaining * partitions. (Again, this also requires nonvolatile quals, but * subquery_is_pushdown_safe handles that.) To conclude that it'd be safe with this particular window function requires deep knowledge of that function's semantics, which the planner has not got. regards, tom lane