Thread: equivalent of @@TRANCOUNT PostgreSQL

equivalent of @@TRANCOUNT PostgreSQL

From
Date:

Hello,

 

I have a procedure where they have used @@TRANCOUNT , is there an equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)

 

Saygılarımla.

---

Mustafa Pekgöz
Co-Founder, Principal Database Consultant
Tel : +90 534 429 76 83

E-mail : mustafa.pekgoz@forenda.com.tr
Website: www.forenda.com.tr
Adres : Barbaros Mah. Begonya Sok. Nidakule No: 1/2 Batı Ataşehir / İSTANBUL

                   

 

Attachment

Re: equivalent of @@TRANCOUNT PostgreSQL

From
Julien Rouhaud
Date:
On Sat, May 29, 2021 at 9:07 PM <mustafa.pekgoz@forenda.com.tr> wrote:

Hello,

 

I have a procedure where they have used @@TRANCOUNT , is there an equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)


What does @@TRANCOUNT do?

RE: equivalent of @@TRANCOUNT PostgreSQL

From
Date:

@@TRANCOUNT: Transaction count.

How many transactions are open at the moment.

I’m not seeing anything that looks appropriate. As far as I can see it might be possible to do something using the Statistics Collector, see https://www.postgresql.org/docs/13/monitoring-stats.html

Hope someone knowledgeable on PostgreSQL will have an answer.

I did find this from 2006. https://www.postgresql.org/message-id/eg5op3$ria$1@sea.gmane.org

HTH.

 

From: Julien Rouhaud [mailto:rjuju123@gmail.com]
Sent: 29 May 2021 14:26
To: mustafa.pekgoz@forenda.com.tr
Cc: Pgsql-admin
Subject: Re: equivalent of @@TRANCOUNT PostgreSQL

 

On Sat, May 29, 2021 at 9:07 PM <mustafa.pekgoz@forenda.com.tr> wrote:

Hello,

 

I have a procedure where they have used @@TRANCOUNT , is there an equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)

 

What does @@TRANCOUNT do?

Re: equivalent of @@TRANCOUNT PostgreSQL

From
"David G. Johnston"
Date:

On Saturday, May 29, 2021, <dave@davebolt.co.uk> wrote:

@@TRANCOUNT: Transaction count.

How many transactions are open at the moment.


pg_stat_activity could be used for this.  One transaction per process actively executing a query.

David J. 

Re: equivalent of @@TRANCOUNT PostgreSQL

From
MichaelDBA
Date:
I don't think so, it's not current active transactions, but number of active transactions on the CURRENT CONNECTION.  No way you get that from pg_stat_activity.  So if your proc had a bunch of embedded BEGIN/END blocks, it would be that count.

David G. Johnston wrote on 5/29/2021 10:44 AM:
One transaction per process actively executing a query.

Re: equivalent of @@TRANCOUNT PostgreSQL

From
Tom Lane
Date:
<dave@davebolt.co.uk> writes:
> @@TRANCOUNT: Transaction count.
> How many transactions are open at the moment.
> I’m not seeing anything that looks appropriate. As far as I can see it might be possible to do something using the
StatisticsCollector, see https://www.postgresql.org/docs/13/monitoring-stats.html 

Yeah, something like this should do for that:

select count(*) from pg_stat_activity where state is not null;

            regards, tom lane



Re: equivalent of @@TRANCOUNT PostgreSQL

From
Tom Lane
Date:
MichaelDBA <MichaelDBA@sqlexec.com> writes:
> I don't think so, it's not current active transactions, but number of 
> active transactions on the CURRENT CONNECTION.

Isn't that a constant 1?  We don't do multiple active transactions
in one session.

            regards, tom lane



Re: equivalent of @@TRANCOUNT PostgreSQL

