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 1tTNgX-005BNa-Vt for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jan 2025 16:04:34 +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 1tTNgW-00Gkvb-JV for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jan 2025 16:04:32 +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.94.2) (envelope-from ) id 1tTNgW-00GkvT-B6 for pgsql-bugs@lists.postgresql.org; Thu, 02 Jan 2025 16:04:32 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tTNgP-002qfA-OW for pgsql-bugs@lists.postgresql.org; Thu, 02 Jan 2025 16:04:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 502G4NLc270214; Thu, 2 Jan 2025 11:04:24 -0500 From: Tom Lane To: "=?utf-8?B?6LCt5b+g5rab?=" cc: "=?utf-8?B?cGdzcWwtYnVncw==?=" Subject: Re: Incorrect sort result caused by ROLLUP and WHERE operation In-reply-to: References: Comments: In-reply-to "=?utf-8?B?6LCt5b+g5rab?=" message dated "Thu, 02 Jan 2025 15:28:25 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <270212.1735833863.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 02 Jan 2025 11:04:23 -0500 Message-ID: <270213.1735833863@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "=3D?utf-8?B?6LCt5b+g5rab?=3D" writes: > create table t1(a int, b int); > insert into t1 values(1,1),(1,2); > select a,b,sum(10) as s from t1 where a =3D 1 group by rollup(a,b) order= by a, s desc; Yeah. This is fixed in HEAD (v18-to-be) but the fix is too invasive to consider back-patching. Basically the problem is that older versions don't understand that the post-ROLLUP value of "a" can be different from the pre-ROLLUP value, so they think the "where a =3D 1" clause removes any need to sort by "a". As a workaround you could write something like "order by a+0, s desc" to fool the optimizer into considering the ordering column to be different from the value that's constrained by WHERE. regards, tom lane