From: Paul Smith <paul@pscs.co.uk>
To: pgsql-admin@lists.postgresql.org
Subject: Re: Sequence last value is different among primary and secondary
Date: Tue, 15 Nov 2022 08:48:09 +0000
Message-ID: <d864578b-b72f-2ab5-d1c8-d11703d91adc@pscs.co.uk> (raw)
In-Reply-To: <CAC0w7LJszznfQ7FmgPzFQBd=4t5y23cd0T1iU_D4g0+PG0X08w@mail.gmail.com>
References: <CAC0w7LJszznfQ7FmgPzFQBd=4t5y23cd0T1iU_D4g0+PG0X08w@mail.gmail.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: paul@pscs.co.uk, pgsql-admin@lists.postgresql.org
Subject: Re: Sequence last value is different among primary and secondary
In-Reply-To: <d864578b-b72f-2ab5-d1c8-d11703d91adc@pscs.co.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox