Re: MAX/MIN optimization via rewrite (plus query rewrites - Mailing list pgsql-hackers

From Greg Stark
Subject Re: MAX/MIN optimization via rewrite (plus query rewrites
Date
Msg-id 871xeva962.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: MAX/MIN optimization via rewrite (plus query rewrites  (Jan Wieck <JanWieck@Yahoo.com>)
Responses Re: MAX/MIN optimization via rewrite (plus query rewrites  (Dawid Kuroczko <qnex42@gmail.com>)
List pgsql-hackers
Jan Wieck <JanWieck@Yahoo.com> writes:

> Both cases can be expressed with order by + limit queries, that would indeed
> utilize those indexes. But what's been discussed so far does not cover any of
> them.

I think people should get away from thinking about "order by + limit". That
isn't going to work for anything with a GROUP BY. And it isn't going to work
for anything more complex than a single min() or max().

min() only needs the first record from whatever set of records it's operating
on as long as they're provided in a specified order. This is just as true for
a min() applied to only a single GROUP as it is for a min() applied to an
entire table.

I don't think you want to use the existing Limit executor node. That will only
ever let you handle these simple aggregates that return the first value they
see. What you want is a normal Aggregate node, but the node feeding it should
be an altered index scan that knows it only needs to pull out the first and/or
last record for each GROUP.

That will let you handle min() and max() in the same query for example. It
might also leave open the door for other more complex data subsets. Say a
geometric data type where it needs all the bounding points of an area.

-- 
greg



pgsql-hackers by date:

Previous
From: joseph speigle
Date:
Subject: Re: psql \e broken again
Next
From: Christopher Kings-Lynne
Date:
Subject: Odd plpgsql behaviour