Re: [SQL] idiom to move records? - Mailing list pgsql-sql

From jwieck@debis.com (Jan Wieck)
Subject Re: [SQL] idiom to move records?
Date
Msg-id m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de
Whole thread Raw
In response to idiom to move records?  (Leslie Mikesell <les@Mcs.Net>)
Responses Re: [SQL] idiom to mv recs => [NEW] bleeding lock?
List pgsql-sql
>
> Is there a handy way to move a set of records to a different
> table in sql?  I'd like to do this based on a WHERE clause
> and have it work atomically such that it will not lose new
> records added between the step that copies to the other table
> and deleting the copied records.

    Use a transaction and lock the source table first.

        BEGIN TRANSACTION;
        LOCK TABLE src_table;
        INSERT INTO dest_table SELECT * FROM src_table
               WHERE ...;
        DELETE FROM src_table WHERE ...;
        COMMIT TRANSACTION;

    No  other  backend  can add, modify or remove rows to/in/from
    src_table while you have a lock on it.


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#======================================== jwieck@debis.com (Jan Wieck) #

pgsql-sql by date:

Previous
From: Herouth Maoz
Date:
Subject: Re: [SQL] idiom to move records?
Next
From: pierre@desertmoon.com
Date:
Subject: Alternate disks for Indexes