pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: tobias.wendorff@tu-dortmund.de
Subject: BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()
Date: Wed, 23 Jun 2021 13:05:23 +0000
Message-ID: <17071-24dc13fbfa29672d@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      17071
Logged by:          Tobias Wendorff
Email address:      tobias.wendorff@tu-dortmund.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.



view thread (5+ messages)  latest in thread

Message-ID: <17071-24dc13fbfa29672d@postgresql.org>
Permalink:  ../17071-24dc13fbfa29672d@postgresql.org/
Also on:    postgresql.org/message-id/17071-24dc13fbfa29672d@postgresql.org

 · 

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: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, tobias.wendorff@tu-dortmund.de
  Subject: Re: BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()
  In-Reply-To: <17071-24dc13fbfa29672d@postgresql.org>

* 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