Clustered table order is not preserved on insert

From: "Andrus" <eetasoft(at)online(dot)ee>
To: pgsql-general(at)postgresql(dot)org
Subject: Clustered table order is not preserved on insert
Date: 2006-04-26 18:55:49
Message-ID: e2ofnp$m5b$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have table of reports

CREATE TABLE report (
ReportName CHAR(5) not null check (reportname<>''),
< a lot of other fields >,
id serial primary key
)

I want to duplicate report so that id order is preserved.

BEGIN;
CREATE temp TABLE tempreport AS
SELECT * FROM report
WHERE reportname='oldr'
ORDER BY id;

ALTER TABLE tempreport DROP COLUMN id;
update tempreport set reportname='newr';
insert into report SELECT * FROM tempreport;
DROP TABLE tempreport;
COMMIT;

SELECT *
FROM report
WHERE reportname='newr'
ORDER BY id;

Observed:

order of some rows in newr is different than in oldr

Expected:

newr must have exactly the same order since
CREATE temp TABLE tempreport AS .... ORDER BY id
creates clustered table.

Is this best method to preform this?
Why postgres 8.1.3 changes order ?
How to preserve order in newr without adding extra field to report table ?

Andrus.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Douglas McNaught 2006-04-26 19:00:44 Re: Clustered table order is not preserved on insert
Previous Message Richard Huxton 2006-04-26 18:53:19 Re: pg_dump -t <> pg_restore -t