How postgres deals with XIDs and wraparounds? I was reading the docs about the xid and it's wraparounds. But can't understand how it all works.


  Normal XIDs are compared using modulo-2^32 arithmetic. This means that for every normal XID, there are two billion XIDs that are “older” and two billion that are “newer”.


How are they compared 
According to documentation xid is 32-bit number and as far as I understand it is incremented with every transaction
Imagine the transaction IDs from 0 to 4294967296 in a circle. AS some point in that circle is your current transaction ID.

Let's say your current transaction ID is 1000000. Then the 2^31 transaction IDs after it are in the future: 1000001 to 2148483648. And the 2^31 transactions before that are the past: 2148483649 to 4294967296 and 0 to 999999 (remember we have a circle, so right after 4294967296 comes 0).

Once your current transaction ID crosses 4294967296, it becomes 0 again (to be precise, it will become 3, because the transaction IDs 0 to 2 have a special meaning).