Re: Re: [GENERAL] question (or feature-request): over ( partition by ... order by LIMIT N)

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Re: [GENERAL] question (or feature-request): over ( partition by ... order by LIMIT N)
Date: 2010-03-25 22:50:19
Message-ID: 603c8f071003251550h3a93b069u30246458e6a2c46d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Thu, Mar 25, 2010 at 5:17 PM, David Fetter <david(at)fetter(dot)org> wrote:
> On Wed, Mar 24, 2010 at 06:31:59PM +0100, A. Kretschmer wrote:
>> Hello @all,
>>
>> I know, i can do:
>>
>> select * from (select ... row_number() over (...) ...) foo where
>> row_number < N
>>
>> to limit the rows per group, but the inner select has to retrieve
>> the whole set of records and in the outer select most of them
>> discarded.
>
> That sounds like the optimizer's falling down on the job.  Would this
> be difficult to fix?

I may not be the best person to offer an opinion on this topic, but it
sounds tricky to me. I think it would need some kind of extremely
specific special-case logic. The planner would have to recognize
row_number() < n, row_number() <= n, and row_number = n as special
cases indicating that n-1, n, and n records respectively should be
expected to be fetched from the partition. And you might also worry
about n > row_number(), and n >= row_number().

It might be worth doing because I suspect that is actually going to be
a fairly common type of query, but some thought needs to be given to
how to do it without resorting to abject kludgery.

...Robert

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Geoffrey Gowey 2010-03-26 02:04:16 Geoffrey Gowey wants to stay in touch on LinkedIn
Previous Message John R Pierce 2010-03-25 22:22:54 Re: Get the list of permissions/privileges on schema

Browse pgsql-hackers by date

  From Date Subject
Next Message Thom Brown 2010-03-26 00:01:54 Remove ROW | ROWS from OFFSET and FETCH
Previous Message David Fetter 2010-03-25 21:17:31 Re: [GENERAL] question (or feature-request): over ( partition by ... order by LIMIT N)