BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: tobias(dot)wendorff(at)tu-dortmund(dot)de
Subject: BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()
Date: 2021-06-23 13:05:23
Message-ID: 17071-24dc13fbfa29672d@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17071
Logged by: Tobias Wendorff
Email address: tobias(dot)wendorff(at)tu-dortmund(dot)de
PostgreSQL version: 12.6
Operating system: Debian Buster
Description:

If the result set of a query has only one row, but an additional row gets
added by GROUP BY rollup(), a subsequent ORDER BY is ignored by the
planner.

test case:
https://dbfiddle.uk/?rdbms=postgres_9.5&fiddle=e467725a3fae6df51f7ad8e89052de98

query:
SELECT
'2021-01-01'::date AS month
GROUP BY
rollup(month)
ORDER BY
month NULLS FIRST;

query plan:
GroupAggregate (cost=0.00..0.03 rows=2 width=0) (actual time=0.003..0.003
rows=2 loops=1)
Group Key: '2021-01-01'::date
Group Key: ()
-> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.000..0.000
rows=1 loops=1)
Planning time: 0.017 ms
Execution time: 0.027 ms

workaround:
CTE or subquery with subsequent sorting

Tested on dbfiddle.uk using versions 9.5, 9.6, 10, 11, 12 and 13.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2021-06-23 13:24:38 Re: Found a buffer-overflow defect in asynchronous database connection API PQconnectPoll
Previous Message Sudheer H R 2021-06-23 12:40:57 Re: Found a buffer-overflow defect in asynchronous database connection API PQconnectPoll