Обсуждение: window function docs

Поиск
Список
Период
Сортировка

window function docs

От
"Robert Haas"
Дата:
Section 7.2.4 of the docs, third paragraph, currently reads:

> Currently, use of window functions always forces sorting, and so the query output will be ordered according to one or
anotherof the window functions' PARTITION BY/ORDER
 
> BY clauses. It is not recommendable to rely on this, however. Use an explicit top-level ORDER BY clause if you want
tobe sure the results are sorted in a particular way.
 

Recommendable is not a commonly used word in English, and I think the
rest of the paragraph can be tightened up a bit.  Maybe something like
this:

Currently, use of window functions always forces sorting, so the query
output will be ordered according to one or another of the window
functions' PARTITION BY/ORDER BY clauses.  However, this may change in
the future, so you should use an explicit top-level ORDER BY clause if
you want the results sorted in a particular way.

Also, section 3.5 of the docs does not references any of the other
sections that talk about window functions (7.2.4, 4.2.8, 9.19), and
7.2.4 does not reference 9.19 either.  I'm not sure what the policy on
cross-references but it took me a bit to find everything.

...Robert


Re: window function docs

От
Heikki Linnakangas
Дата:
Robert Haas wrote:
> Currently, use of window functions always forces sorting, ...

That's not strictly true, actually. No sort is done if the input happens 
to be sorted already, as in:

postgres=# CREATE TABLE foo (id integer PRIMARY KEY);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index 
"foo_pkey" for table "foo"
CREATE TABLE
postgres=# INSERT INTO foo SELECT generate_series(1,100);
INSERT 0 100
postgres=# explain SELECT SUM(id) OVER (ORDER BY id) FROM (SELECT * FROM 
foo WHERE id < 100) AS a;                                 QUERY PLAN 

----------------------------------------------------------------------------- WindowAgg  (cost=0.00..70.25 rows=800
width=4)  ->  Index Scan using foo_pkey on foo  (cost=0.00..58.25 rows=800 
 
width=4)         Index Cond: (id < 100)
(3 rows)

--   Heikki Linnakangas  EnterpriseDB   http://www.enterprisedb.com