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 1jcAqA-0001Lh-DN for pgsql-admin@arkaria.postgresql.org; Fri, 22 May 2020 16:48:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jcAq9-0004XI-AI for pgsql-admin@arkaria.postgresql.org; Fri, 22 May 2020 16:48:09 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jcAq9-0004XA-18 for pgsql-admin@lists.postgresql.org; Fri, 22 May 2020 16:48:09 +0000 Received: from impout006aa.msg.chrl.nc.charter.net ([47.43.20.30] helo=impout006.msg.chrl.nc.charter.net) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jcAq6-00087v-Gy for pgsql-admin@lists.postgresql.org; Fri, 22 May 2020 16:48:08 +0000 Received: from cerberus.crazybean.net ([24.151.69.73]) by cmsmtp with ESMTP id cAq3jGyNO6k6FcAq3jn8VJ; Fri, 22 May 2020 16:48:03 +0000 Authentication-Results: crazybean.net; none X-Authority-Analysis: v=2.3 cv=G8io7Os5 c=1 sm=1 tr=0 a=YXTuGtgWN9YX+aIhe8tYuA==:117 a=YXTuGtgWN9YX+aIhe8tYuA==:17 a=xqWC_Br6kY4A:10 a=sTwFKg_x9MkA:10 a=pGLkceISAAAA:8 a=UXENWu1Bc-PoB0gt-EYA:9 a=lzCv3k7usPHjJZIS:21 a=JMQvGGgJUW7mzAxI:21 a=QEXdDO2ut3YA:10 a=4aBxpxBoe6JJbY-rdMsA:9 a=f23ILPDDSfLMuUz3:21 a=iBKrJ_a5fEP6dUnO:21 a=m2m9aZhYCjg3xuU_:21 a=_W_S_7VecoQA:10 Received: from [IPv6:2001:470:e117:4:fc2f:4b79:636b:9311] ( [2001:470:e117:4:fc2f:4b79:636b:9311]) by cerberus.crazybean.net (OpenSMTPD) with ESMTPSA id b1d27b2c (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Fri, 22 May 2020 12:48:02 -0400 (EDT) From: Rui DeSousa Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_CB64E4D2-ABC3-46A3-8771-44B9B6D140B2" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Subject: Re: Lag clarification with Sync Replication Date: Fri, 22 May 2020 12:48:01 -0400 In-Reply-To: Cc: Pgsql-admin To: Raj kumar References: X-Mailer: Apple Mail (2.3608.80.23.2.2) X-CMAE-Envelope: MS4wfCDfILm6GVuM5g+ikc+BGXf2CA/kxhTGZ/wLDqk/JlVejMKSJztWJ1CJio6y9S0MP62CvrEgAk1//vCx9UcRrKXHIRDtNcsuzpIkpEFZYS5RhxB9pesg ktpjUJJHK7W+InVoK/7DkNtG5zKCepD3XU9qW5KMkBJrB8Cl29D0YGQmh/6Ex8fc0n4RbNNlqvs2HVQNbFVTU5jy0Y9vk/HvqoSjLQZ1unOSzK6q23gjPPUT List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_CB64E4D2-ABC3-46A3-8771-44B9B6D140B2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 22, 2020, at 12:36 AM, Raj kumar = wrote: >=20 > . But, ideally for sync replication, lag should be always 0 right. Incorrect. Synchronous replication means that a commit will not return = until it has been safely written to disk on the primary and the replica. = That means the transaction is written to WAL file on both primary and = replica. On the primary, the transaction is also visible to = transactions with a later xmin. On the replica, the transaction has = been recorded in the WAL; it still needs to get applied to the database = for it to become visible to read transactions. Also, changes could occur faster on the primary where the replica could = be lagging behind =E2=80=94 either by lagging in receiving the updates = or writing the information to the WAL. What will happen is when the = commit is iussed; the session will hang until replica catches up and = writes the all the data need safely to the WAL. I would look at pg_stat_replication view on the primary/upstream = provider. Here=E2=80=99s a query that make it easier. Sent_lag =E2=80=94 data that still needs to be sent to the replica Flush_lag =E2=80=94data that still has not been fsync=E2=80=99d to WAL = files yet Replay_lag =E2=80=94 data that still need to be applied to the database = so that it can be queried select pg_stat_replication.client_addr , pg_stat_replication.application_name , pg_stat_replication.sync_priority , pg_stat_replication.sync_state , pg_stat_replication.state , case pg_is_in_recovery() when true then = pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), sent_lsn)) else pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = sent_lsn)) end as sent_lag , case pg_is_in_recovery() when true then = pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), flush_lsn)) else pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = flush_lsn))=20 end as flush_lag , case pg_is_in_recovery() when true then = pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), replay_lsn))=20= else pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = replay_lsn)) end as replay_lag from pg_stat_replication ; --Apple-Mail=_CB64E4D2-ABC3-46A3-8771-44B9B6D140B2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On May 22, 2020, at 12:36 AM, Raj kumar <rajkumar820999@gmail.com> wrote:

. But, ideally for sync replication, lag should be always 0 = right.

Incorrect.  Synchronous replication means that a commit = will not return until it has been safely written to disk on the primary = and the replica.  That means the transaction is written to WAL file = on both primary and replica.  On the primary, the transaction is = also visible to transactions with a later xmin.  On the replica, = the transaction has been recorded in the WAL; it still needs to get = applied to the database for it to become visible to read = transactions.

Also, changes could occur faster on the primary where the = replica could be lagging behind =E2=80=94 either by lagging in receiving = the updates or writing the information to the WAL.  What will = happen is when the commit is iussed; the session will hang until replica = catches up and writes the all the data need safely to the WAL.

I would look at = pg_stat_replication view on the primary/upstream provider. =  Here=E2=80=99s a query that make it easier.
Sent_lag =E2=80=94 data that still = needs to be sent to the replica
Flush_lag =E2=80=94da= ta that still has not been fsync=E2=80=99d to WAL files yet
Replay_lag =E2=80=94 data that still need to be applied to = the database so that it can be queried

select = pg_stat_replication.client_addr
  , = pg_stat_replication.application_name
  , = pg_stat_replication.sync_priority
  , = pg_stat_replication.sync_state
  , = pg_stat_replication.state
  , case = pg_is_in_recovery()
      when true = then pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), = sent_lsn))
      else = pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = sent_lsn))
    end as sent_lag
  , case pg_is_in_recovery()
  =     when true then = pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), = flush_lsn))
      else = pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = flush_lsn)) 
    end as = flush_lag
  , case = pg_is_in_recovery()
      when true = then pg_size_pretty(pg_wal_lsn_diff(pg_last_wal_receive_lsn(), = replay_lsn)) 
      else = pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), = replay_lsn))
    end as = replay_lag
from pg_stat_replication
;

= --Apple-Mail=_CB64E4D2-ABC3-46A3-8771-44B9B6D140B2--