Re: COPY TO order

From: Guy Fraser <guy(at)incentre(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: COPY TO order
Date: 2004-04-12 17:02:45
Message-ID: 407ACBB5.3020003@incentre.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Christopher Browne wrote:

>Centuries ago, Nostradamus foresaw when clodoaldo_pinto(at)yahoo(dot)com(dot)br (Clodoaldo Pinto Neto) would write:
>
>
>>How to make sure COPY TO writes the table lines to the file in the same order
>>they were inserted?
>>
>>
>
>You probably want to rewrite PostgreSQL then.
>
>
>
>>I'm producing html pages in pl/pgsql and using COPY TO to write then
>>to file. Occasionaly, about once in 7 or 9, the lines are copied to
>>the file out of the order they were inserted in the table.
>>
>>
>
>If you need to maintain data in some order, then you need to add a key
>field that indicates that ordering, and use ORDER BY in order to
>select the data in that order.
>
>That will involve not using COPY TO.
>
>
Not really.

If you have a 'serial' or 'bigserial' field like this :

create table test_table (
test_id bigserial,
data integer,
comment text
);

and you use :

copy test_table (data,comment)
from '/wherever/the/file/is'
using delimiters ',';

to insert data like this :

27,some kind of entry
32,another kind of entry
16,yet another entry
...

Assuming this is the first set of data entered the table will get populated with :

1 | 27 | some kind of entry
2 | 32 | another kind of entry
3 | 16 | yet another entry
...

I have used this in the past and it works well.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Clodoaldo Pinto Neto 2004-04-12 17:15:30 Re: COPY TO order
Previous Message Tom Lane 2004-04-12 15:51:06 Re: ERROR: REINDEX DATABASE: Can be executed only on the currently open database.