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 1u0EtU-00ExX4-QS for pgsql-performance@arkaria.postgresql.org; Thu, 03 Apr 2025 07:21:44 +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 1u0EtT-004BNJ-Fg for pgsql-performance@arkaria.postgresql.org; Thu, 03 Apr 2025 07:21:43 +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 1u0Esj-0047f4-3J for pgsql-performance@lists.postgresql.org; Thu, 03 Apr 2025 07:20:57 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u0Esh-002gzf-0K for pgsql-performance@lists.postgresql.org; Thu, 03 Apr 2025 07:20:56 +0000 Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-5e5dce099f4so828185a12.1 for ; Thu, 03 Apr 2025 00:20:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743664854; x=1744269654; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=r6vhhwM8RSvoG7ShY0MEYKG6Xlxx5bQw81r22zoxW/c=; b=aonXlHrHNHvE0tUmkNXLvXEl59KQwbIIkhqqfSRfHdNIl2ViWNDA5vsCjUulH5UInF lwGHLFAE+XCn4HEQAL+WosYz9xf0iuJhXwvTu+9zGcBc75HPiEe1NdPl5H7bOuwSwHN4 BjSgCM4xWfUCo/6AbKWH8SMmNwMNLydxOmQKq+DsfkgZIbk+uB33PsCPagCFw2Y5iCAY mcOXAHelWUJoLNXCd/Pa/gNH5qiIkJ5BtStCtW1MQaAi/FbmPUCO2imlFrTN0fHggIGi YGC5kmdk3oMNr/JrCPO84ZIN7+vXZlpQPMFaBsuYU8h7fmnv/E7gnorj9pW1smMvaNaP CScg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743664854; x=1744269654; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=r6vhhwM8RSvoG7ShY0MEYKG6Xlxx5bQw81r22zoxW/c=; b=jg1VbpD83QFB3d7C/dAbzV6i9D4ilrIEu+Wx1LhyH8qRfMvIgG8m/yN0JgykFylBrq ktMVGhNKywqzcUtQcSrtClS0rYRGuFThX7NDH4L+F/s+mWY0rJadsQm8aWhJBfLgWfM1 T9W0HprUV/9VMiV7kC1HkgC6MpG6lAvxphcwIkW8G/XW774CucT6wW/+oj16mbiREaCJ KHw1ZnK4tsXDA0mvVsci5+iRNGQfuyXkfkn8st8WZx9TqKOWpvZ11LkDGZYMtsEb3ZmO 8+oNKpeMp39v9rQuBdYVnjkqd4LSHoSaI4t9EFi1/TicoOKCUbFs2jNonpbKIenqia0E cGjg== X-Gm-Message-State: AOJu0Yx2CNMPG6PT4H2uIsvd4gTOJUB3tLVGka9VUHBNLRD4KuJc46FW foxiNLmFoxkrBSFZ9GLFYUUe+m5hdYXKRD9E4y+i2KcHOuzX0jHA1OTvwKbs2E+hBySBQLkrSao pN6WP9EEgZm2G76aEftCeN/IQ4Qg8Hw== X-Gm-Gg: ASbGncs7xva4LMP5R9hYVCme+jfTUkr5R69vCc3/gh7eF737/ZrlOBalCjpRYvOO3cU 3PMTBEKRfcxfUycPvPQekpILzNQ0lh+VZAZ1V9OcsIKKWSNzWvZp5IXsuyWvscyDYuouBTDPflC FNxhItioh5GnqNlsSI0qrjRYV5YYA= X-Google-Smtp-Source: AGHT+IGjaFsl+h5ZDVIrxRWR4hhCOtTpS92NOQuwn714BN3sWByEg6bAD6zhyO+KuWlihLcJoZlUPj9MfAe2W8BbDh8= X-Received: by 2002:a05:6402:1e8e:b0:5e1:dac1:fa22 with SMTP id 4fb4d7f45d1cf-5edfdafe39dmr15661669a12.21.1743664853525; Thu, 03 Apr 2025 00:20:53 -0700 (PDT) MIME-Version: 1.0 References: <348393.1743517738@sss.pgh.pa.us> In-Reply-To: From: Chris Joysn Date: Thu, 3 Apr 2025 09:20:40 +0200 X-Gm-Features: AQ5f1JpIhEjMSi9yQLSi02_4SDnbOAqyI-Si-xfs4lS5OKQxgCW2M1cn_Rrpb_s Message-ID: Subject: Fwd: Very slow query performance when using CTE To: pgsql-performance@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000033ac950631da9cd1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000033ac950631da9cd1 Content-Type: text/plain; charset="UTF-8" unfortunately that increased the query execution time by a factor of 8: https://explain.dalibo.com/plan/a6d2443d87fea1ee On Tue, 1 Apr 2025 at 16:28, Tom Lane wrote: > Chris Joysn writes: > > I have an issue when using CTEs. A query, which consists of multiple > CTEs, > > runs usually rather fast (~5s on my environment). But it turned out that > > using one CTE can lead to execution times of up to one minute. > > That CTE is used two times within the query. > > Try labeling that CTE as NOT MATERIALIZED. > > regards, tom lane > --00000000000033ac950631da9cd1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
unfortunately that increased the query execution time by a fa= ctor of 8:


Chris Joysn &= lt;joysn71@gmail.com= > writes:
> I have an issue when using CTEs. A query, which consists of multiple C= TEs,
> runs usually rather fast (~5s on my environment). But it turned out th= at
> using one CTE can lead to execution times of up to one minute.
> That CTE is used two times within the query.

Try labeling that CTE as NOT MATERIALIZED.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--00000000000033ac950631da9cd1--