pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Query plan regression between CTE and views
3+ messages / 2 participants
[nested] [flat]

* Query plan regression between CTE and views
@ 2023-08-14 14:54  David Gilman <davidgilman1@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: David Gilman @ 2023-08-14 14:54 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I have a query that was originally written as a handful of CTEs out of
convenience. It is producing a reasonable query plan because the CTE
materialization was kicking in at an appropriate place. The CTEs
aren't totally linear. The graph looks like this, where A, B, C and D
are CTEs, and B -> A means B selects from A. In Graphviz format:

G {
   B -> A;
   C -> A;
   C -> B;
   D -> C;
}

Out of curiosity I tried turning the query into a series of views and
ran that query. The query plan is vastly different, there is no
materialization and it runs much slower.

My question is: is this a valid bug? I am not sure if I should expect
the view version to find a way to materialize and produce a comparable
query plan. Also, making a minimal test case is going to take a bit
and I don't want to start unless this smells like a genuine bug.

-- 
David Gilman
:DG<





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Query plan regression between CTE and views
@ 2023-08-14 15:44  Ron <ronljohnsonjr@gmail.com>
  parent: David Gilman <davidgilman1@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Ron @ 2023-08-14 15:44 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 8/14/23 09:54, David Gilman wrote:
> I have a query that was originally written as a handful of CTEs out of
> convenience. It is producing a reasonable query plan because the CTE
> materialization was kicking in at an appropriate place. The CTEs
> aren't totally linear. The graph looks like this, where A, B, C and D
> are CTEs, and B -> A means B selects from A. In Graphviz format:
>
> G {
>     B -> A;
>     C -> A;
>     C -> B;
>     D -> C;
> }
>
> Out of curiosity I tried turning the query into a series of views and
> ran that query. The query plan is vastly different, there is no
> materialization and it runs much slower.
>
> My question is: is this a valid bug? I am not sure if I should expect
> the view version to find a way to materialize and produce a comparable
> query plan. Also, making a minimal test case is going to take a bit
> and I don't want to start unless this smells like a genuine bug.


What version of Postgresql?

(Also, back before, I think, v12, CTEs were optimizer fences.  You were 
better using views or sub-queries.)

-- 
Born in Arizona, moved to Babylonia.





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Query plan regression between CTE and views
@ 2023-08-15 12:59  David Gilman <davidgilman1@gmail.com>
  parent: Ron <ronljohnsonjr@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: David Gilman @ 2023-08-15 12:59 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

I'm on PostgreSQL 15 with essentially a stock configuration.

On Tue, Aug 15, 2023 at 8:58 AM Ron <ronljohnsonjr@gmail.com> wrote:
>
> On 8/14/23 09:54, David Gilman wrote:
> > I have a query that was originally written as a handful of CTEs out of
> > convenience. It is producing a reasonable query plan because the CTE
> > materialization was kicking in at an appropriate place. The CTEs
> > aren't totally linear. The graph looks like this, where A, B, C and D
> > are CTEs, and B -> A means B selects from A. In Graphviz format:
> >
> > G {
> >     B -> A;
> >     C -> A;
> >     C -> B;
> >     D -> C;
> > }
> >
> > Out of curiosity I tried turning the query into a series of views and
> > ran that query. The query plan is vastly different, there is no
> > materialization and it runs much slower.
> >
> > My question is: is this a valid bug? I am not sure if I should expect
> > the view version to find a way to materialize and produce a comparable
> > query plan. Also, making a minimal test case is going to take a bit
> > and I don't want to start unless this smells like a genuine bug.
>
>
> What version of Postgresql?
>
> (Also, back before, I think, v12, CTEs were optimizer fences.  You were
> better using views or sub-queries.)
>
> --
> Born in Arizona, moved to Babylonia.
>
>
>
>


-- 
David Gilman
:DG<





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2023-08-15 12:59 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-08-14 14:54 Query plan regression between CTE and views David Gilman <davidgilman1@gmail.com>
2023-08-14 15:44 ` Ron <ronljohnsonjr@gmail.com>
2023-08-15 12:59   ` David Gilman <davidgilman1@gmail.com>

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