Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ourhm-0007Wm-Iv for pgsql-admin@arkaria.postgresql.org; Tue, 15 Nov 2022 08:54:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ourhl-0004jY-8n for pgsql-admin@arkaria.postgresql.org; Tue, 15 Nov 2022 08:54:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ourhk-0004jN-Rc for pgsql-admin@lists.postgresql.org; Tue, 15 Nov 2022 08:54:04 +0000 Received: from mail.pscs.co.uk ([178.159.9.185]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ourhh-0004Go-8W for pgsql-admin@lists.postgresql.org; Tue, 15 Nov 2022 08:54:03 +0000 Authentication-Results: mail.pscs.co.uk; spf=none; auth=pass (cram-md5) smtp.auth=pscs Received: from lmail.pscs.co.uk ([192.168.150.1]) by mail.pscs.co.uk ([192.168.150.185] running VPOP3) with ESMTPSA (TLSv1.3 TLS_AES_256_GCM_SHA384) for ; Tue, 15 Nov 2022 08:53:57 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pscs.co.uk; q=dns/txt; s=lmail; h=Content-Type:Message-ID:Date:MIME-Version:Subject:To:References:From :In-Reply-To:Cc:Content-Transfer-Encoding:Reply-to:Sender; t=1668502094; x=1669106894; bh=qxPCZb1dAhiRzO7A1lUXbSiIx5AkGs2JK6SJTyer1W8=; b=XwQbZ4JFdSbTUOF+cAWlFPpa98+wHSc5x36IvBj1UiHAWS5x61XuyFTAeweEIQTxcLUPnSJz 91FJNbRr28VGfT0ASdTCVIheqqasLZIZA0D+Ll/vKOmLyA63fTnTpQbZDV39/b+urMz/H5EueH Z98h09ipkr6DRwbTXGjM4A+bU= Authentication-Results: lmail.pscs.co.uk; spf=none; auth=pass (cram-md5) smtp.auth=paul Received: from [192.168.57.78] ([82.68.48.206] (82-68-48-206.dsl.in-addr.zen.co.uk)) by lmail.pscs.co.uk ([192.168.150.70] running VPOP3) with ESMTPSA (TLSv1.3 TLS_AES_256_GCM_SHA384) for ; Tue, 15 Nov 2022 08:48:13 -0000 Content-Type: multipart/alternative; boundary="------------O0nEeqRCYtGdJW1cEkPGkGXK" Message-ID: Date: Tue, 15 Nov 2022 08:48:09 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Sequence last value is different among primary and secondary To: pgsql-admin@lists.postgresql.org References: From: Paul Smith In-Reply-To: X-Authenticated-Sender: paul X-Server: VPOP3 Enterprise V8.4 - Registered X-Organisation: Paul Smith Computer Services X-VPOP3Tester: 12 345 X-Authenticated-Sender: pscs List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------O0nEeqRCYtGdJW1cEkPGkGXK Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 15/11/2022 08:24, Kenny Bachman wrote: > Hello Team, > > I got a weird situation about the sequence value in PG 14.5 (rhel > 8.6). The sequence's last value is different between primary and > secondary nodes. > > This sequence output belongs to the PRIMARY node: > > SELECT * FROM "ACCOUNT_ITEM_SEQ" ; >  last_value | log_cnt | is_called > ------------+---------+----------- >      105485 |      31 | t > > This output belongs to the SECONDARY node: > > SELECT * FROM "ACCOUNT_ITEM_SEQ" ; >  last_value | log_cnt | is_called > ------------+---------+----------- >      105516 |       0 | t > > *There is no replication gap or lag between the nodes.* > What is the reason behind this difference? I believe this is to do with the way PostgreSQL does sequences. It doesn't update the WAL every time a sequence increments, just every 32 times to avoid writing lots of unnecessary entries given that the sequence generally updates in a predictable manner. The 'log_cnt' keeps track of how many increments are to go before the next WAL update. I guess the secondary is just updated with the next 'step', so that if there's a failover, the sequence number is at a 'safe' position without causing duplicates. (note that the primary 'last_value + log_cnt' = secondary 'last_value') Eg, see https://stackoverflow.com/questions/66456952/what-does-log-cnt-mean-in-the-postgres-sequence Paul -- Paul Smith Computer Services Tel: 01484 855800 Vat No: GB 685 6987 53 Sign up for news & updates at http://www.pscs.co.uk/go/subscribe --------------O0nEeqRCYtGdJW1cEkPGkGXK Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 15/11/2022 08:24, Kenny Bachman wrote:
Hello Team,

I got a weird situation about the sequence value in PG 14.5 (rhel 8.6). The sequence's last value is different between primary and secondary nodes.

This sequence output belongs to the PRIMARY node:

SELECT * FROM "ACCOUNT_ITEM_SEQ" ;
 last_value | log_cnt | is_called
------------+---------+-----------
     105485 |      31 | t

This output belongs to the SECONDARY node:

SELECT * FROM "ACCOUNT_ITEM_SEQ" ;
 last_value | log_cnt | is_called
------------+---------+-----------
     105516 |       0 | t

There is no replication gap or lag between the nodes.
What is the reason behind this difference?


I believe this is to do with the way PostgreSQL does sequences. It doesn't update the WAL every time a sequence increments, just every 32 times to avoid writing lots of unnecessary entries given that the sequence generally updates in a predictable manner. The 'log_cnt' keeps track of how many increments are to go before the next WAL update.

I guess the secondary is just updated with the next 'step', so that if there's a failover, the sequence number is at a 'safe' position without causing duplicates. (note that the primary 'last_value + log_cnt' = secondary 'last_value')

Eg, see https://stackoverflow.com/questions/66456952/what-does-log-cnt-mean-in-the-postgres-sequence

Paul


--

Paul Smith Computer Services
Tel: 01484 855800
Vat No: GB 685 6987 53

--------------O0nEeqRCYtGdJW1cEkPGkGXK--