Thread: Select query order
Hi All,
I am aware that Select query does not guarantee the order of the rows returned ( The rows are returned in whatever order the system finds fastest to produce).
Is there any way to SELECT the rows in the same order of insertion ? The problem is there is no Primary Key in the table, I am having only a composite key. So I cannot ORDER BY pk also.
I tried row_number() over () Window function but not of use
Thanks & Regards
Krithi
I am aware that Select query does not guarantee the order of the rows returned ( The rows are returned in whatever order the system finds fastest to produce).
Is there any way to SELECT the rows in the same order of insertion ? The problem is there is no Primary Key in the table, I am having only a composite key. So I cannot ORDER BY pk also.
I tried row_number() over () Window function but not of use
Thanks & Regards
Krithi
On Wed, Mar 24, 2010 at 8:06 AM, Krithinarayanan Ganesh Kumar <krithinarayanan@gmail.com> wrote: > Hi All, > > I am aware that Select query does not guarantee the order of the rows > returned ( The rows are returned in whatever order the system finds fastest > to produce). > > Is there any way to SELECT the rows in the same order of insertion ? The > problem is there is no Primary Key in the table, I am having only a > composite key. So I cannot ORDER BY pk also. Unless you have stored something in the table that correlates with the order of insertion, I don't know of a way to get at the insertion order as this information is not tracked by Postgresql. Sean
In response to Krithinarayanan Ganesh Kumar : > Hi All, > > I am aware that Select query does not guarantee the order of the rows returned > ( The rows are returned in whatever order the system finds fastest to produce). > > Is there any way to SELECT the rows in the same order of insertion ? The > problem is there is no Primary Key in the table, I am having only a composite > key. So I cannot ORDER BY pk also. You can't. You can use the ctid-column like my example: test=# create table Krithi ( i int); CREATE TABLE test=*# copy krithi from stdin; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself. >> 1 >> 2 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> \. test=*# commit; COMMIT test=# select ctid, i from krithi ; ctid | i -------+--- (0,1) | 1 (0,2) | 2 (0,3) | 3 (0,4) | 4 (0,5) | 5 (0,6) | 6 (0,7) | 7 (0,8) | 8 (8 rows) test=*# select ctid, i from krithi order by ctid; ctid | i -------+--- (0,1) | 1 (0,2) | 2 (0,3) | 3 (0,4) | 4 (0,5) | 5 (0,6) | 6 (0,7) | 7 (0,8) | 8 (8 rows) Okay, looks good, but if you do an update this will fail: test=*# update krithi set i=5 where i=5; UPDATE 1 test=*# select ctid, i from krithi order by ctid; ctid | i -------+--- (0,1) | 1 (0,2) | 2 (0,3) | 3 (0,4) | 4 (0,6) | 6 (0,7) | 7 (0,8) | 8 (0,9) | 5 (8 rows) As you can see, the old tuple (0,5) is deleted and a new (0,9) is created. Okay, some more traffic: test=# insert into krithi values (10); INSERT 0 1 test=*# select ctid, i from krithi order by ctid; ctid | i --------+---- (0,1) | 1 (0,2) | 2 (0,3) | 3 (0,4) | 4 (0,6) | 6 (0,7) | 7 (0,8) | 8 (0,9) | 5 (0,10) | 10 (9 rows) test=*# vacuum full krithi ; ERROR: VACUUM cannot run inside a transaction block test=!# rollback; ROLLBACK test=# vacuum full krithi ; VACUUM test=# insert into krithi values (11); INSERT 0 1 test=*# select ctid, i from krithi order by ctid; ctid | i -------+---- (0,1) | 1 (0,2) | 2 (0,3) | 3 (0,4) | 4 (0,5) | 5 (0,6) | 6 (0,7) | 7 (0,8) | 8 (0,9) | 11 (9 rows) Oh, as you can see, our row with i=5 is now on (0,5). In short: PG has no timestamp or similar for the insert-time for a record. But you can use, for instance, a new SERIAL column and order by this column. Or a timestamp default now(). Regards, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
On Wed, Mar 24, 2010 at 8:21 AM, Francisco Leovey <fleovey@yahoo.com> wrote:
Hi, Francisco. I hope you don't mind this going back to the list....
I wouldn't recommend this solution for several reasons including the fact that OIDs are not by default created for user tables. They are not guaranteed to be unique and even the insert order is not accurately tracked by them if there is a rollover. See here for details:
http://www.postgresql.org/docs/8.4/static/datatype-oid.html
Sean
Use oid as the sort orderSELECT *,oid from table order by oid
Hi, Francisco. I hope you don't mind this going back to the list....
I wouldn't recommend this solution for several reasons including the fact that OIDs are not by default created for user tables. They are not guaranteed to be unique and even the insert order is not accurately tracked by them if there is a rollover. See here for details:
http://www.postgresql.org/docs/8.4/static/datatype-oid.html
Sean
--- On Wed, 3/24/10, Sean Davis <sdavis2@mail.nih.gov> wrote:
From: Sean Davis <sdavis2@mail.nih.gov>
Subject: Re: [NOVICE] Select query order
To: "Krithinarayanan Ganesh Kumar" <krithinarayanan@gmail.com>
Cc: "pgsql-novice@postgresql.org" <pgsql-novice@postgresql.org>
Date: Wednesday, March 24, 2010, 9:13 AM--On Wed, Mar 24, 2010 at 8:06 AM, Krithinarayanan Ganesh Kumar
<krithinarayanan@gmail.com> wrote:
> Hi All,
>
> I am aware that Select query does not guarantee the order of the rows
> returned ( The rows are returned in whatever order the system finds fastest
> to produce).
>
> Is there any way to SELECT the rows in the same order of insertion ? The
> problem is there is no Primary Key in the table, I am having only a
> composite key. So I cannot ORDER BY pk also.
Unless you have stored something in the table that correlates with the
order of insertion, I don't know of a way to get at the insertion
order as this information is not tracked by Postgresql.
Sean
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
On 2010-03-24, Krithinarayanan Ganesh Kumar <krithinarayanan@gmail.com> wrote: > --000325560bde5e345c04828ac143 > Content-Type: text/plain; charset=ISO-8859-1 > > Hi All, > > I am aware that Select query does not guarantee the order of the rows > returned ( The rows are returned in whatever order the system finds fastest > to produce). > > Is there any way to SELECT the rows in the same order of insertion ? only if the records contain that information. > The > problem is there is no Primary Key in the table, I am having only a > composite key. So I cannot ORDER BY pk also. You can order by the primary key, but from your description it's unlikely to give insertion order.