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 1tFmni-00Fwx9-CO for pgsql-performance@arkaria.postgresql.org; Tue, 26 Nov 2024 04:03:46 +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 1tFmnf-003Q3A-46 for pgsql-performance@arkaria.postgresql.org; Tue, 26 Nov 2024 04:03: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 1tFmne-003Q30-MR for pgsql-performance@lists.postgresql.org; Tue, 26 Nov 2024 04:03:42 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tFmnb-003nrh-RB for pgsql-performance@lists.postgresql.org; Tue, 26 Nov 2024 04:03:41 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-53de8ecb39bso497586e87.2 for ; Mon, 25 Nov 2024 20:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732593818; x=1733198618; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=oyMu+FmInnqi+qCLp5FWThh/06dx9qEyI8GteASj/P8=; b=WAgIJtlzPATrL38H61GYI++xYvAw/f+yYa8/uihjqug644KMIuTnryxSev0VTJ7TnR F8tRqUVdKckJsgW7x4iocciQDipP0jc1Lql+20RQkgqx+9A0EbQLUBzftGxG+99aPjbX bFBtOJftd7n3GjeF1SLdbL+4CTxn7ofiYx7oWcniigpWKQhdpDZBtlUClfdUumP+4nAj hnVfJ7En3AjXvbyKXBpMPNBc1izjlAz1GMgMVoxlG/S4YllfwaEJD0Q0Mo14tGYWixJ4 FONYVkuEdjezE3aUq9hjKsckb8v37sWIZPsWdiGgvQYWxIO+KsfVvB/E5LI1zy/IDFT0 dfqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732593818; x=1733198618; h=content-transfer-encoding:cc: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=oyMu+FmInnqi+qCLp5FWThh/06dx9qEyI8GteASj/P8=; b=g4o8UN5n7+m8eQwzZdS3aAEiBK8K6QyhfD9C7Sx7oqcDHWMorfXYn56BukaQrxKpqc wEQJPJ528gb6o5OvbEn4qvy9dZlRlzp0bN6WLmBRBEcainAaqBo7PGQHCj8tbbSOeeaJ kBOLBphM6c7xjIH1gsal+oZlDH61OVfEORH/EvYcdV8YdYt3NTXRhUDG/5wGNNSSyj+7 UDuX4pH77TuZU0OxxeSPj7+t510eIjjewAWV0wdgs+kW7uFAwrDBoKA7yvRl0G0vH+fw o1PEVt8BTZTC2i8ZnE8K1vblEec6hLIdoyEyaQzXLvoVA1lR5JwojNmXf8oFvYCf+RAz zmTQ== X-Forwarded-Encrypted: i=1; AJvYcCUmFjNsG8JyjglKSRsXK+DqKQ9Fh9y8kKOZvIKp6YFu5aQuDQtgM+FZdswic7KrkgS+xW8GJ+iwWBVVNuSxmdtOLg==@lists.postgresql.org X-Gm-Message-State: AOJu0YzvgjuBKICGV+ul5IlNEjvFuvrPH42YeBYhUCD1geCy8pzxUQRX bZkorjAzQULCaTxjXFFyPr/sy4p0cDdnlgB6IQOgfHw05F9YY/EbPuSRM7kgb+kx4loIK2lt3Nj +vG1ZnJ3T2LN4pcqNQ7rqlRq19qM= X-Gm-Gg: ASbGncuZ2c+Oc7D3rawwkHX5XN2WBSq57O+WrQlwB6GBI5yxp75zUu3/7H9WX/9Rl5R BEkdwrtPxYAcXt5Q4WjpwnGFD/pEL7Ey5Ztx/DWmOPuoL2hg0tkBARqdQEFwxnE2b X-Google-Smtp-Source: AGHT+IGkO9dko0tZOJqxArH83yMeKeAyiQR/a9+ZD2IK2BVzYqSYhmJZlcAbeJ4KBSgOfmx++zSBdtZ3ljDsA3sYMgM= X-Received: by 2002:a05:6512:3502:b0:53d:dbe8:e393 with SMTP id 2adb3069b0e04-53ddbe8e406mr3498265e87.1.1732593817554; Mon, 25 Nov 2024 20:03:37 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 26 Nov 2024 17:03:24 +1300 Message-ID: Subject: Re: CTE Inline On TPC-DS Query 95 To: Ba Jinsheng Cc: Andrei Lepikhov , "pgsql-performance@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 26 Nov 2024 at 10:03, Ba Jinsheng wrote: > If we specify ``NOT MATERIALIZED`` for the query, the execution time is r= educed from 1min to 1s due to the usage of inline CTE. It seems expected as= described in the PostgreSQL documentation. However, from the code: https:/= /github.com/postgres/postgres/blob/REL_17_STABLE/src/backend/optimizer/plan= /subselect.c#L939, I understand that this query does not contain volatile f= unctions and includes simple predicates. I am wondering whether we can rela= x this condition checking, to enable inline CTE for such queries? Because i= t seems the performance benefit is huge on a standard benchmark. The relevant part of the documentation [1] seems to be "By default, this happens if the parent query references the WITH query just once, but not if it references the WITH query more than once." If you're proposing that we change the heuristics for when to inline CTEs, then I suggest you go and check the archives for why we made this decision. You'll need to prove to us why your newly proposed heuristic is better than what we have. That's going to take more than one example query to show that your heuristics are better. However, just to save you from wasting any time, I highly doubt an adjustment to the CTE inlining heuristics alone is enough. Just imagine the CTE evaluation performed a Seq Scan on a 1 billion row table and found 1 matching row. Let's assume the remaining part of the query is very cheap. It's obviously going to be better to materialise the CTE so that we perform the Seq Scan once rather than twice (or however many times the CTE is referenced). Adjusting the heuristics here is just going to make some queries faster at the expense of making other queries slower. That's never going to pass community standards. If you truly wanted to improve things here, the heuristics would need to be swapped out with a cost-based decision. I can tell you now, that doing that is a very difficult task as it basically requires performing the join search and quite a large portion of planning once for each combination of CTE inlined vs not-inlined. If you had a query with a dozen or so CTEs, that's going to be a very costly thing to plan. David [1] https://www.postgresql.org/docs/current/queries-with.html