agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Vik Fearing <vik.fearing@dalibo.com>
To: Herouth Maoz <herouth@unicell.co.il>
To: pgsql-sql@postgresql.org
Subject: Re: An archiving query - is it safe?
Date: Tue, 14 Jan 2014 13:55:23 +0100
Message-ID: <52D533BB.5030602@dalibo.com> (raw)
In-Reply-To: <671916D8-6897-4D4F-940C-3FFF61D67513@unicell.co.il>
References: <671916D8-6897-4D4F-940C-3FFF61D67513@unicell.co.il>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

On 01/14/2014 12:06 PM, Herouth Maoz wrote:
> I have regular archiving scripts which traditionally did something like this
>
> BEGIN TRANSACTION;
>   INSERT INTO a__archive
>   SELECT * FROM a
>   WHERE <condition>; -- date range condition
>
>   DELETE FROM a
>   WHERE <condition>; -- same date range condition
> COMMIT;
>
> This is "classic" SQL. I'm thinking of changing this into something like:
>
> WITH del AS ( DELETE FROM a WHERE <condition> RETURNING * )
> INSERT INTO a__archive SELECT * FROM del;
>
> As this would only access table "a" once, deleting and returning the records in the same access, which I believe will be more efficient.
>
> Is this safe to do? Is there any danger of losing data? Is it atomic?

Yes.  No.  Yes.

-- 
Vik



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



view thread (3+ messages)  latest in thread

Message-ID: <52D533BB.5030602@dalibo.com>
Permalink:  ../52D533BB.5030602@dalibo.com/
Also on:    postgresql.org/message-id/52D533BB.5030602@dalibo.com

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: vik.fearing@dalibo.com, herouth@unicell.co.il
  Subject: Re: An archiving query - is it safe?
  In-Reply-To: <52D533BB.5030602@dalibo.com>

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

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