From
Bruce Momjian
Date:
On Sat, May 29, 2021 at 03:34:41PM +0100, dave@davebolt.co.uk wrote:
> @@TRANCOUNT: Transaction count.
> 
> How many transactions are open at the moment.
> 
> I’m not seeing anything that looks appropriate. As far as I can see it might be
> possible to do something using the Statistics Collector, see https://
> www.postgresql.org/docs/13/monitoring-stats.html
> 
> Hope someone knowledgeable on PostgreSQL will have an answer.
> 
> I did find this from 2006. https://www.postgresql.org/message-id/
> eg5op3$ria$1@sea.gmane.org

Uh, the only way I know to do that is to query pg_stat_activity like
this:

    SELECT *
    FROM pg_stat_activity
    WHERE state != 'idle' AND backend_type = 'client backend';

However, the purpose of why you are using these values probably is not
going to translate to Postgres easily.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.




Re: equivalent of @@TRANCOUNT PostgreSQL

From
Thomas Kellerer
Date:
mustafa.pekgoz@forenda.com.tr schrieb am 29.05.2021 um 15:07:
> I have a procedure where they have used @@TRANCOUNT , is there an
> equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)
The equivalent in Postgres is

    SELECT 1





Re: equivalent of @@TRANCOUNT PostgreSQL

From
MichaelDBA
Date:
This is sooooo funny how this thread has been progressing.  The purpose 
for TRANCOUNT is for applications to track the state of transactions for 
a given connection to the database. TRANCOUNT keeps track of open 
transactions and is intented for usage in MSSQL server since 
transactions  are handled differently there.  In PG we don't have this 
"stacked" transactions scenario unit PG 11 where we started to support 
procedures that can call other procedures and hence we can have a 
stacked list of open transactions.  But still, TRANCOUNT in PG is 
basically useless, so when you convert from MS SQL to PG, just throw the 
damn thing away!

Regards,
Michael Vitale



Thomas Kellerer wrote on 5/29/2021 2:15 PM:
> mustafa.pekgoz@forenda.com.tr schrieb am 29.05.2021 um 15:07:
>> I have a procedure where they have used @@TRANCOUNT , is there an
>> equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)
> The equivalent in Postgres is
>
>    SELECT 1
>
>
>
>




Re: equivalent of @@TRANCOUNT PostgreSQL

From
Thomas Carroll
Date:
This may or may not be helpful: txid_current_if_assigned().

Sometimes @@trancount is used to see if the current context is within a transaction - without regard to depth.  If that is your case maybe this can help.

SELECT * FROM txid_current_if_assigned();

 txid_current_if_assigned
--------------------------
                     NULL
(1 row)

BEGIN;

BEGIN

CREATE TABLE delete_me (x INTEGER NOT NULL);

CREATE TABLE

SELECT * FROM txid_current_if_assigned();

 txid_current_if_assigned
--------------------------
                   656442
(1 row)

ROLLBACK;

ROLLBACK

SELECT * FROM txid_current_if_assigned();

 txid_current_if_assigned
--------------------------
                     NULL
(1 row)

Note that I had to actually make a modification to get the transaction ID to be assigned.  With MSSQL, @@trancount gets set to 1 right away when you open a transaction - without you having to modify anything.

Tom
On Saturday, May 29, 2021, 6:28:47 PM EDT, MichaelDBA <michaeldba@sqlexec.com> wrote:


This is sooooo funny how this thread has been progressing.  The purpose
for TRANCOUNT is for applications to track the state of transactions for
a given connection to the database. TRANCOUNT keeps track of open
transactions and is intented for usage in MSSQL server since
transactions  are handled differently there.  In PG we don't have this
"stacked" transactions scenario unit PG 11 where we started to support
procedures that can call other procedures and hence we can have a
stacked list of open transactions.  But still, TRANCOUNT in PG is
basically useless, so when you convert from MS SQL to PG, just throw the
damn thing away!

Regards,
Michael Vitale



Thomas Kellerer wrote on 5/29/2021 2:15 PM:
> mustafa.pekgoz@forenda.com.tr schrieb am 29.05.2021 um 15:07:
>> I have a procedure where they have used @@TRANCOUNT , is there an
>> equivalent of @@TRANCOUNT in PostgreSQL? (Except Savepoint)
> The equivalent in Postgres is
>
>    SELECT 1
>
>
>
>