Query plan regression between CTE and views

From: David Gilman <davidgilman1(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Query plan regression between CTE and views
Date: 2023-08-14 14:54:46
Message-ID: CALBH9DDEFesJy-hS33dvS88krG0+XZDiPr1iG2_YQNyJZRwa-w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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<

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sai Teja 2023-08-14 15:29:29 Re: Fatal Error : Invalid Memory alloc request size 1236252631
Previous Message Stephen Frost 2023-08-14 12:58:59 Re: Best strategy to perform individual incremental backups