How to check for in-progress transactions

From: Tejasvi Kashi <mail(at)tejasvi(dot)dev>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: How to check for in-progress transactions
Date: 2023-03-15 19:48:26
Message-ID: CAJWWkarMfiCaD_+NQ2-zRtcfW1g29mhc6W6VT5Q=VUjj_uJKLQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi everyone,

I'm Tej, a grad student poking around postgres for a project.

For my use case, I'm trying to ascertain if there are any in-flight
transactions that are yet to be replicated to synchronous standbys (in a
synchronous streaming replication setting)

The first way to do this would be to check the WalSndCtl->lsn[] array to
see if the current max lsn of the system has replicated or not. This works
well when postgres is running and being actively used. However, when
postgres has just started up, WalSndCtl->lsn[] values could be 0, but there
could still be transactions waiting to replicate.

The second way to do it would be to scan ProcGlobal to check for active
xids. However, the issue is that I'm calling ProcArrayEndTransaction()
before calling SyncRepWaitForLSN() to ensure that the transaction becomes
visible to other transactions before it begins to wait in the SyncRep
queue.

So, with this change, if I scan ProcGlobal, I would not see transactions
that have been committed locally but are yet to be replicated to
synchronous standbys because ProcArrayEndTransaction() would have marked
the transaction as completed.

I've been looking at sent_lsn, write_lsn, flush_lsn etc., of the
walsender, but with no success. Considering the visibility change added
above, is there a way for me to check for transactions that have been
committed locally but are waiting for replication?

I would appreciate it if someone could point me in the right direction!

Sincerely,

Tej Kashi
MMath CS, University of Waterloo
Waterloo, ON, CA

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mikhail Gribkov 2023-03-15 20:23:12 Re: On login trigger: take three
Previous Message Daniel Gustafsson 2023-03-15 19:15:36 Re: uuid-ossp source or binaries for Windows