sort/limit across union all

From: "Marc Morin" <marc(at)sandvine(dot)com>
To: <psql-performance(at)postgresql(dot)org>
Subject: sort/limit across union all
Date: 2005-11-14 13:25:10
Message-ID: 2BCEB9A37A4D354AA276774EE13FB8C263B0F4@mailserver.sandvine.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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;


Browse pgsql-performance by date

  From Date Subject
Next Message Kelly Burkhart 2005-11-14 14:43:30 Re: 8.x index insert performance
Previous Message Tom Lane 2005-11-12 15:53:14 Re: IO Error