pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Rob Nikander <rob.nikander@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: procedures and transactions
Date: Tue, 19 Feb 2019 12:41:03 -0800
Message-ID: <e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.com> (raw)
In-Reply-To: <FB0D74E9-6CAE-4FDD-BAD6-8AA31169F363@gmail.com>
References: <FB0D74E9-6CAE-4FDD-BAD6-8AA31169F363@gmail.com>

On 2/19/19 12:31 PM, Rob Nikander wrote:
> Hi,
> 
> I’m trying to understand how procedures work with transactions. I tried the code below - it’s a simple procedure to print some notices and commit a transaction. If I call it from psql after a `begin`, then it gives an error. What does that error mean? Are procedures not allowed to commit/rollback if they are called within in an outer transaction?
> 
> Also, I tried putting a `start transaction` command in the procedure. I got another error: `unsupported transaction command in PL/pgSQL`. Are procedures not allowed to start transactions? Or is there another command?
> 
> thanks,
> Rob
> 
> create or replace procedure t_test(n integer)
> as $$
> begin
>      raise notice 'current isolation level: %', (select current_setting('transaction_isolation'));
>      raise notice 'current txid: %', (select txid_current());
>      raise notice '---';
>      commit;
>      raise notice 'current isolation level: %', (select current_setting('transaction_isolation'));
>      raise notice 'current txid: %', (select txid_current());
> end;
> $$ language plpgsql;
> 
> psql> begin;
> psql> call t_test(1);

Don't use the begin;

call t_test(1);
NOTICE:  current isolation level: read committed
NOTICE:  current txid: 592
NOTICE:  ---
NOTICE:  current isolation level: read committed
NOTICE:  current txid: 593
CALL


A function already starts in a transaction.

> 
> NOTICE:  current isolation level: read committed
> NOTICE:  current txid: 111490
> NOTICE:  ---
> ERROR:  invalid transaction termination
> 
> 
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (8+ messages)  latest in thread

Message-ID: <e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.com>
Permalink:  ../e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.com/
Also on:    postgresql.org/message-id/e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.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 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: adrian.klaver@aklaver.com, rob.nikander@gmail.com
  Subject: Re: procedures and transactions
  In-Reply-To: <e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.com>
  References: <FB0D74E9-6CAE-4FDD-BAD6-8AA31169F363@gmail.com> <e5d5cb87-046c-042f-aca4-4381cab2a3d3@aklaver.com>

* 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