Обсуждение: XID comparations

Поиск
Список
Период
Сортировка

XID comparations

От
"Carlos H. Reimer"
Дата:
Hi,

I would like to understand better the logic to determine when a xid is older
than another one.

As I could understand, the XID is always incremented, never reset. If it is
true, then we can have rows with cmin ranging
from 1 to 4.294.967.295 (2^32-1).

When xid overflows (32 bits) the next one will be 3 (1 and 2 are reserved).

In this case, we could have have lines with cmin 4.294.967.295 and lines
with cmin 3. How are they compared to determine that
rows with cmin 3 are newer than rows with cmin 4.294.967.295?

Thanks in advance,

Reimer


Re: XID comparations

От
Tom Lane
Дата:
"Carlos H. Reimer" <carlosreimer@terra.com.br> writes:
> I would like to understand better the logic to determine when a xid is older
> than another one.

It's circular mod 2^32, with a special case for FrozenXID.  It's a
mistake to imagine that XIDs are unsigned ints, really --- the
comparison doesn't work that way.  For an XID of say 1billion, XIDs from
1billion to 3billion are "after", the rest "before".  So once a row
is created, it has to be deleted or frozen within 2 billion
transactions, else its XID wraps around and appears to be "in the
future" rather than "in the past" compared to current XIDs.

            regards, tom lane

Re: XID comparations

От
Martijn van Oosterhout
Дата:
On Tue, Jun 13, 2006 at 12:10:26PM -0300, Carlos H. Reimer wrote:
> When xid overflows (32 bits) the next one will be 3 (1 and 2 are reserved).
>
> In this case, we could have have lines with cmin 4.294.967.295 and lines
> with cmin 3. How are they compared to determine that
> rows with cmin 3 are newer than rows with cmin 4.294.967.295?

The same way you handle any circular numbering system, compare the
difference. i.e.

if( (xmin1-xmin2) mod (2^32) < 2^31 )
{
  xmin1 is newer than xmin2
}

In the example you give:

(3 - 4.294.967.295) mod (2^32)
= -4294967292 mod (2^32)
= 4

Hence XID 3 is newer compared to XID 4.294.967.295.

Note in such a circular system, it is possible for A < B, B < C and C <
A to all be simultaneously true. However, we always know where the
current transaction is, so everything is compared to that.

I hope this clears it up for you.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Вложения

RES: XID comparations

От
"Carlos H. Reimer"
Дата:
Thanks,

In my first question I would like to use xmin instead of cmin, even so I
could understand the logic.

Then for each XID you have 2 bilions XIDs that are considered lower than and
the other 2 bi higher than.

About row visibility: are all the rows with xmin higher than my XID be
considered in the future and not visible to my XID?

Thanks in advance!

Reimer