pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Nicolas Seinlet <nicolas@seinlet.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: second CTE kills perf
Date: Tue, 22 Jun 2021 09:53:15 -0400
Message-ID: <1714207.1624369995@sss.pgh.pa.us> (raw)
In-Reply-To: <KJasd1rgyZASsWz7cdAdWDy9iCxVuSW4WdIatFAwGJjYg-9nYe7pVdNkuOXYcrSUz6u2kf91e-5rSTwmKy32nYDFBitSPKR4K5KFKMrgv7s=@seinlet.com>
References: <KJasd1rgyZASsWz7cdAdWDy9iCxVuSW4WdIatFAwGJjYg-9nYe7pVdNkuOXYcrSUz6u2kf91e-5rSTwmKy32nYDFBitSPKR4K5KFKMrgv7s=@seinlet.com>

Nicolas Seinlet <nicolas@seinlet.com> 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 seconds. If I add a second CTE with the same query as the previous one and select * 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=32;

> 10 minutes version:
> | WITH cte1 AS (SELECT x,y,z FROM table), cte2 AS (SELECT row_number() over(),x,y,z FROM cte1) SELECT * FROM cte2 WHERE x=32;

[ shrug... ]  You are asking for two different computations, and the
second one is far more expensive.

In the first case, the WHERE x=32 clause is applied before the window
function, so we can (indeed must) filter out all rows not having x=32
before doing the window function.

In the second case, WHERE x=32 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





view thread (4+ messages)  latest in thread

Message-ID: <1714207.1624369995@sss.pgh.pa.us>
Permalink:  ../1714207.1624369995@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1714207.1624369995@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, nicolas@seinlet.com
  Subject: Re: second CTE kills perf
  In-Reply-To: <1714207.1624369995@sss.pgh.pa.us>
  References: <KJasd1rgyZASsWz7cdAdWDy9iCxVuSW4WdIatFAwGJjYg-9nYe7pVdNkuOXYcrSUz6u2kf91e-5rSTwmKy32nYDFBitSPKR4K5KFKMrgv7s=@seinlet.com> <1714207.1624369995@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox