In Postgres txid_current() should return biginteger but instead an integer is returned

All we need is an easy explanation of the problem, so here it is.

In Postgres 11, according to documentation, txid_current() should return biginteger but I’m getting small integer that doesn’t look like is extended by epoch counter. My txid looks like this: 1123

"The internal transaction ID type (xid) is 32 bits wide and wraps
around every 4 billion transactions. However, these functions 
export a 64-bit format that is extended with an “epoch” counter
so it will not wrap around during the life of an installation."

https://www.postgresql.org/docs/11/functions-info.html

Am I interpreting documentation incorrectly?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The internal transaction counter in PostgreSQL is 4 bytes wide, but txid_current() will return a bigint that keeps increasing even after transaction wraparound. Think of it as an “external transaction ID”.

Method 2

Postgres has an internal 32bit xid counter which is different than the value returned by txid_current().

The txid_current() returns 64bit (bigint) in which the high bits are an epoch increment which happens once per xid wrap around.

So at every wraparound the epoch kicks in and txid_current() keeps incrementing.
Thats why at a fresh launch of Postgres the returned txid looks like a regular integer and the epoch element is not apparent.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply