Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 99C079FB4F2 for ; Fri, 13 Oct 2006 19:13:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 73307-04 for ; Fri, 13 Oct 2006 22:12:55 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 1EA669FB486 for ; Fri, 13 Oct 2006 19:12:55 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.180]) by svr4.postgresql.org (Postfix) with ESMTP id 3C9405AF03C for ; Fri, 13 Oct 2006 22:12:54 +0000 (GMT) Received: by py-out-1112.google.com with SMTP id d80so1323777pyd for ; Fri, 13 Oct 2006 15:11:53 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=XEuR+SZiJB3pBKlWzXlxn53kl82buTuKVPGwHhkM6LDB7xZ51+yOXUEtlWpM7pIMqakXn/t4G7vRdQFIBg8yYX9VeytAgYV65swAtsI8gsAm4BtUcpwcxkItYGMQwoOw0MQvv3Mpug3+MhAHUbXknwEDZlDjG3a5NCr2sdhtqYY= Received: by 10.35.39.13 with SMTP id r13mr6560304pyj; Fri, 13 Oct 2006 14:45:18 -0700 (PDT) Received: by 10.35.80.9 with HTTP; Fri, 13 Oct 2006 14:45:18 -0700 (PDT) Message-ID: Date: Fri, 13 Oct 2006 16:45:18 -0500 From: "Aaron Bono" To: "Jim C. Nasby" Subject: Re: Copying data from table to table (cloned tables) Cc: "Bricklen Anderson" , "Fourat Zouari" , pgsql-novice@postgresql.org, pgsql-admin@postgresql.org In-Reply-To: <20061010152457.GG72517@nasby.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_34273_8371204.1160775918402" References: <621eda8a0610091651oc78994fy2b358260f5c9b58a@mail.gmail.com> <452AE2FF.1020409@presinet.com> <20061010152457.GG72517@nasby.net> X-Google-Sender-Auth: c9006c8c436781e3 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.27 tagged_above=0 required=5 tests=DNS_FROM_RFC_ABUSE, HTML_MESSAGE, SPF_NEUTRAL X-Spam-Level: * X-Archive-Number: 200610/96 X-Sequence-Number: 23399 ------=_Part_34273_8371204.1160775918402 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/10/06, Jim C. Nasby wrote: > > On Mon, Oct 09, 2006 at 05:02:07PM -0700, Bricklen Anderson wrote: > > Fourat Zouari wrote: > > >Hello all, > > >Any one could suggest the best way to copy data from table to table in > > >the same db, the reason why am seeking for this is that the first table > > >is becoming very big, and old data has no reason why to stay there, so > i > > >created a cloned table but without indexes and constraints (lighter) > and > > >now i would like to copy 'some' data from first to second table (copied > > >data is data older than 3 months, i have a timestamp column). > > > > > >In other way, i have a table called 'hotqueues' where i store fresh > > >messages queued, once messages are treated, they stay in 'hotqueues' > but > > >with a flag indicating that their arent queued for treatment.. > > >so in this way, data will rest there forever, slowing down any searches > > >in that table, the solution was to copy old messages to another table > > >called 'coldqueues' that has the same structure as 'hotqueues' but > > >lighter (without constraints and indexes). > > >How to copy these data with 100% data-loose free. > > > > > >Thanks for any help you can provide. > > > > If you just want to copy the data across to the other table: > > begin; > > insert into table2 select * from table1 where ; > > commit; > > > > if you also want to remove that same data from table1: > > begin; > > insert into table2 select * from table1 where ; > > delete from table1 where ; > > commit; > > You need to be careful with this method. For what the OP wants to do it > would probably work, but not always. The problem is that in some > scenarios, won't necessarily return the same > set of rows. > > Starting in 8.2 you'll be able to do something like > > INSERT INTO table2 DELET FROM table1 WHERE ... RETURNING *; > > The RETURNING * will return all the data that the command deleted. In > older versions, your best bet is to store the data you're moving in a > temporary table, and then use that to delete the exact rows. You may want to lock the table before doing the copy/delete - that would take care of having a record added between the copy and the delete. If you are doing this on a regular basis, you may want to consider adding an insert/update trigger on the original table to put a copy into the secondary table and then all you have to do is do a simple delete from the originating table every so often. -- ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ================================================================== ------=_Part_34273_8371204.1160775918402 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/10/06, Jim C. Nasby <jim@nasby.net> wrote:
On Mon, Oct 09, 2006 at 05:02:07PM -0700, Bricklen Anderson wrote:
> Fourat Zouari wrote:
> >Hello all,
> >Any one could suggest the best way to copy data from table to table in
> >the same db, the reason why am seeking for this is that the first table
> >is becoming very big, and old data has no reason why to stay there, so i
> >created a cloned table but without indexes and constraints (lighter) and
> >now i would like to copy 'some' data from first to second table (copied
> >data is data older than 3 months, i have a timestamp column).
> >
> >In other way, i have a table called 'hotqueues' where i store fresh
> >messages queued, once messages are treated, they stay in 'hotqueues' but
> >with a flag indicating that their arent queued for treatment..
> >so in this way, data will rest there forever, slowing down any searches
> >in that table, the solution was to copy old messages to another table
> >called 'coldqueues' that has the same structure as 'hotqueues' but
> >lighter (without constraints and indexes).
> >How to copy these data with 100% data-loose free.
> >
> >Thanks for any help you can provide.
>
> If you just want to copy the data across to the other table:
> begin;
> insert into table2 select * from table1 where <some criteria>;
> commit;
>
> if you also want to remove that same data from table1:
> begin;
> insert into table2 select * from table1 where <some criteria>;
> delete from table1 where <same criteria as above>;
> commit;

You need to be careful with this method. For what the OP wants to do it
would probably work, but not always. The problem is that in some
scenarios, <same criteria as above> won't necessarily return the same
set of rows.

Starting in 8.2 you'll be able to do something like

INSERT INTO table2 DELET FROM table1 WHERE ... RETURNING *;

The RETURNING * will return all the data that the command deleted. In
older versions, your best bet is to store the data you're moving in a
temporary table, and then use that to delete the exact rows.

You may want to lock the table before doing the copy/delete - that would take care of having a record added between the copy and the delete.

If you are doing this on a regular basis, you may want to consider adding an insert/update trigger on the original table to put a copy into the secondary table and then all you have to do is do a simple delete from the originating table every so often.

--
==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
================================================================== ------=_Part_34273_8371204.1160775918402--