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.96) (envelope-from ) id 1vxmUn-00H7yZ-1B for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Mar 2026 13:42:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vxmUl-00CtUb-2b for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Mar 2026 13:42:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vxmUl-00CtUT-1n for pgsql-bugs@lists.postgresql.org; Wed, 04 Mar 2026 13:42:36 +0000 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vxmUj-00000000XUh-3mFT for pgsql-bugs@lists.postgresql.org; Wed, 04 Mar 2026 13:42:35 +0000 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-65bfc858561so2962383a12.2 for ; Wed, 04 Mar 2026 05:42:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1772631753; cv=none; d=google.com; s=arc-20240605; b=G7BEZ6b/HQWklqHbXtwb8mbH3qc01DHhVBDhvKC9bK72FWSk3h0OurRECs745xUDaI ubsbpSB+bGSINwFlti6vJpIEz11F2HgVzwJkmoJb4sjiU1g3fxvx2Q/TsTlF5HQ07IZL XLGVbz2F83OCE64ABVVG8dnSyW+a5JSNmU4l0bKjjq1EMfpyYViSW0cn4XbF5veABNPc 9uWao2FZi3vrM+0jXm8XuUwfKwAAQYHqyF6o7pbmplrTA8s02aOZ+qQZ2ecOjfZq1TQZ zlfI9couRKAsBlQKlz+ZcYQqJJC0p/J/ho6YO6AUHJiWB9y+DI5aZ0bOrXMN+bv16VCh 6bsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=gy/UvjsbbQ2XkDYIYYIWRBPlRYWj2/swgAbK+IMPpWQ=; fh=HlF6x692wWPJtOwuRBKJ6Qr9GvK0F0eFoGSwtDD0+Ys=; b=BwZG8jLeMhszvmIZCdusynIDL0iS9cz26Ug/KvX1gm4EjF5Y4cU937MzEYaeJTi0Rg hRunEvW0iST5KFWZmCKJYHwdEmpm+LHUrKK0r2d4fLz96yeGVhdFj/JRxr7i3YURx0V4 jeRqsAVxJ/l+U54Lgb04yy3q3bH9j/vg6ityDmjuSOIj4jeJ6+pZv/YG/D86DXQcHFRh 54qRr439mx5HG3IM8gwxHu4f9w5vqiBf6XAY2Wy8i0KttF9h9y/NBjQzZXl7EtUJNLni gzFiMsnNtQA7s0qdhQqkn1mdVCr20LSp0gByf0dEnXI/PqDU8OwxO78la5roSCfgf1/m FyoA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772631753; x=1773236553; 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=gy/UvjsbbQ2XkDYIYYIWRBPlRYWj2/swgAbK+IMPpWQ=; b=TKysvvxWw3aCjb8MpQCK/lTdZLPxJP88ZFi+1wip8GyYxhT/KLCJY7ij0j6dCE4Bhh MHBB648dN/2aUQCcBhFWinufbiOP7gdBtmWXGUpBfVdJNvgPDtkHquULLyPm0Ecl4qpa 9rQglS9F0ooT+6eCZYXvxC4J+RhADKjaqJIbocPBzZykdFDubJpvVoAbJVyjh/uKU77x EDdBdUS9/LdkEJSjXTY6YOQlSwRUX5egxGjgj2YZUCaJFIbProQrv/7V9FYv2qnir1g8 dTsReGqUvKG4cCBOgIupri2ABWi561T54GMMKO8Z0Y5y1qZ2lvSj6TCvtqzmlQrEIpEV k6VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772631753; x=1773236553; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=gy/UvjsbbQ2XkDYIYYIWRBPlRYWj2/swgAbK+IMPpWQ=; b=jCBWUU99+zB7ZankRiIOqoRZKN2tqK5Se7eWIWgAOA9SOVwLXhoryvODhdTnXR+SAu bSp1C1cNwG0VF6RGtdvGgAVj34JkX4A9BmAqu8JCwzQnJvNbfbM24G58adxQOq3PsXvQ 37ao1v7f2ktvnm/w1Fej/j+4TGqY2/3dKt2PvP7LQy+2dhh31H8nvorSajDcTKpjJMJJ aR6jaxQXENTn+Tw0EHTbSvmYoYtg0h1+jL5envdGVVvYSHnc5dIoNCU75K0V0h2ZhWnC IDpmd8/1ZMmaifu+xUnjNUCWeeg2T6jwah1clPi476xxumC/UehM6abeY+NSrRECjPVe NMaQ== X-Gm-Message-State: AOJu0Yw7eQWBd2PbHUZYKL1Bh0rmxsvMRDi8JOOriM7Os5L+WIMnN7K+ YCw2hVsj0PAlJhYLKdsErv1+pC+1CdAp4QSf7RrR1S73wlMQM0Uxs2uxEz971axPiSVJt17vuxv al4kKpJ4vYVIM/aAozfPDHLIGl93O24OEyK+KVtU= X-Gm-Gg: ATEYQzwn8O2eSEXIxqll8JY7GeoJgF5GL3z2zL7JUEWpJPdyQlnIGaBGD6ZsCdWa7Cp jAKaDKvTYYZ/yk4xUs5ms29zPoJIOpXOXp41ZGFvFsdaTe0UE+Wcg4fDaHJeRaOIBR6koPT0roV T1K2YzHNk8pIsCkfma8ypGLNL0j7WYwpVmMQJbfJotn+kRq4ifW64rRht+jNx9kqJHu3jhBuo+J IenvW3pThipOr6M4HkzD+zYVyMN0JpPgM6DMB0KTrrUv8ruibRi2nN1XOwdu1qUw63W8ofLeOGz jBSy/e2k X-Received: by 2002:a05:6402:440a:b0:65c:611d:e6a6 with SMTP id 4fb4d7f45d1cf-660efcad345mr1160281a12.19.1772631752822; Wed, 04 Mar 2026 05:42:32 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Tender Wang Date: Wed, 4 Mar 2026 21:42:21 +0800 X-Gm-Features: AaiRm51YljVkzraULNA9ie0RWM-x7nnHUJcmeebH9gR_QdxPMdt3DVd5L_UQ6Nk Message-ID: Subject: Re: GROUP BY in CTE causes ELSE in outer query to be prematurely evaluated To: =?UTF-8?Q?Bj=C3=B6rn_Kautler?= Cc: pgsql-bugs@lists.postgresql.org, Laurenz Albe 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 Bj=C3=B6rn Kautler =E4=BA=8E2026=E5=B9=B43=E6=9C=884= =E6=97=A5=E5=91=A8=E4=B8=89 20:31=E5=86=99=E9=81=93=EF=BC=9A > > Hi > > If you have the query > > WITH FOO AS ( > SELECT 0 AS GROUPING > ) > SELECT CASE > WHEN GROUPING >=3D 0 THEN 'non-negative' > ELSE CAST((1 / 0) AS VARCHAR) > END > FROM FOO; > > it works successfully, having the ELSE as a safeguard against having code= d a bug, having forgotten a WHEN branch, so it fails fast. In the planner, the cte FOO is pulled up as a subquery and finally is transformed to be RTE_RESULT, and the GROUPING in the output of top query will be transformed to: "WHEN 0 >=3D 0 THEN 'non-negative' When the planner evaluates the targetList of the query, it finds that 0 >=3D 0 is true and 'non-negative' is a constant, so it returns immediately. The ELSE branch has no change to process. So the query works successfully. > it fails with a division by zero error. > > But if you have the query > > WITH FOO AS ( > SELECT 0 AS GROUPING > GROUP BY 1 > ) > SELECT CASE > WHEN GROUPING >=3D 0 THEN 'non-negative' > ELSE CAST((1 / 0) AS VARCHAR) > END > FROM FOO; > > then it always fails with division by zero error, even though the result = should still be 'non-negative'. > If you write CTE with group by, the CTE will continue to be transformed into a subquery, but the subquery can't be pulled up, seeing is_simple_subquery (). The GROUPING in the targetList will continue to be the Var node. It can't be simplified directly when pressing "WHEN GROUPING >=3D 0 THEN 'non-negative'" So ELSE CAST((1 / 0) AS VARCHAR) has the chance to process, then "division by zero" error is reported. --=20 Thanks, Tender Wang