second CTE kills perf

From: Nicolas Seinlet <nicolas(at)seinlet(dot)com>
To: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: second CTE kills perf
Date: 2021-06-22 08:20:08
Message-ID: KJasd1rgyZASsWz7cdAdWDy9iCxVuSW4WdIatFAwGJjYg-9nYe7pVdNkuOXYcrSUz6u2kf91e-5rSTwmKy32nYDFBitSPKR4K5KFKMrgv7s=@seinlet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

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;

The real examples, with query plans:
https://explain.dalibo.com/plan/98A
https://explain.dalibo.com/plan/o6X4

Thanks for your time,

Nicolas Seinlet.

Attachment Content-Type Size
publickey - nicolas@seinlet.com - 0xCAEB7FAF.asc application/pgp-keys 729 bytes

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Vijaykumar Jain 2021-06-22 09:04:15 Re: Postgres PANIC when it could not open file in pg_logical/snapshots directory
Previous Message Mike Yeap 2021-06-22 08:02:01 Postgres PANIC when it could not open file in pg_logical/snapshots directory