sort/limit across union all - Mailing list pgsql-performance

From Marc Morin
Subject sort/limit across union all
Date
Msg-id 2BCEB9A37A4D354AA276774EE13FB8C263B0F4@mailserver.sandvine.com
Whole thread Raw
List pgsql-performance
We have a large DB with partitioned tables in postgres.   We have had trouble with a ORDER/LIMIT type query.  The order and limit are not pushed down to the sub-tables....
 
CREATE TABLE base (
    foo int
);
 
CREATE TABLE bar_0
    extra int
) INHERITS (base);
ALTER TABLE bar ADD PRIMARY KEY (foo);
 
-- repeated for bar_0... bar_40
 
SELECT foo FROM base ORDER BY foo LIMIT 10;
 
is real slow. What is required to make the query planner generate the following instead... (code change i know, but how hard would it be?)
 
SELECT
    foo
FROM
(
    SELECT
        *
    FROM bar_0
    ORDER BY foo LIMIT 10
UNION ALL
    SELECT
        *
    FROM bar_1
    ORDER BY foo LIMIT 10
....
) AS base
ORDER BY foo
LIMIT 10;
 
 

pgsql-performance by date:

Previous
From: Ron
Date:
Subject: Re: Hardware/OS recommendations for large databases
Next
From: Leigh Dyer
Date:
Subject: Re: Help speeding up delete