Re: window function count(*) and limit

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jesper Krogh <jesper@krogh.cc>
Cc: pgsql-hackers@postgresql.org
Date: 2010-10-23T16:42:49Z
Lists: pgsql-hackers
Jesper Krogh <jesper@krogh.cc> writes:
> I have been puzzled about the evaluation order when using window
> functions and limit.

It's basically FROM -> WHERE -> window functions -> LIMIT.

> I expected it to either count to 3 or blow up and tell me that count(*)
> wasn't a window function.

Any aggregate function can be used as a window function.  It just
aggregates over the whole partition (which in this case is all 8
rows that satisfy the WHERE).

LIMIT is supposed to truncate the output at a specified number of
rows, not change what is in those rows; so ISTM that this evaluation
order is the expected one.  If you want to put a LIMIT in front of
the aggregates/window functions, put it in a sub-select.

			regards, tom lane