Thread: Virtual tx id

Virtual tx id

From
James Sewell
Date:
Hello Hackers!

Is it possible to get the current virtual txid from C somehow?

I've looked through the code, but can't seem to find anything other than getting a NULL when there is no (real) xid assigned. Maybe I'm missing something?

Cheers,
James

Re: Virtual tx id

From
Japin Li
Date:
On Wed, 21 Sep 2022 at 11:58, James Sewell <james.sewell@gmail.com> wrote:
> Hello Hackers!
>
> Is it possible to get the current virtual txid from C somehow?
>
The virtual txid is consisted of MyProc->backendId and MyProc->lxid.  Do you
mean a C function that returns virtual txid?

> I've looked through the code, but can't seem to find anything other than
> getting a NULL when there is no (real) xid assigned. Maybe I'm missing
> something?
>
Do you mean use SQL function to check the virtual?  IIRC, there is no such
functions.  Maybe you can use pg_export_snapshot() to get virtual txid (v10
and later), the filename of exported snapshot consists MyProc->backendId
and MyProc->lxid.


-- 
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.



Re: Virtual tx id

From
Zhang Mingli
Date:
HI,
On Sep 21, 2022, 11:59 +0800, James Sewell <james.sewell@gmail.com>, wrote:
Hello Hackers!

Is it possible to get the current virtual txid from C somehow?

I've looked through the code, but can't seem to find anything other than getting a NULL when there is no (real) xid assigned. Maybe I'm missing something?

Cheers,
James

Virtual xid is meaningful only inside a read-only transaction. 

It’s made up of MyProc->BackendId and MyProc->LocalTransactionId.

To catch it, you can begin a transaction, but don’t exec sqls that could change the db.

And gdb on process to see( must exec a read only sql to call StartTransaction, else MyProc->lxid is not assigned).

```
Begin;
// do nothing

```
gdb on it and see

```
p MyProc->lxid
p MyProc->backendId

```




Regards,
Zhang Mingli

Re: Virtual tx id

From
Julien Rouhaud
Date:
Hi,

On Wed, Sep 21, 2022 at 01:58:47PM +1000, James Sewell wrote:
> Hello Hackers!
>
> Is it possible to get the current virtual txid from C somehow?
>
> I've looked through the code, but can't seem to find anything other than
> getting a NULL when there is no (real) xid assigned. Maybe I'm missing
> something?

It should be MyProc->lxid, and prepend it with MyBackendId if you want to
compare something like pg_locks.virtualtransaction.