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 1tzhpr-005UMu-5Y for pgsql-performance@arkaria.postgresql.org; Tue, 01 Apr 2025 20:03:47 +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 1tzhpp-005wNf-1U for pgsql-performance@arkaria.postgresql.org; Tue, 01 Apr 2025 20:03:45 +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 1tzhpo-005wNX-Ka for pgsql-performance@lists.postgresql.org; Tue, 01 Apr 2025 20:03:44 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tzhpl-002Pev-31 for pgsql-performance@lists.postgresql.org; Tue, 01 Apr 2025 20:03:43 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-43cf0d787eeso56578295e9.3 for ; Tue, 01 Apr 2025 13:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743537820; x=1744142620; darn=lists.postgresql.org; h=mime-version:message-id:date:user-agent:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=Wvt0MLuStcTHGz30aFS0r/Dj3olYrtR5O8ghlnOIGYs=; b=RNBwVk+hGWGrM9OJ+X7G/SoV/LhaSOIz2eWFksJI+HuwFH5YYl02bmto6n1VVGrIoS B4R6rEOhHk/HuFvDqRi2pZwL3ic5Uz6YQuypQh8soLX372tkyD16x+cBMjrwcWSnjim7 YARkJrPbYtbh+EuM6zrmVqZBG+jjUF9c3p1UTS0cWh6LL+gqMsH+ch4lukaq61S9sfT2 Hollx7BNnacOnotYyNq65r2dHjRlknVHfTLVydYFxDn713VLQ6DV0s6JBDG6fir+40Xg kJvmd9yhT3QTH1IQNVXagxpC0QlvE/cgbxiNw2R57WEfaor3gTjF217J1jPoCOJ2lGm0 FwHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743537820; x=1744142620; h=mime-version:message-id:date:user-agent:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Wvt0MLuStcTHGz30aFS0r/Dj3olYrtR5O8ghlnOIGYs=; b=v/gvPrXje73JuGgK85Lr6R71QGuBK4pWcIZYmisqwhaKxVCHQBoAs3+/MSpsq7VKyW Dr6gG1JMroksl9WKxyFWTUZm+B60tKHHX370/Nl4GApQOU0NEIN+aJ6j5ZsWx+BrbZOZ oPu8/5EJaQXkZ3SvGdvpdyJRc1zaGXLp8vunGg+KDQH0Sg2cHHWRf/sxVwQpQpMNSxIi n3e3RcNTWucvvkjyNrjedpnUhp6eBnougYnfs97ANXAefEUIr6I59Rjf7rrS1EbR/z9W 6POfUWy1lzL1AlL8MBn6m5FmKRwI3XphB1UaW92rys3CyX4l1+Xi3lTu3XhKDScwLdhZ 99rA== X-Gm-Message-State: AOJu0YzoAm73YiRc0/ivn8RBmghfUcf56ns3ugjVRmfSaqi3xi24j3i8 53jzpRuxU3GUdS2XOjgo+JjIrchsDBN4nLv6Ae+MzQ7eUoNt7XsTC2SSrUbl X-Gm-Gg: ASbGncv/UxNSbxSa00QhxN8pv99bAbVZxffLu2q2XmTYwZL1kDGmRv0EDG5+XWwkvbv VDTzLY40kLUItXoEUVpwBcHbDPwwK9ttTuRQXNhiz4MSKysKh7QQyUWfbhpFGTOD36XtAIpZTZ5 Cr/1l2ZyZqVqKnF9xglvaSBaqTYHM4KD29xOutldNJ3uKbxGdtM8syekVj28GWxs/2/H3pF2hoI Prnu8v0MH4b+RKCnmbe3lnNUkzoBOXoKwzAc09eMIfP0Zyb1ojpv1qlo5msBHbvIGoIwAAVe8co ck+jJ9q5xr86rVefVOvSbQ/c5MsefLZWnzZB6FVmtOWgEHiiLgqMerQZYg== X-Google-Smtp-Source: AGHT+IFw6JqqE/DwzofsgzdL9aaFAyWAD5w3etfGlkZnidLauF/Lfg4bm/ansvc/G41Qk0oicySx3A== X-Received: by 2002:a05:600c:468c:b0:43d:83a:417d with SMTP id 5b1f17b1804b1-43db622a42emr146711835e9.12.1743537819358; Tue, 01 Apr 2025 13:03:39 -0700 (PDT) Received: from small-boat ([193.207.154.72]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d82dede5csm213458445e9.8.2025.04.01.13.03.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Apr 2025 13:03:37 -0700 (PDT) From: Renan Alves Fonseca To: Chris Joysn Cc: pgsql-performance@lists.postgresql.org Subject: Re: Very slow query performance when using CTE In-Reply-To: (Chris Joysn's message of "Tue, 1 Apr 2025 15:37:51 +0200") References: User-Agent: mu4e 1.12.1; emacs 29.4 Date: Tue, 01 Apr 2025 22:03:29 +0200 Message-ID: <87msczsl7y.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Chris Joysn writes: > Hello, > 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. In the CTE there are 2600 rows, compared to results of the other CTEs its a > fraction of the data. > When replacing this CTE and use the original table instead in the jions, the query performs nicely. > However, it is not always like this. Running the same query on a almost same set of data, quantity wise, may give indeed > good performance when using that CTE. > This is the slow performing query using CTE: > https://explain.dalibo.com/plan/45ce86d9cfge14c7 > And this is the fast performing query without that CTE: > https://explain.dalibo.com/plan/4abgc4773gg349b4 > > The query runs on the very same environment and data. > What can be the issue here and how can I address it? Hi, the planner gets really bad estimates on the number of rows that the first two CTE return. It is the same situation in both queries. It is just an accident that one of them works fine. We need to understand why these estimates are wrong. The protocol to fix this kind of issues is to apply ANALYZE so that the statistics get update. You can try that, but my guess is that we are dealing with a generic plan here. So, try to replace the value of $simRunId by the actual value and see if the plan changes. Best regards, Renan