pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: 谭忠涛 <zhongtao.tan@seaboxdata.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
Subject: 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
Date: Fri, 3 Jan 2025 09:17:58 +0800
Message-ID: <tencent_36E46A690C75D7AE585BE432@qq.com> (raw)

Thank you for your feedback.


regards


                    Tom Lane<tgl@sss.pgh.pa.us> 在 2025年1月3日 周五 0:04 写道:

"=?utf-8?B?6LCt5b+g5rab?=" <zhongtao.tan@seaboxdata.com> 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 = 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 = 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

view thread (2+ messages)

Message-ID: <tencent_36E46A690C75D7AE585BE432@qq.com>
Permalink:  ../tencent_36E46A690C75D7AE585BE432@qq.com/
Also on:    postgresql.org/message-id/tencent_36E46A690C75D7AE585BE432@qq.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: zhongtao.tan@seaboxdata.com, tgl@sss.pgh.pa.us, pgsql-bugs@lists.postgresql.org
  Subject: Re: 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
  In-Reply-To: <tencent_36E46A690C75D7AE585BE432@qq.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox