pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Robert Inder <robert@interactive.co.uk>
To: pgsql-general@lists.postgresql.org
Subject: Re: Basic question about structuring SQL
Date: Tue, 07 Jul 2020 16:41:04 +0200
Message-ID: <265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at> (raw)
In-Reply-To: <CAKqjJm8Da8V-T0+kpg8jCqWHejKPiMbqqOKgCeSpeMkPWjV7cw@mail.gmail.com>
References: <CAKqjJm8Da8V-T0+kpg8jCqWHejKPiMbqqOKgCeSpeMkPWjV7cw@mail.gmail.com>

On Tue, 2020-07-07 at 12:40 +0100, Robert Inder wrote:
> NOW, I want to do the same for a BIGGER task.
> BUT I realise that if I create bigger-task.sql like this...
>    BEGIN;
>       <<preparatory  operations>>
>    \i do-something-useful.sql
>       <<tidy up code>>
>    COMMIT;
> ...the COMMIT inside "do-something-useful.sql" closes the transaction started in "bigger-task.sql"
> So I can get some things committed even if other things (in tidy-up) fail.
> 
> So how SHOULD I tackle this? 
> PostgreSQL does not do nested transactions (right?)

You can use subtransactions.

In the inner scripts, don't use BEGIN, but SAVEPOINT <name>.
Instead of ROLLBACK in the inner script, use ROLLBACK TO SAVEPOINT <name>.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (4+ messages)  latest in thread

Message-ID: <265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at>
Permalink:  ../265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at/
Also on:    postgresql.org/message-id/265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at

 · 

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-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, robert@interactive.co.uk
  Subject: Re: Basic question about structuring SQL
  In-Reply-To: <265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at>
  References: <CAKqjJm8Da8V-T0+kpg8jCqWHejKPiMbqqOKgCeSpeMkPWjV7cw@mail.gmail.com> <265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at>

* 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