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 1gPD9D-0000pk-MP for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 21:01:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gPD8E-0007Bh-48 for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 21:00:26 +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 1gPD8D-0007BO-M4 for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 21:00:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPD8A-0004b2-Ry for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 21:00:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wAKL0H2L031772; Tue, 20 Nov 2018 16:00:17 -0500 From: Tom Lane To: =?UTF-8?q?Mart=C3=ADn_Fern=C3=A1ndez?= cc: "pgsql-generallists.postgresql.org" Subject: Re: Transaction Id Space, Freezing and Wraparound In-reply-to: <5bf45ed1ab1e814ed9000008@polymail.io> References: <5bf45ed1ab1e814ed9000008@polymail.io> Comments: In-reply-to =?UTF-8?q?Mart=C3=ADn_Fern=C3=A1ndez?= message dated "Tue, 20 Nov 2018 11:23:46 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <31770.1542747617.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 20 Nov 2018 16:00:17 -0500 Message-ID: <31771.1542747617@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =3D?UTF-8?q?Mart=3DC3=3DADn_Fern=3DC3=3DA1ndez?=3D w= rites: > 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 wit= h fragmentation issues ? Then I later found that the txid space is actuall= y circular and not linearly as I originally thought it was. This exposed m= e to the fact that the txid is actually exported as a 64bit value where th= e last 32bits are an epoch. My understanding is that the epoch is the comp= onent that allows the circularity of the data structure. I then started an= alyzing 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 this is the last comm= itted tx) with the xmin value of the given tuple, if xmin precedes the cut= off_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 d= atabase really old where a txid with integer value 10 is greater than a tx= id of value 1000 ?=C2=A0 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. 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