Re: Sort Order inconsistent when using Grouping Sets Rollup

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chris Rohlfs <seeken(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Sort Order inconsistent when using Grouping Sets Rollup
Date: 2022-11-22 15:41:06
Message-ID: 3455209.1669131666@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Chris Rohlfs <seeken(at)gmail(dot)com> writes:
> The attached file will create a table and run 2 queries, the first of which
> is not honoring the requested sort order. I expect the Nulls first.

Yeah, this is a known issue that's a bit difficult to fix. Because
you've constrained both the first_name and last_name columns to have
unique values:

> where (( "actor_id" >= 1 ) and ( "last_name" = 'GUINESS' ) and (
> "first_name" = 'PENELOPE' ))

the planner is of the opinion that sorting on those columns is a no-op,
so it doesn't bother to emit a sort step after the aggregation. We
need to teach it that the output of GROUP BY, when there are grouping
sets, is not identical to the input because of possible injection of
null values. I'm working on some patches that should lead to that result,
but they won't appear till v16 at the earliest.

As a grotty workaround, you could do the ordering in a different
query level:

=# explain select * from (
select "last_name", "first_name", count("actor_id")
from actor
where (( "actor_id" >= 1 ) and ( "last_name" = 'GUINESS' ) and ( "first_name" = 'PENELOPE' ))
group by rollup( 1, 2 )
) ss
order by 2 asc nulls first, 1 asc nulls first;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------
Sort (cost=23.71..23.72 rows=3 width=72)
Sort Key: actor.first_name NULLS FIRST, actor.last_name NULLS FIRST
-> GroupAggregate (cost=0.00..23.69 rows=3 width=72)
Group Key: actor.last_name, actor.first_name
Group Key: actor.last_name
Group Key: ()
-> Seq Scan on actor (cost=0.00..23.65 rows=1 width=68)
Filter: ((actor_id >= 1) AND (last_name = 'GUINESS'::text) AND (first_name = 'PENELOPE'::text))
(8 rows)

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2022-11-22 23:49:50 Re: BUG #17233: Incorrect behavior of DELETE command with bad subquery in WHERE clause
Previous Message Thomas Weiner 2022-11-22 09:31:17 Segmentfault in PG 11.18 / Postgis 3.3.1