pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Durumdara <durumdara@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Am I in the same transaction block in complex PLPGSQL?
Date: Fri, 11 Mar 2022 10:01:12 -0500
Message-ID: <830175.1647010872@sss.pgh.pa.us> (raw)
In-Reply-To: <CAEcMXhkYe02Fpzfgo4SFMjNUZzKCSbC1ses9drDvqpwNxs0f=A@mail.gmail.com>
References: <CAEcMXhkYe02Fpzfgo4SFMjNUZzKCSbC1ses9drDvqpwNxs0f=A@mail.gmail.com>

Durumdara <durumdara@gmail.com> writes:
> In autocommit mode, when I start a simple update or select, it is one
> transaction, so if something fails, the whole modification is rolled back
> (there is no "half update", or "only first record updated").
> What will happen with complex statements, like PLPGSQL stored procedure, or
> trigger?

It's one transaction unless you take steps to make it something else.
In plpgsql, you can make "sub transactions" with BEGIN/EXCEPTION blocks
(an exception rolls back only side-effects that happened since BEGIN).
Or, if it's a procedure not a function, you can explicitly COMMIT
what's been done so far, and then your next action starts a new
transaction.  But without such measures, all actions executed by
a function are part of the same transaction as the calling statement.

			regards, tom lane





view thread (4+ messages)  latest in thread

Message-ID: <830175.1647010872@sss.pgh.pa.us>
Permalink:  ../830175.1647010872@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/830175.1647010872@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, durumdara@gmail.com
  Subject: Re: Am I in the same transaction block in complex PLPGSQL?
  In-Reply-To: <830175.1647010872@sss.pgh.pa.us>
  References: <CAEcMXhkYe02Fpzfgo4SFMjNUZzKCSbC1ses9drDvqpwNxs0f=A@mail.gmail.com> <830175.1647010872@sss.pgh.pa.us>

* 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