Re: Basic question about structuring SQL

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Robert Inder <robert(at)interactive(dot)co(dot)uk>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Basic question about structuring SQL
Date: 2020-07-07 14:41:04
Message-ID: 265c0fb3098df1da73c9d091fd9cde5389345e27.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sebastien Arod 2020-07-07 15:01:20 Re: Is postgres able to share sorts required by common partition window functions?
Previous Message Jason Wang 2020-07-07 13:14:18 Re: Basic question about structuring SQL