Re: query optimization: aggregate and distinct - Mailing list pgsql-general

From Tom Lane
Subject Re: query optimization: aggregate and distinct
Date
Msg-id 4568.1061472999@sss.pgh.pa.us
Whole thread Raw
In response to Re: query optimization: aggregate and distinct  (Jeff Davis <jdavis-pgsql@empires.org>)
Responses Re: query optimization: aggregate and distinct  (Jeff Davis <jdavis-pgsql@empires.org>)
List pgsql-general
Jeff Davis <jdavis-pgsql@empires.org> writes:
> I had an idea about using aggregates: what if I made an aggregate function
> called "first" that just returned the value in the first tuple it
> encountered?

You could make that work in 7.4, but not in any existing releases.

The trouble is that you need something like

    SELECT first(foo) FROM (SELECT ... ORDER BY col1,col2) ss
    GROUP BY col1

and before 7.4 the optimizer doesn't realize that it can skip re-sorting
at the outer level.  So unless the sort is stable (which it won't be, on
most platforms anyway) the needed ordering by col2 within each group is
destroyed.

            regards, tom lane

pgsql-general by date:

Previous
From: "Vilson farias"
Date:
Subject: timeofday() and CAST
Next
From: Tom Lane
Date:
Subject: Re: timeofday() and CAST