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 1gPBf8-0000uh-Gk for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 19:26:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gPBd0-0006xH-IQ for pgsql-general@arkaria.postgresql.org; Tue, 20 Nov 2018 19:24:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPBd0-0006xA-3D for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 19:24:06 +0000 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gPBct-0003Lk-A2 for pgsql-general@lists.postgresql.org; Tue, 20 Nov 2018 19:24:04 +0000 Received: by mail-io1-xd2f.google.com with SMTP id n9so2232778ioh.7 for ; Tue, 20 Nov 2018 11:23:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:date:message-id:subject:from:to; bh=rev+OBRCQ09n/FGIk3xmQOn3tGneOol6r5bsAYUJJBE=; b=TJgqyreOKnmmhl5oTYOSu2lpNs7SOjtBTYUmHmqPxAeTELOFkd8TiiKTlh51OkiCiv FION2itYhD/mCI8/IJSinn5E2GObZ+CtR46WPXM1nxsjSOQaYXQiRinBnqGOqChUVp5N F80eU/8V/gKkV1ILUeF7p/u7KU6O4PWgz3uLRVgZqiS0TKEiy2qWSjGZXa3Z+xHIJi7O r94PJmdK11lB/THAGZuQGNkqMC3Ug2CZAFiEBOJ7TkIZ4Qeh7tFVOuiE5XrNXuJ8kBCr A9/rSuIBtNIc8HZaEAWlNOgiQPdYBUnsO66f0uv1dCpO+vw8j4gwP13qqFwnxa4VeDtA b+Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to; bh=rev+OBRCQ09n/FGIk3xmQOn3tGneOol6r5bsAYUJJBE=; b=IYKV5TTESKSJOAseknzW+uTuOjnNesYaGtGoM1krsMJ/+CkcnyR/yndMLyhgn5EDX8 s1nTHlCI8ZDLX6trBE3OISlIHJmWNG0P9CnGyno2O4V/uPJseFQl6dJpy2rrjLaIuj3e XULeAJJAsTkRWHlb12tkpMvkqK7iJZ6V7IPJ30YvMXNdyffUzt/brMps2dnTdBGZ33Kr L4fMK9qVR683hCYsGm0ePRVEB9HXuPJdbhKLPU21AjoX7IqozzNgoq3xKcoPEHHLg2OO f2dQxZJzPwvktob0TlDt3VobVLXXG6X4JPm40P9bxStl2Wq5NuEHenJ4p+dSOsd5Yr+n 8ADA== X-Gm-Message-State: AA+aEWbgDW9/OOekGPL0lFBKmKluJaDFDowEOk2UEiruxTqSKHDtmyty w6SiGpeWFWzyrusfmtjxj83WSyEG X-Google-Smtp-Source: AFSGD/ULDVXZRd9+ibBdvCnCX3B/0OS+rGO/Ib7wUZEloXHhdTl3/+MfVZvAAv7/LcY5Rp7zUxX4Bw== X-Received: by 2002:a6b:7a46:: with SMTP id k6mr2667802iop.60.1542741837529; Tue, 20 Nov 2018 11:23:57 -0800 (PST) Received: from localhost (166.193.193.35.bc.googleusercontent.com. [35.193.193.166]) by smtp.gmail.com with ESMTPSA id j4-v6sm13086090iop.42.2018.11.20.11.23.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 11:23:56 -0800 (PST) Mime-Version: 1.0 Date: Tue, 20 Nov 2018 11:23:46 -0800 X-Mailer: Polymail Message-ID: <5bf45ed1ab1e814ed9000008@polymail.io> Subject: Transaction Id Space, Freezing and Wraparound From: =?UTF-8?q?Mart=C3=ADn_Fern=C3=A1ndez?= To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary=6a7755d317189b24f69cd31222dcf631de4fd1243343f163319efc56db55 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --6a7755d317189b24f69cd31222dcf631de4fd1243343f163319efc56db55 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Hello everyone, second time writing to this awesome mailing list. I=E2=80=99m helping manage a postgresql 9.2.24 high volume transaction data= base and yesterday we were literally one hour away of having to deal with t= ransaction id wraparound. We were really lucky about identifying the issue = one hour before getting materialized, that gave us some time to reduce the = transaction load so that we could run VACUUM FREEZE on our hottest table. B= y running VACUUM FREEZE on our hottest table we were able to move our oldes= t txid by almost 1 billion.=C2=A0 After almost experiencing transaction id wraparound I decided to start inve= stigating deeper around how postgresql works with transactions ids and wrap= around in general.=C2=A0 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 fr= agmentation issues ? Then I later found that the txid space is actually cir= cular and not linearly as I originally thought it was. This exposed me to t= he 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 component th= at allows the circularity of the data structure. I then started analyzing h= ow 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 committed tx) w= ith the xmin value of the given tuple, if xmin precedes the cutoff_tx the t= uple is eligible (I=E2=80=99m ignoring HEAP_XMAX_IS_MULTI and HEAP_MOVED ca= ses). Now, the xmin of a tuple is an 32 bit integer, so, how is the epoch p= art 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 I=E2=80=99m probably missing something here so bare with me if my previous = explanation doesn=E2=80=99t make sense at all. As usual, thanks before hand, any insight will be appreciated.=C2=A0 PD: I based my research looking at 9.2.24 code base. Best, Mart=C3=ADn --6a7755d317189b24f69cd31222dcf631de4fd1243343f163319efc56db55 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8 3D""
Hello everyo= ne, second time writing to this awesome mailing list.

<= div>I=E2=80=99m helping manage a postgresql 9.2.24 high volume transaction = database and yesterday we were literally one hour away of having to deal wi= th transaction id wraparound. We were really lucky about identifying the is= sue one hour before getting materialized, that gave us some time to reduce = the transaction load so that we could run VACUUM FREEZE on our hottest tabl= e. By running VACUUM FREEZE on our hottest table we were able to move our o= ldest txid by almost 1 billion. 

After almost= experiencing transaction id wraparound I decided to start investigating de= eper around how postgresql works with transactions ids and wraparound in ge= neral. 

First thing that generated a lot of n= oise 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 fou= nd that the txid space is actually circular and not linearly as I originall= y thought it was. This exposed me to the fact that the txid is actually exp= orted as a 64bit value 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 eli= gible for freezing.  I found out that pg will compare a cutoff_tx (I a= ssume this 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 ignorin= g 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 he= re ? What if we had a database really old where a txid with integer value 1= 0 is greater than a txid of value 1000 ? 

I= =E2=80=99m probably missing something here so bare with me if my previous e= xplanation doesn=E2=80=99t make sense at all.

As u= sual, thanks before hand, any insight will be appreciated. 
=
PD: I based my research looking at 9.2.24 code base.
Best,
Mart=C3=ADn
--6a7755d317189b24f69cd31222dcf631de4fd1243343f163319efc56db55--