Re: Am I in the same transaction block in complex PLPGSQL? - Mailing list pgsql-general

From Durumdara
Subject Re: Am I in the same transaction block in complex PLPGSQL?
Date
Msg-id CAEcMXhmY6Af=fcu4N9VRpwYzk20b_gg+cDXVHg7Wsm=9VKQfYA@mail.gmail.com
Whole thread Raw
In response to Re: Am I in the same transaction block in complex PLPGSQL?  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Am I in the same transaction block in complex PLPGSQL?
List pgsql-general
Dear Tom!

Ok, very-very thanks for the answer! I have the same experience with txid:

DO $$DECLARE tx bigint;
BEGIN
  select txid_current() into tx;
  raise notice ''TXID: %'', tx;
  insert into a values (26);
  select txid_current() into tx;
  raise notice ''TXID: %'', tx;
  insert into a values (27);
  select txid_current() into tx;
  raise notice ''TXID: %'', tx;
  insert into a values (28);
  select txid_current() into tx;
  raise notice ''TXID: %'', tx;
  call test_trx_value(30);
  select txid_current() into tx;
  raise notice ''TXID: %'', tx;
END$$;
 

All of them are the same.

Then back to my previous question:

> How to log to client (port native code to stored procedure) + to db tables

The main problem that the code is local now. A Win32 app, with many logs. Some of them stored into the database log tables for easily get.

But: when the transaction is same, I can't log in PGSQL 9.6 - only to client with "notices".

F. e. I have a stored procedure which has an exception handler block to catch the log and error, and pass back to the caller.
On exception this catches the error, and returns with error + log + stored them in a table record. 

But the caller also must raise an exception, for rollback and stop. In this time the exception rollbacks my table level logs too (log table rows).
Only client notices could help me what happened.

Same problem if the process started by a trigger (for example).

So if any problem happens, I will stand without any help why it happened. I can't see from db log tables, because of the posted log records will vanish on rollback!

I have access only to a database - not to the server (and it's real logs).

Hmmm....

Do you have any idea? Or we must upgrade to min. PGSQL 11 for access transaction handling and could post the logs through another transaction?

F. e.

...
call SubProc(oError, oLog);
if oError > '' then 
  rollback;   <--- on Error we must roll back
end if;
insert mylog values(oError, oLog); <--- post log everytime
if oError > '' then
   commit;   <--- on error we save the logs with commit + raise an error again
   raise Exception oError;
end if;
...

Thanks for it!

dd



Tom Lane <tgl@sss.pgh.pa.us> ezt írta (időpont: 2022. márc. 11., P, 16:01):
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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Am I in the same transaction block in complex PLPGSQL?
Next
From: Dominique Devienne
Date:
Subject: COPY TO STDOUT WITH (FORMAT CSV, HEADER), and embedded newlines