pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jwieck@debis.com
To: les@Mcs.Net
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] idiom to move records?
Date: Mon, 26 Oct 1998 11:02:22 +0100 (MET)
Message-ID: <m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de> (raw)
In-Reply-To: <199810260531.XAA14075@Mars.mcs.net>

>
> 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) #





view thread (3+ messages)

Message-ID: <m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de>
Permalink:  ../m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de/
Also on:    postgresql.org/message-id/m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jwieck@debis.com, les@Mcs.Net
  Subject: Re: [SQL] idiom to move records?
  In-Reply-To: <m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de>
  References: <199810260531.XAA14075@Mars.mcs.net> <m0zXjTi-000EBPC@orion.SAPserv.Hamburg.dsh.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox