Re: Hash grouping, aggregates

Lists: pgsql-hackers
From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Hash grouping, aggregates
Date: 2003-02-11 14:48:11
Message-ID: 87n0l2rhzo.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


So one of the items on the TODO list is "Add hash for evaluating GROUP BY
aggregates (Tom)"

I'm finding this would benefit a lot of my queries. Most of the time seems to
be going into sorts for group by clauses. I don't know how long it would take
to build a hash of course, but I suspect it would be less than the sort.

Is this something a beginner could figure out? I'm thinking I need a normal
Hash node that builds exactly the same kind of hash as a join, then a HashScan
node that picks all the rows out of the hash.

The neat thing is that hash aggregates would allow grouping on data types that
have = operators but no useful < operator.

(Incidentally, I'm fond of "nested loop", I remember when I was a beginner SQL
programmer looking at plans and it was intuitively obvious what it meant. I
suspect for a beginner looking at "nestloop" it might not be quite so
obvious.)

--
greg


From: Greg Stark <gsstark(at)mit(dot)edu>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Greg Stark <gsstark(at)MIT(dot)EDU>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 15:18:32
Message-ID: 87el6ergl3.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Bruno Wolff III <bruno(at)wolff(dot)to> writes:

> This is already in 7.4. You could try it out by building from CVS.
> From the HISTORY file:
> System can use either hash- or sort-based strategy for grouped
> aggregation

Ooh, doing that now. Thanks.

--
greg


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 15:28:28
Message-ID: 20030211152828.GA31840@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Feb 11, 2003 at 09:48:11 -0500,
Greg Stark <gsstark(at)mit(dot)edu> wrote:
>
> So one of the items on the TODO list is "Add hash for evaluating GROUP BY
> aggregates (Tom)"
>
> I'm finding this would benefit a lot of my queries. Most of the time seems to
> be going into sorts for group by clauses. I don't know how long it would take
> to build a hash of course, but I suspect it would be less than the sort.
>
> Is this something a beginner could figure out? I'm thinking I need a normal
> Hash node that builds exactly the same kind of hash as a join, then a HashScan
> node that picks all the rows out of the hash.

This is already in 7.4. You could try it out by building from CVS.
>From the HISTORY file:
System can use either hash- or sort-based strategy for grouped
aggregation


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 15:41:53
Message-ID: 25794.1044978113@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Greg Stark <gsstark(at)mit(dot)edu> writes:
> So one of the items on the TODO list is "Add hash for evaluating GROUP BY
> aggregates (Tom)"

It's done in CVS tip ... give it a try.

> The neat thing is that hash aggregates would allow grouping on data types that
> have = operators but no useful < operator.

Hm. Right now I think that would barf on you, because the parser wants
to find the '<' operator to label the grouping column with, even if the
planner later decides not to use it. It'd take some redesign of the
query data structure (specifically SortClause/GroupClause) to avoid that.

regards, tom lane


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 16:39:52
Message-ID: 26171.1044981592@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruno Wolff III <bruno(at)wolff(dot)to> writes:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Greg Stark <gsstark(at)mit(dot)edu> writes:
>>> The neat thing is that hash aggregates would allow grouping on data types that
>>> have = operators but no useful < operator.
>>
>> Hm. Right now I think that would barf on you, because the parser wants
>> to find the '<' operator to label the grouping column with, even if the
>> planner later decides not to use it. It'd take some redesign of the
>> query data structure (specifically SortClause/GroupClause) to avoid that.

> I think another issue is that for some = operators you still might not
> be able to use a hash. I would expect the discussion for hash joins in
> http://developer.postgresql.org/docs/postgres/xoper-optimization.html
> would to hash aggregates as well.

Right, the = operator must be hashable or you're out of luck. But we
could imagine tweaking the parser to allow GROUP BY if it finds a
hashable = operator and no sort operator. The only objection I can see
to this is that it means the planner *must* use hash aggregation, which
might be a bad move if there are too many distinct groups.

regards, tom lane


From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Greg Stark <gsstark(at)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 16:44:04
Message-ID: 20030211164404.GA32571@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Feb 11, 2003 at 10:41:53 -0500,
Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Greg Stark <gsstark(at)mit(dot)edu> writes:
> > So one of the items on the TODO list is "Add hash for evaluating GROUP BY
> > aggregates (Tom)"
>
> It's done in CVS tip ... give it a try.
>
> > The neat thing is that hash aggregates would allow grouping on data types that
> > have = operators but no useful < operator.
>
> Hm. Right now I think that would barf on you, because the parser wants
> to find the '<' operator to label the grouping column with, even if the
> planner later decides not to use it. It'd take some redesign of the
> query data structure (specifically SortClause/GroupClause) to avoid that.

I think another issue is that for some = operators you still might not
be able to use a hash. I would expect the discussion for hash joins in
http://developer.postgresql.org/docs/postgres/xoper-optimization.html
would to hash aggregates as well.


From: Hannu Krosing <hannu(at)tm(dot)ee>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruno Wolff III <bruno(at)wolff(dot)to>, Greg Stark <gsstark(at)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-11 20:21:26
Message-ID: 1044994885.1607.5.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane kirjutas T, 11.02.2003 kell 18:39:
> Bruno Wolff III <bruno(at)wolff(dot)to> writes:
> > Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >> Greg Stark <gsstark(at)mit(dot)edu> writes:
> >>> The neat thing is that hash aggregates would allow grouping on data types that
> >>> have = operators but no useful < operator.
> >>
> >> Hm. Right now I think that would barf on you, because the parser wants
> >> to find the '<' operator to label the grouping column with, even if the
> >> planner later decides not to use it. It'd take some redesign of the
> >> query data structure (specifically SortClause/GroupClause) to avoid that.
>
> > I think another issue is that for some = operators you still might not
> > be able to use a hash. I would expect the discussion for hash joins in
> > http://developer.postgresql.org/docs/postgres/xoper-optimization.html
> > would to hash aggregates as well.
>
> Right, the = operator must be hashable or you're out of luck. But we
> could imagine tweaking the parser to allow GROUP BY if it finds a
> hashable = operator and no sort operator. The only objection I can see
> to this is that it means the planner *must* use hash aggregation, which
> might be a bad move if there are too many distinct groups.

If we run out of sort memory, we can always bail out later, preferrably
with a descriptive error message. It is not as elegant as erring out at
parse (or even plan/optimise) time, but the result is /almost/ the same.

Relying on hash aggregation will become essential if we are ever going
to implement the "other" groupings (CUBE, ROLLUP, (), ...), so it would
be nice if hash aggregation could also overflow to disk - I suspect that
this will still be faster that running an independent scan for each
GROUP BY grouping and merging the results.

-----
Hannu


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Hannu Krosing <hannu(at)tm(dot)ee>
Cc: Bruno Wolff III <bruno(at)wolff(dot)to>, Greg Stark <gsstark(at)mit(dot)edu>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Hash grouping, aggregates
Date: 2003-02-12 05:12:03
Message-ID: 830.1045026723@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hannu Krosing <hannu(at)tm(dot)ee> writes:
> Relying on hash aggregation will become essential if we are ever going
> to implement the "other" groupings (CUBE, ROLLUP, (), ...), so it would
> be nice if hash aggregation could also overflow to disk

I did not make this happen, but it sounds like Joe and Natassa are
about to send their students out to do it ... in a month or so, we
can review the homework assignments and decide who gets an A ;-)

regards, tom lane