Re: How to implement oracle like rownum(function or seudocolumn)

From: "Jonah H(dot) Harris" <jonah(dot)harris(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Jan Wieck" <JanWieck(at)yahoo(dot)com>, "Michael Fuhr" <mike(at)fuhr(dot)org>, "Juan Manuel Diaz Lara" <jmdiazlr(at)yahoo(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: How to implement oracle like rownum(function or seudocolumn)
Date: 2006-04-08 19:19:43
Message-ID: 36e682920604081219q31adf9dct75b4c9fc65c3b3c1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 4/8/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> A function implemented as per Michael's example would not give the
> results that I think people would expect for
>
> SELECT rownum(), * FROM foo ORDER BY whatever;
>

Yep, the query would have to be rewritten similar to Oracle's:

SELECT rownum(), * FROM (SELECT * FROM foo ORDER BY whatever);

IIRC, processing-wise, rownum and order-by processing is handled as follows:

SELECT id, whatever FROM foo WHERE rownum <= 10 ORDER BY id;

is the same as PostgreSQL's

SELECT id, whatever FROM (SELECT id, whatever FROM foo LIMIT 10) ORDER BY id;

--
Jonah H. Harris, Database Internals Architect
EnterpriseDB Corporation
732.331.1324

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Josh Berkus 2006-04-08 19:54:54 Summer of Code -- mentors needed as well
Previous Message Tom Lane 2006-04-08 19:04:40 Re: How to implement oracle like rownum(function or seudocolumn)