Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPDYv-0002tW-Qa for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 21:28:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gPDYu-0000z2-HA for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 21:28:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPDYu-0000yu-5O for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 21:28:00 +0000 Received: from mail-it1-x141.google.com ([2607:f8b0:4864:20::141]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPDYr-0005B8-7v for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 21:27:59 +0000 Received: by mail-it1-x141.google.com with SMTP id h193so5859411ita.5 for ; Tue, 20 Nov 2018 13:27:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:message-id:subject:date:from:cc:references:to :in-reply-to; bh=+g+3yTidpqtFky/BCAClqHM0edXqEN1E968RYD2MgnA=; b=S1WqnK3YV5v4oxjezGv7ytdksHW3rGE5ranMjD+OSrLhMFTSgFyihr6jJQoN/7ycPx k+RvlDvYB6ksFsE70AdT1TdMz9p2AT4uWvVgs7K7gywq4+ew72mT04aFqMMECVC+VkRU /cM9D9gqqVX14gF+GGbOLdNUy/lRIQxis9OEGwcDZxJp5wMvO/8tZUK8/mUgHZSX3GPT 6cSZo4jsB4ZgLxfd1oMXXNvIhnf1dUJmPtN5+bBe/TqxGkdY20CHhWpiWK4gAZAqdtB0 kZ9XGwgp8VyUXiG6WgTifvqQQJvlY6yJpYOjkYMR1p3GNDIUSmBHzkqKg0IIY3IHdZeu XQQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:message-id:subject:date:from:cc :references:to:in-reply-to; bh=+g+3yTidpqtFky/BCAClqHM0edXqEN1E968RYD2MgnA=; b=N/RBnbAiv1/+WAIq1odTkQFyupt+l8PN11Ge6weyEpRHA7b5qP3isdgGtciDsYD+m/ ePpUvmS39Quz1fkSFk3RSVcsDEjApLwwHubzWnIbHnk8FAZgjYonToxzIyrwrLjWiRWr W+DHybK/yrSPFOHK3NoYzvncXvU29HF/Trmrw6lKpj4gybq7CcwqLxMegEO/nqcOSaB4 JmaYJllE9RgeZ3HZM/t2RFZ+tpP64g+FZdDic/znn766huf/NlW+cCvJR/6oiJFs14VR n5J7yzlzbazmcRTqheGtQpoIza6KDDTf2QezW3ii1CCwgxlPbqxmcP0TmZcgZCedcUoc PvNg== X-Gm-Message-State: AA+aEWbF0CazGMHxq16BRg5knOTMYSrngVMz4kXCnXQnCqOUcv1pKHx0 B/e1kNknsKk8wp1LIHR6rFYf2W+B X-Google-Smtp-Source: AJdET5dtS5iFLgh1HvhFj5JIMsqw/ntprD9e8gN0sG31znWTWv+i270J4wCcPXCo/4/NigNUigg4vw== X-Received: by 2002:a24:3609:: with SMTP id l9mr3765019itl.142.1542749274714; Tue, 20 Nov 2018 13:27:54 -0800 (PST) Received: from localhost (238.242.184.35.bc.googleusercontent.com. [35.184.242.238]) by smtp.gmail.com with ESMTPSA id e10sm5434339ioa.65.2018.11.20.13.27.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 13:27:54 -0800 (PST) Mime-Version: 1.0 Message-ID: <5bf4794bab1e814ed9000009@polymail.io> Subject: Re: Transaction Id Space, Freezing and Wraparound Date: Tue, 20 Nov 2018 13:27:44 -0800 From: =?UTF-8?q?Mart=C3=ADn_Fern=C3=A1ndez?= Cc: "pgsql-generallists.postgresql.org" References: <31771.1542747617@sss.pgh.pa.us> X-Mailer: Polymail To: "Tom Lane" In-Reply-To: <31771.1542747617@sss.pgh.pa.us> Content-Type: multipart/alternative; boundary=e2cc6f24ae850f5d5b959892d44627fe9d4657b1b71b3852d1c2da2469de List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --e2cc6f24ae850f5d5b959892d44627fe9d4657b1b71b3852d1c2da2469de Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Mart=C3=ADn On Tue, Nov 20th, 2018 at 6:0 PM, Tom Lane wrote: >=20 >=20 >=20 > =3D?UTF-8?q?Mart=3DC3=3DADn_Fern=3DC3=3DA1ndez?=3D < fmartin91@gmail.com = > writes: > > First thing that generated a lot of noise in my head was the following, > if pg assigns contiguous numeric values for the txid, how does pg deal > with fragmentation issues ? Then I later found that the txid space is > actually circular and not linearly as I originally thought it was. This > exposed me to the fact that the txid is actually exported as a 64bit valu= e > where the last 32bits are an epoch. My understanding is that the epoch is > the component that allows the circularity of the data structure. I then > started analyzing how pg decides if a given tuple is eligible for > freezing.=C2=A0 I found out that pg will compare a cutoff_tx (I assume th= is is > the last committed tx) with the xmin value of the given tuple, if xmin > precedes the cutoff_tx the tuple is eligible (I=E2=80=99m ignoring > HEAP_XMAX_IS_MULTI and HEAP_MOVED cases). Now, the xmin of a tuple is an > 32 bit integer, so, how is the epoch part of an exported txid considered > here ? What if we had a database really old where a txid with integer > value 10 is greater than a txid of value 1000 ?=C2=A0 >=20 > Actually, XID epoch is an artifact that's bolted on for possible use by > replication or what have you. So far as the core database is concerned, > XIDs are 32 bits in a circular space, and the way that we deal with your > question is we don't let the case arise. Every old tuple must be marked > "frozen" before its XID gets to be 2 billion XIDs old; after that, we > don't particularly care just how old it is. The whole "wraparound" > business just exists to make sure that happens in time. >=20 > If the stored XIDs were 64 bits wide, we'd not have to bother with all > of this mess ... but adding another 64 bits to tuple headers would be > a painful space cost, not to mention the storage compatibility issues. >=20 > regards, tom lane >=20 >=20 >=20 >=20 Tom, Thanks for the insight!! I got confused with the comment under "Transaction IDs and Snapshots"( http= s://www.postgresql.org/docs/current/functions-info.html )=C2=A0"The interna= l transaction ID type (xid) is 32 bits wide and wraps around every 4 billio= n transactions. However, these functions export a 64-bit format that is ext= ended with an "epoch" counter so it will not wrap around during the life of= an installation. " --e2cc6f24ae850f5d5b959892d44627fe9d4657b1b71b3852d1c2da2469de Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8 3D""

Mart=C3=ADn

On Tue, Nov 20th, 2018 at 6:0 PM, Tom Lane <= ;tgl@sss.pgh.pa.us> wrote:

=3D?UTF-8?q?Mart=3DC3=3DADn_Fern=3DC3=3DA1ndez?=3D <fmartin91@gmail.com> writes:
> First thing that generated a lot of noise in my head was the follo= wing, if pg assigns contiguous numeric values for the txid, how does pg dea= l with fragmentation issues ? Then I later found that the txid space is act= ually circular and not linearly as I originally thought it was. This expose= d me to the fact that the txid is actually exported as a 64bit value where = the last 32bits are an epoch. My understanding is that the epoch is the com= ponent that allows the circularity of the data structure. I then started an= alyzing how pg decides if a given tuple is eligible for freezing.  I f= ound out that pg will compare a cutoff_tx (I assume this is the last commit= ted tx) with the xmin value of the given tuple, if xmin precedes the cutoff= _tx the tuple is eligible (I=E2=80=99m ignoring HEAP_XMAX_IS_MULTI and HEAP= _MOVED cases). Now, the xmin of a tuple is an 32 bit integer, so, how is th= e epoch part of an exported txid considered here ? What if we had a databas= e really old where a txid with integer value 10 is greater than a txid of v= alue 1000 ? 

Actually, XID epoch is an artifact that's bolted on for possible use by
replication or what have you. So far as the core database is concerned= ,
XIDs are 32 bits in a circular space, and the way that we deal with you= r
question is we don't let the case arise. Every old tuple must be marke= d
"frozen" before its XID gets to be 2 billion XIDs old; after that, we
don't particularly care just how old it is. The whole "wraparound"
business just exists to make sure that happens in time.

If the stored XIDs were 64 bits wide, we'd not have to bother with all
of this mess ... but adding another 64 bits to tuple headers would be
a painful space cost, not to mention the storage compatibility issues.

regards, tom lane

Tom,

Thanks= for the insight!!

I got confused with the comment under "Transactio= n IDs and Snapshots"(https:/= /www.postgresql.org/docs/current/functions-info.html) "The interna= l transaction ID type (xid) is 32 bits wide and wraps around every 4 billio= n transactions. However, these functions export a 64-bit format that is ext= ended with an "epoch" counter so it will not wrap around during the life of= an installation. "
--e2cc6f24ae850f5d5b959892d44627fe9d4657b1b71b3852d1c2da2469de--