pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Logan Bowers <logan.bowers@gmail.com>
To: pgsql-bugs@lists.postgresql.org
Subject: Optimizer Doesn't Push Down Where Expressions on Rollups
Date: Mon, 9 Mar 2020 14:05:27 -0700
Message-ID: <17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.com> (raw)

Hello, 

I’m running into a performance issue where I’m trying to introduce a rollup into GROUP BY clause. I believe the query planner is missing an optimization when grouping by multiple terms. 

Here’s an example that replicates the problem: 

BEGIN;
CREATE TABLE limitation_demo (c1 integer, c2 integer, c3 integer, c4 integer);

INSERT INTO limitation_demo SELECT
    (random() * 20)::integer,
    (random() * 20)::integer,
    (random() * 20)::integer,
    (random() * 20)::integer
  FROM generate_series(0,10000) AS foo;

CREATE INDEX idx1 ON limitation_demo (c1, c2, c3);

--Good
EXPLAIN SELECT * FROM (SELECT c1,c2,c3, sum(c4) FROM limitation_demo GROUP BY c1,c2,c3) AS foo  WHERE c1 = 1 AND c2 = 5 ;
--Bad
EXPLAIN SELECT * FROM (SELECT c1,c2,c3, sum(c4) FROM limitation_demo GROUP BY c1, c2, ROLLUP(c3)) AS foo  WHERE c1 = 1 AND c2 = 5;
ROLLBACK;


Here are the respective query plans: 

                                    QUERY PLAN
-----------------------------------------------------------------------------------
 GroupAggregate  (cost=0.29..8.32 rows=1 width=20)
   Group Key: limitation_demo.c1, limitation_demo.c2, limitation_demo.c3
   ->  Index Scan using idx1 on limitation_demo  (cost=0.29..8.30 rows=1 width=16)
         Index Cond: ((c1 = 1) AND (c2 = 5))
(4 rows)

                                 QUERY PLAN
----------------------------------------------------------------------------
 HashAggregate  (cost=255.02..360.03 rows=2 width=20)
   Hash Key: limitation_demo.c1, limitation_demo.c2, limitation_demo.c3
   Hash Key: limitation_demo.c1, limitation_demo.c2
   Filter: ((limitation_demo.c1 = 1) AND (limitation_demo.c2 = 5))
   ->  Seq Scan on limitation_demo  (cost=0.00..155.01 rows=10001 width=16)
(5 rows)

Despite being semantically equivalent, I believe, the latter plan with the rollup does not use the index. I believe what is happening is that the WHERE clause is getting pushed down into the inner query without the ROLLUP, but is not with the ROLLUP. 

This is a simplified example. In my real-world use case, the inner query is a view, so I don’t have the option of moving the where clause. 

Can y’all let me know if I should submit this somewhere else? Thanks! 

view thread (6+ messages)  latest in thread

Message-ID: <17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.com>
Permalink:  ../17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.com/
Also on:    postgresql.org/message-id/17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: logan.bowers@gmail.com
  Subject: Re: Optimizer Doesn't Push Down Where Expressions on Rollups
  In-Reply-To: <17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.com>
  References: <17F738BE-8D45-422C-BAD0-ACA3090BF46D@gmail.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