LCOV - differential code coverage report
Current view: top level - src/backend/replication - syncrep.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC EUB ECB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 77.5 % 316 245 3 68 10 235 3 11
Current Date: 2026-03-14 14:10:32 -0400 Functions: 84.2 % 19 16 1 2 5 11
Baseline: lcov-20260315-024220-baseline Branches: 67.4 % 230 155 6 69 1 12 142 2 2 12 6
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 83.9 % 31 26 3 2 10 16
(360..) days: 76.8 % 285 219 66 219
Function coverage date bins:
(360..) days: 84.2 % 19 16 1 2 5 11
Branch coverage date bins:
(30,360] days: 71.4 % 42 30 6 6 12 18
(360..) days: 65.1 % 192 125 63 1 124 2 2

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * syncrep.c
                                  4                 :                :  *
                                  5                 :                :  * Synchronous replication is new as of PostgreSQL 9.1.
                                  6                 :                :  *
                                  7                 :                :  * If requested, transaction commits wait until their commit LSN are
                                  8                 :                :  * acknowledged by the synchronous standbys.
                                  9                 :                :  *
                                 10                 :                :  * This module contains the code for waiting and release of backends.
                                 11                 :                :  * All code in this module executes on the primary. The core streaming
                                 12                 :                :  * replication transport remains within WALreceiver/WALsender modules.
                                 13                 :                :  *
                                 14                 :                :  * The essence of this design is that it isolates all logic about
                                 15                 :                :  * waiting/releasing onto the primary. The primary defines which standbys
                                 16                 :                :  * it wishes to wait for. The standbys are completely unaware of the
                                 17                 :                :  * durability requirements of transactions on the primary, reducing the
                                 18                 :                :  * complexity of the code and streamlining both standby operations and
                                 19                 :                :  * network bandwidth because there is no requirement to ship
                                 20                 :                :  * per-transaction state information.
                                 21                 :                :  *
                                 22                 :                :  * Replication is either synchronous or not synchronous (async). If it is
                                 23                 :                :  * async, we just fastpath out of here. If it is sync, then we wait for
                                 24                 :                :  * the write, flush or apply location on the standby before releasing
                                 25                 :                :  * the waiting backend. Further complexity in that interaction is
                                 26                 :                :  * expected in later releases.
                                 27                 :                :  *
                                 28                 :                :  * The best performing way to manage the waiting backends is to have a
                                 29                 :                :  * single ordered queue of waiting backends, so that we can avoid
                                 30                 :                :  * searching the through all waiters each time we receive a reply.
                                 31                 :                :  *
                                 32                 :                :  * In 9.5 or before only a single standby could be considered as
                                 33                 :                :  * synchronous. In 9.6 we support a priority-based multiple synchronous
                                 34                 :                :  * standbys. In 10.0 a quorum-based multiple synchronous standbys is also
                                 35                 :                :  * supported. The number of synchronous standbys that transactions
                                 36                 :                :  * must wait for replies from is specified in synchronous_standby_names.
                                 37                 :                :  * This parameter also specifies a list of standby names and the method
                                 38                 :                :  * (FIRST and ANY) to choose synchronous standbys from the listed ones.
                                 39                 :                :  *
                                 40                 :                :  * The method FIRST specifies a priority-based synchronous replication
                                 41                 :                :  * and makes transaction commits wait until their WAL records are
                                 42                 :                :  * replicated to the requested number of synchronous standbys chosen based
                                 43                 :                :  * on their priorities. The standbys whose names appear earlier in the list
                                 44                 :                :  * are given higher priority and will be considered as synchronous.
                                 45                 :                :  * Other standby servers appearing later in this list represent potential
                                 46                 :                :  * synchronous standbys. If any of the current synchronous standbys
                                 47                 :                :  * disconnects for whatever reason, it will be replaced immediately with
                                 48                 :                :  * the next-highest-priority standby.
                                 49                 :                :  *
                                 50                 :                :  * The method ANY specifies a quorum-based synchronous replication
                                 51                 :                :  * and makes transaction commits wait until their WAL records are
                                 52                 :                :  * replicated to at least the requested number of synchronous standbys
                                 53                 :                :  * in the list. All the standbys appearing in the list are considered as
                                 54                 :                :  * candidates for quorum synchronous standbys.
                                 55                 :                :  *
                                 56                 :                :  * If neither FIRST nor ANY is specified, FIRST is used as the method.
                                 57                 :                :  * This is for backward compatibility with 9.6 or before where only a
                                 58                 :                :  * priority-based sync replication was supported.
                                 59                 :                :  *
                                 60                 :                :  * Before the standbys chosen from synchronous_standby_names can
                                 61                 :                :  * become the synchronous standbys they must have caught up with
                                 62                 :                :  * the primary; that may take some time. Once caught up,
                                 63                 :                :  * the standbys which are considered as synchronous at that moment
                                 64                 :                :  * will release waiters from the queue.
                                 65                 :                :  *
                                 66                 :                :  * Portions Copyright (c) 2010-2026, PostgreSQL Global Development Group
                                 67                 :                :  *
                                 68                 :                :  * IDENTIFICATION
                                 69                 :                :  *    src/backend/replication/syncrep.c
                                 70                 :                :  *
                                 71                 :                :  *-------------------------------------------------------------------------
                                 72                 :                :  */
                                 73                 :                : #include "postgres.h"
                                 74                 :                : 
                                 75                 :                : #include <unistd.h>
                                 76                 :                : 
                                 77                 :                : #include "access/xact.h"
                                 78                 :                : #include "common/int.h"
                                 79                 :                : #include "miscadmin.h"
                                 80                 :                : #include "pgstat.h"
                                 81                 :                : #include "replication/syncrep.h"
                                 82                 :                : #include "replication/walsender.h"
                                 83                 :                : #include "replication/walsender_private.h"
                                 84                 :                : #include "storage/proc.h"
                                 85                 :                : #include "tcop/tcopprot.h"
                                 86                 :                : #include "utils/guc_hooks.h"
                                 87                 :                : #include "utils/ps_status.h"
                                 88                 :                : #include "utils/wait_event.h"
                                 89                 :                : 
                                 90                 :                : /* User-settable parameters for sync rep */
                                 91                 :                : char       *SyncRepStandbyNames;
                                 92                 :                : 
                                 93                 :                : #define SyncStandbysDefined() \
                                 94                 :                :     (SyncRepStandbyNames != NULL && SyncRepStandbyNames[0] != '\0')
                                 95                 :                : 
                                 96                 :                : static bool announce_next_takeover = true;
                                 97                 :                : 
                                 98                 :                : SyncRepConfigData *SyncRepConfig = NULL;
                                 99                 :                : static int  SyncRepWaitMode = SYNC_REP_NO_WAIT;
                                100                 :                : 
                                101                 :                : static void SyncRepQueueInsert(int mode);
                                102                 :                : static void SyncRepCancelWait(void);
                                103                 :                : static int  SyncRepWakeQueue(bool all, int mode);
                                104                 :                : 
                                105                 :                : static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr,
                                106                 :                :                                  XLogRecPtr *flushPtr,
                                107                 :                :                                  XLogRecPtr *applyPtr,
                                108                 :                :                                  bool *am_sync);
                                109                 :                : static void SyncRepGetOldestSyncRecPtr(XLogRecPtr *writePtr,
                                110                 :                :                                        XLogRecPtr *flushPtr,
                                111                 :                :                                        XLogRecPtr *applyPtr,
                                112                 :                :                                        SyncRepStandbyData *sync_standbys,
                                113                 :                :                                        int num_standbys);
                                114                 :                : static void SyncRepGetNthLatestSyncRecPtr(XLogRecPtr *writePtr,
                                115                 :                :                                           XLogRecPtr *flushPtr,
                                116                 :                :                                           XLogRecPtr *applyPtr,
                                117                 :                :                                           SyncRepStandbyData *sync_standbys,
                                118                 :                :                                           int num_standbys,
                                119                 :                :                                           uint8 nth);
                                120                 :                : static int  SyncRepGetStandbyPriority(void);
                                121                 :                : static int  standby_priority_comparator(const void *a, const void *b);
                                122                 :                : static int  cmp_lsn(const void *a, const void *b);
                                123                 :                : 
                                124                 :                : #ifdef USE_ASSERT_CHECKING
                                125                 :                : static bool SyncRepQueueIsOrderedByLSN(int mode);
                                126                 :                : #endif
                                127                 :                : 
                                128                 :                : /*
                                129                 :                :  * ===========================================================
                                130                 :                :  * Synchronous Replication functions for normal user backends
                                131                 :                :  * ===========================================================
                                132                 :                :  */
                                133                 :                : 
                                134                 :                : /*
                                135                 :                :  * Wait for synchronous replication, if requested by user.
                                136                 :                :  *
                                137                 :                :  * Initially backends start in state SYNC_REP_NOT_WAITING and then
                                138                 :                :  * change that state to SYNC_REP_WAITING before adding ourselves
                                139                 :                :  * to the wait queue. During SyncRepWakeQueue() a WALSender changes
                                140                 :                :  * the state to SYNC_REP_WAIT_COMPLETE once replication is confirmed.
                                141                 :                :  * This backend then resets its state to SYNC_REP_NOT_WAITING.
                                142                 :                :  *
                                143                 :                :  * 'lsn' represents the LSN to wait for.  'commit' indicates whether this LSN
                                144                 :                :  * represents a commit record.  If it doesn't, then we wait only for the WAL
                                145                 :                :  * to be flushed if synchronous_commit is set to the higher level of
                                146                 :                :  * remote_apply, because only commit records provide apply feedback.
                                147                 :                :  */
                                148                 :                : void
 3638 rhaas@postgresql.org      149                 :CBC      122880 : SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
                                150                 :                : {
                                151                 :                :     int         mode;
                                152                 :                : 
                                153                 :                :     /*
                                154                 :                :      * This should be called while holding interrupts during a transaction
                                155                 :                :      * commit to prevent the follow-up shared memory queue cleanups to be
                                156                 :                :      * influenced by external interruptions.
                                157                 :                :      */
 2326 michael@paquier.xyz       158         [ -  + ]:         122880 :     Assert(InterruptHoldoffCount > 0);
                                159                 :                : 
                                160                 :                :     /*
                                161                 :                :      * Fast exit if user has not requested sync replication, or there are no
                                162                 :                :      * sync replication standby names defined.
                                163                 :                :      *
                                164                 :                :      * Since this routine gets called every commit time, it's important to
                                165                 :                :      * exit quickly if sync replication is not requested.
                                166                 :                :      *
                                167                 :                :      * We check WalSndCtl->sync_standbys_status flag without the lock and exit
                                168                 :                :      * immediately if SYNC_STANDBY_INIT is set (the checkpointer has
                                169                 :                :      * initialized this data) but SYNC_STANDBY_DEFINED is missing (no sync
                                170                 :                :      * replication requested).
                                171                 :                :      *
                                172                 :                :      * If SYNC_STANDBY_DEFINED is set, we need to check the status again later
                                173                 :                :      * while holding the lock, to check the flag and operate the sync rep
                                174                 :                :      * queue atomically.  This is necessary to avoid the race condition
                                175                 :                :      * described in SyncRepUpdateSyncStandbysDefined().  On the other hand, if
                                176                 :                :      * SYNC_STANDBY_DEFINED is not set, the lock is not necessary because we
                                177                 :                :      * don't touch the queue.
                                178                 :                :      */
 2020 fujii@postgresql.org      179   [ +  +  +  + ]:         122880 :     if (!SyncRepRequested() ||
  338 michael@paquier.xyz       180         [ +  + ]:          91881 :         ((((volatile WalSndCtlData *) WalSndCtl)->sync_standbys_status) &
                                181                 :                :          (SYNC_STANDBY_INIT | SYNC_STANDBY_DEFINED)) == SYNC_STANDBY_INIT)
 2020 fujii@postgresql.org      182                 :          90546 :         return;
                                183                 :                : 
                                184                 :                :     /* Cap the level for anything other than commit to remote flush only. */
 3638 rhaas@postgresql.org      185         [ +  + ]:          32334 :     if (commit)
                                186                 :          32316 :         mode = SyncRepWaitMode;
                                187                 :                :     else
                                188                 :             18 :         mode = Min(SyncRepWaitMode, SYNC_REP_WAIT_FLUSH);
                                189                 :                : 
 1152 andres@anarazel.de        190         [ -  + ]:          32334 :     Assert(dlist_node_is_detached(&MyProc->syncRepLinks));
 5477 rhaas@postgresql.org      191         [ -  + ]:          32334 :     Assert(WalSndCtl != NULL);
                                192                 :                : 
                                193                 :          32334 :     LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
                                194         [ -  + ]:          32334 :     Assert(MyProc->syncRepState == SYNC_REP_NOT_WAITING);
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * We don't wait for sync rep if SYNC_STANDBY_DEFINED is not set.  See
                                198                 :                :      * SyncRepUpdateSyncStandbysDefined().
                                199                 :                :      *
                                200                 :                :      * Also check that the standby hasn't already replied. Unlikely race
                                201                 :                :      * condition but we'll be fetching that cache line anyway so it's likely
                                202                 :                :      * to be a low cost check.
                                203                 :                :      *
                                204                 :                :      * If the sync standby data has not been initialized yet
                                205                 :                :      * (SYNC_STANDBY_INIT is not set), fall back to a check based on the LSN,
                                206                 :                :      * then do a direct GUC check.
                                207                 :                :      */
  338 michael@paquier.xyz       208         [ +  + ]:          32334 :     if (WalSndCtl->sync_standbys_status & SYNC_STANDBY_INIT)
                                209                 :                :     {
                                210         [ +  - ]:             40 :         if ((WalSndCtl->sync_standbys_status & SYNC_STANDBY_DEFINED) == 0 ||
                                211         [ +  + ]:             40 :             lsn <= WalSndCtl->lsn[mode])
                                212                 :                :         {
                                213                 :              5 :             LWLockRelease(SyncRepLock);
                                214                 :              5 :             return;
                                215                 :                :         }
                                216                 :                :     }
                                217         [ -  + ]:          32294 :     else if (lsn <= WalSndCtl->lsn[mode])
                                218                 :                :     {
                                219                 :                :         /*
                                220                 :                :          * The LSN is older than what we need to wait for.  The sync standby
                                221                 :                :          * data has not been initialized yet, but we are OK to not wait
                                222                 :                :          * because we know that there is no point in doing so based on the
                                223                 :                :          * LSN.
                                224                 :                :          */
  338 michael@paquier.xyz       225                 :UBC           0 :         LWLockRelease(SyncRepLock);
                                226                 :              0 :         return;
                                227                 :                :     }
  338 michael@paquier.xyz       228   [ +  -  +  - ]:CBC       32294 :     else if (!SyncStandbysDefined())
                                229                 :                :     {
                                230                 :                :         /*
                                231                 :                :          * If we are here, the sync standby data has not been initialized yet,
                                232                 :                :          * and the LSN is newer than what need to wait for, so we have fallen
                                233                 :                :          * back to the best thing we could do in this case: a check on
                                234                 :                :          * SyncStandbysDefined() to see if the GUC is set or not.
                                235                 :                :          *
                                236                 :                :          * When the GUC has a value, we wait until the checkpointer updates
                                237                 :                :          * the status data because we cannot be sure yet if we should wait or
                                238                 :                :          * not. Here, the GUC has *no* value, we are sure that there is no
                                239                 :                :          * point to wait; this matters for example when initializing a
                                240                 :                :          * cluster, where we should never wait, and no sync standbys is the
                                241                 :                :          * default behavior.
                                242                 :                :          */
 5477 rhaas@postgresql.org      243                 :          32294 :         LWLockRelease(SyncRepLock);
                                244                 :          32294 :         return;
                                245                 :                :     }
                                246                 :                : 
                                247                 :                :     /*
                                248                 :                :      * Set our waitLSN so WALSender will know when to wake us, and add
                                249                 :                :      * ourselves to the queue.
                                250                 :                :      */
 3638                           251                 :             35 :     MyProc->waitLSN = lsn;
 5477                           252                 :             35 :     MyProc->syncRepState = SYNC_REP_WAITING;
 5158 simon@2ndQuadrant.co      253                 :             35 :     SyncRepQueueInsert(mode);
                                254         [ -  + ]:             35 :     Assert(SyncRepQueueIsOrderedByLSN(mode));
 5477 rhaas@postgresql.org      255                 :             35 :     LWLockRelease(SyncRepLock);
                                256                 :                : 
                                257                 :                :     /* Alter ps display to show waiting for sync rep. */
                                258         [ +  - ]:             35 :     if (update_process_title)
                                259                 :                :     {
                                260                 :                :         char        buffer[32];
                                261                 :                : 
  251 alvherre@kurilemu.de      262                 :GNC          35 :         sprintf(buffer, "waiting for %X/%08X", LSN_FORMAT_ARGS(lsn));
 1119 drowley@postgresql.o      263                 :CBC          35 :         set_ps_display_suffix(buffer);
                                264                 :                :     }
                                265                 :                : 
                                266                 :                :     /*
                                267                 :                :      * Wait for specified LSN to be confirmed.
                                268                 :                :      *
                                269                 :                :      * Each proc has its own wait latch, so we perform a normal latch
                                270                 :                :      * check/wait loop here.
                                271                 :                :      */
                                272                 :                :     for (;;)
 5488 simon@2ndQuadrant.co      273                 :             35 :     {
                                274                 :                :         int         rc;
                                275                 :                : 
                                276                 :                :         /* Must reset the latch before testing state. */
 4078 andres@anarazel.de        277                 :             70 :         ResetLatch(MyLatch);
                                278                 :                : 
                                279                 :                :         /*
                                280                 :                :          * Acquiring the lock is not needed, the latch ensures proper
                                281                 :                :          * barriers. If it looks like we're done, we must really be done,
                                282                 :                :          * because once walsender changes the state to SYNC_REP_WAIT_COMPLETE,
                                283                 :                :          * it will never update it again, so we can't be seeing a stale value
                                284                 :                :          * in that case.
                                285                 :                :          */
 3502 simon@2ndQuadrant.co      286         [ +  + ]:             70 :         if (MyProc->syncRepState == SYNC_REP_WAIT_COMPLETE)
 5477 rhaas@postgresql.org      287                 :             35 :             break;
                                288                 :                : 
                                289                 :                :         /*
                                290                 :                :          * If a wait for synchronous replication is pending, we can neither
                                291                 :                :          * acknowledge the commit nor raise ERROR or FATAL.  The latter would
                                292                 :                :          * lead the client to believe that the transaction aborted, which is
                                293                 :                :          * not true: it's already committed locally. The former is no good
                                294                 :                :          * either: the client has requested synchronous replication, and is
                                295                 :                :          * entitled to assume that an acknowledged commit is also replicated,
                                296                 :                :          * which might not be true. So in this case we issue a WARNING (which
                                297                 :                :          * some clients may be able to interpret) and shut off further output.
                                298                 :                :          * We do NOT reset ProcDiePending, so that the process will die after
                                299                 :                :          * the commit is cleaned up.
                                300                 :                :          */
                                301         [ -  + ]:             35 :         if (ProcDiePending)
                                302                 :                :         {
 5477 rhaas@postgresql.org      303         [ #  # ]:UBC           0 :             ereport(WARNING,
                                304                 :                :                     (errcode(ERRCODE_ADMIN_SHUTDOWN),
                                305                 :                :                      errmsg("canceling the wait for synchronous replication and terminating connection due to administrator command"),
                                306                 :                :                      errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
                                307                 :              0 :             whereToSendOutput = DestNone;
                                308                 :              0 :             SyncRepCancelWait();
                                309                 :              0 :             break;
                                310                 :                :         }
                                311                 :                : 
                                312                 :                :         /*
                                313                 :                :          * It's unclear what to do if a query cancel interrupt arrives.  We
                                314                 :                :          * can't actually abort at this point, but ignoring the interrupt
                                315                 :                :          * altogether is not helpful, so we just terminate the wait with a
                                316                 :                :          * suitable warning.
                                317                 :                :          */
 5477 rhaas@postgresql.org      318         [ -  + ]:CBC          35 :         if (QueryCancelPending)
                                319                 :                :         {
 5477 rhaas@postgresql.org      320                 :UBC           0 :             QueryCancelPending = false;
                                321         [ #  # ]:              0 :             ereport(WARNING,
                                322                 :                :                     (errmsg("canceling wait for synchronous replication due to user request"),
                                323                 :                :                      errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
                                324                 :              0 :             SyncRepCancelWait();
                                325                 :              0 :             break;
                                326                 :                :         }
                                327                 :                : 
                                328                 :                :         /*
                                329                 :                :          * Wait on latch.  Any condition that should wake us up will set the
                                330                 :                :          * latch, so no need for timeout.
                                331                 :                :          */
 2669 tmunro@postgresql.or      332                 :CBC          35 :         rc = WaitLatch(MyLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH, -1,
                                333                 :                :                        WAIT_EVENT_SYNC_REP);
                                334                 :                : 
                                335                 :                :         /*
                                336                 :                :          * If the postmaster dies, we'll probably never get an acknowledgment,
                                337                 :                :          * because all the wal sender processes will exit. So just bail out.
                                338                 :                :          */
                                339         [ -  + ]:             35 :         if (rc & WL_POSTMASTER_DEATH)
                                340                 :                :         {
 5477 rhaas@postgresql.org      341                 :UBC           0 :             ProcDiePending = true;
                                342                 :              0 :             whereToSendOutput = DestNone;
                                343                 :              0 :             SyncRepCancelWait();
                                344                 :              0 :             break;
                                345                 :                :         }
                                346                 :                :     }
                                347                 :                : 
                                348                 :                :     /*
                                349                 :                :      * WalSender has checked our LSN and has removed us from queue. Clean up
                                350                 :                :      * state and leave.  It's OK to reset these shared memory fields without
                                351                 :                :      * holding SyncRepLock, because any walsenders will ignore us anyway when
                                352                 :                :      * we're not on the queue.  We need a read barrier to make sure we see the
                                353                 :                :      * changes to the queue link (this might be unnecessary without
                                354                 :                :      * assertions, but better safe than sorry).
                                355                 :                :      */
 3168 heikki.linnakangas@i      356                 :CBC          35 :     pg_read_barrier();
 1152 andres@anarazel.de        357         [ -  + ]:             35 :     Assert(dlist_node_is_detached(&MyProc->syncRepLinks));
 5477 rhaas@postgresql.org      358                 :             35 :     MyProc->syncRepState = SYNC_REP_NOT_WAITING;
   45 alvherre@kurilemu.de      359                 :GNC          35 :     MyProc->waitLSN = InvalidXLogRecPtr;
                                360                 :                : 
                                361                 :                :     /* reset ps display to remove the suffix */
 1119 drowley@postgresql.o      362         [ +  - ]:CBC          35 :     if (update_process_title)
                                363                 :             35 :         set_ps_display_remove_suffix();
                                364                 :                : }
                                365                 :                : 
                                366                 :                : /*
                                367                 :                :  * Insert MyProc into the specified SyncRepQueue, maintaining sorted invariant.
                                368                 :                :  *
                                369                 :                :  * Usually we will go at tail of queue, though it's possible that we arrive
                                370                 :                :  * here out of order, so start at tail and work back to insertion point.
                                371                 :                :  */
                                372                 :                : static void
 5164 simon@2ndQuadrant.co      373                 :             35 : SyncRepQueueInsert(int mode)
                                374                 :                : {
                                375                 :                :     dlist_head *queue;
                                376                 :                :     dlist_iter  iter;
                                377                 :                : 
                                378   [ +  -  -  + ]:             35 :     Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
 1152 andres@anarazel.de        379                 :             35 :     queue = &WalSndCtl->SyncRepQueue[mode];
                                380                 :                : 
                                381   [ +  -  -  + ]:             35 :     dlist_reverse_foreach(iter, queue)
                                382                 :                :     {
 1152 andres@anarazel.de        383                 :UBC           0 :         PGPROC     *proc = dlist_container(PGPROC, syncRepLinks, iter.cur);
                                384                 :                : 
                                385                 :                :         /*
                                386                 :                :          * Stop at the queue element that we should insert after to ensure the
                                387                 :                :          * queue is ordered by LSN.
                                388                 :                :          */
 4825 alvherre@alvh.no-ip.      389         [ #  # ]:              0 :         if (proc->waitLSN < MyProc->waitLSN)
                                390                 :                :         {
 1152 andres@anarazel.de        391                 :              0 :             dlist_insert_after(&proc->syncRepLinks, &MyProc->syncRepLinks);
                                392                 :              0 :             return;
                                393                 :                :         }
                                394                 :                :     }
                                395                 :                : 
                                396                 :                :     /*
                                397                 :                :      * If we get here, the list was either empty, or this process needs to be
                                398                 :                :      * at the head.
                                399                 :                :      */
 1152 andres@anarazel.de        400                 :CBC          35 :     dlist_push_head(queue, &MyProc->syncRepLinks);
                                401                 :                : }
                                402                 :                : 
                                403                 :                : /*
                                404                 :                :  * Acquire SyncRepLock and cancel any wait currently in progress.
                                405                 :                :  */
                                406                 :                : static void
 5477 rhaas@postgresql.org      407                 :UBC           0 : SyncRepCancelWait(void)
                                408                 :                : {
                                409                 :              0 :     LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
 1152 andres@anarazel.de        410         [ #  # ]:              0 :     if (!dlist_node_is_detached(&MyProc->syncRepLinks))
                                411                 :              0 :         dlist_delete_thoroughly(&MyProc->syncRepLinks);
 5477 rhaas@postgresql.org      412                 :              0 :     MyProc->syncRepState = SYNC_REP_NOT_WAITING;
                                413                 :              0 :     LWLockRelease(SyncRepLock);
                                414                 :              0 : }
                                415                 :                : 
                                416                 :                : void
 5331 tgl@sss.pgh.pa.us         417                 :CBC       16962 : SyncRepCleanupAtProcExit(void)
                                418                 :                : {
                                419                 :                :     /*
                                420                 :                :      * First check if we are removed from the queue without the lock to not
                                421                 :                :      * slow down backend exit.
                                422                 :                :      */
 1152 andres@anarazel.de        423         [ -  + ]:          16962 :     if (!dlist_node_is_detached(&MyProc->syncRepLinks))
                                424                 :                :     {
 5488 simon@2ndQuadrant.co      425                 :UBC           0 :         LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
                                426                 :                : 
                                427                 :                :         /* maybe we have just been removed, so recheck */
 1152 andres@anarazel.de        428         [ #  # ]:              0 :         if (!dlist_node_is_detached(&MyProc->syncRepLinks))
                                429                 :              0 :             dlist_delete_thoroughly(&MyProc->syncRepLinks);
                                430                 :                : 
 5488 simon@2ndQuadrant.co      431                 :              0 :         LWLockRelease(SyncRepLock);
                                432                 :                :     }
 5488 simon@2ndQuadrant.co      433                 :CBC       16962 : }
                                434                 :                : 
                                435                 :                : /*
                                436                 :                :  * ===========================================================
                                437                 :                :  * Synchronous Replication functions for wal sender processes
                                438                 :                :  * ===========================================================
                                439                 :                :  */
                                440                 :                : 
                                441                 :                : /*
                                442                 :                :  * Take any action required to initialise sync rep state from config
                                443                 :                :  * data. Called at WALSender startup and after each SIGHUP.
                                444                 :                :  */
                                445                 :                : void
                                446                 :            757 : SyncRepInitConfig(void)
                                447                 :                : {
                                448                 :                :     int         priority;
                                449                 :                : 
                                450                 :                :     /*
                                451                 :                :      * Determine if we are a potential sync standby and remember the result
                                452                 :                :      * for handling replies from standby.
                                453                 :                :      */
                                454                 :            757 :     priority = SyncRepGetStandbyPriority();
                                455         [ +  + ]:            757 :     if (MyWalSnd->sync_standby_priority != priority)
                                456                 :                :     {
 2157 tgl@sss.pgh.pa.us         457         [ -  + ]:             18 :         SpinLockAcquire(&MyWalSnd->mutex);
 5488 simon@2ndQuadrant.co      458                 :             18 :         MyWalSnd->sync_standby_priority = priority;
 2157 tgl@sss.pgh.pa.us         459                 :             18 :         SpinLockRelease(&MyWalSnd->mutex);
                                460                 :                : 
 5488 simon@2ndQuadrant.co      461         [ -  + ]:             18 :         ereport(DEBUG1,
                                462                 :                :                 (errmsg_internal("standby \"%s\" now has synchronous standby priority %d",
                                463                 :                :                                  application_name, priority)));
                                464                 :                :     }
                                465                 :            757 : }
                                466                 :                : 
                                467                 :                : /*
                                468                 :                :  * Update the LSNs on each queue based upon our latest state. This
                                469                 :                :  * implements a simple policy of first-valid-sync-standby-releases-waiter.
                                470                 :                :  *
                                471                 :                :  * Other policies are possible, which would change what we do here and
                                472                 :                :  * perhaps also which information we store as well.
                                473                 :                :  */
                                474                 :                : void
                                475                 :         100000 : SyncRepReleaseWaiters(void)
                                476                 :                : {
                                477                 :         100000 :     volatile WalSndCtlData *walsndctl = WalSndCtl;
                                478                 :                :     XLogRecPtr  writePtr;
                                479                 :                :     XLogRecPtr  flushPtr;
                                480                 :                :     XLogRecPtr  applyPtr;
                                481                 :                :     bool        got_recptr;
                                482                 :                :     bool        am_sync;
 5164                           483                 :         100000 :     int         numwrite = 0;
                                484                 :         100000 :     int         numflush = 0;
 3638 rhaas@postgresql.org      485                 :         100000 :     int         numapply = 0;
                                486                 :                : 
                                487                 :                :     /*
                                488                 :                :      * If this WALSender is serving a standby that is not on the list of
                                489                 :                :      * potential sync standbys then we have nothing to do. If we are still
                                490                 :                :      * starting up, still running base backup or the current flush position is
                                491                 :                :      * still invalid, then leave quickly also.  Streaming or stopping WAL
                                492                 :                :      * senders are allowed to release waiters.
                                493                 :                :      */
 5488 simon@2ndQuadrant.co      494         [ +  + ]:         100000 :     if (MyWalSnd->sync_standby_priority == 0 ||
 2663 michael@paquier.xyz       495         [ +  + ]:            279 :         (MyWalSnd->state != WALSNDSTATE_STREAMING &&
                                496         [ +  + ]:            157 :          MyWalSnd->state != WALSNDSTATE_STOPPING) ||
  129 alvherre@kurilemu.de      497         [ -  + ]:GNC         265 :         !XLogRecPtrIsValid(MyWalSnd->flush))
                                498                 :                :     {
 3630 fujii@postgresql.org      499                 :CBC       99735 :         announce_next_takeover = true;
 5488 simon@2ndQuadrant.co      500                 :          99739 :         return;
                                501                 :                :     }
                                502                 :                : 
                                503                 :                :     /*
                                504                 :                :      * We're a potential sync standby. Release waiters if there are enough
                                505                 :                :      * sync standbys and we are considered as sync.
                                506                 :                :      */
                                507                 :            265 :     LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
                                508                 :                : 
                                509                 :                :     /*
                                510                 :                :      * Check whether we are a sync standby or not, and calculate the synced
                                511                 :                :      * positions among all sync standbys.  (Note: although this step does not
                                512                 :                :      * of itself require holding SyncRepLock, it seems like a good idea to do
                                513                 :                :      * it after acquiring the lock.  This ensures that the WAL pointers we use
                                514                 :                :      * to release waiters are newer than any previous execution of this
                                515                 :                :      * routine used.)
                                516                 :                :      */
 3373 fujii@postgresql.org      517                 :            265 :     got_recptr = SyncRepGetSyncRecPtr(&writePtr, &flushPtr, &applyPtr, &am_sync);
                                518                 :                : 
                                519                 :                :     /*
                                520                 :                :      * If we are managing a sync standby, though we weren't prior to this,
                                521                 :                :      * then announce we are now a sync standby.
                                522                 :                :      */
 3630                           523   [ +  +  +  + ]:            265 :     if (announce_next_takeover && am_sync)
                                524                 :                :     {
                                525                 :             16 :         announce_next_takeover = false;
                                526                 :                : 
 3373                           527         [ +  - ]:             16 :         if (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY)
                                528         [ +  - ]:             16 :             ereport(LOG,
                                529                 :                :                     (errmsg("standby \"%s\" is now a synchronous standby with priority %d",
                                530                 :                :                             application_name, MyWalSnd->sync_standby_priority)));
                                531                 :                :         else
 3373 fujii@postgresql.org      532         [ #  # ]:UBC           0 :             ereport(LOG,
                                533                 :                :                     (errmsg("standby \"%s\" is now a candidate for quorum synchronous standby",
                                534                 :                :                             application_name)));
                                535                 :                :     }
                                536                 :                : 
                                537                 :                :     /*
                                538                 :                :      * If the number of sync standbys is less than requested or we aren't
                                539                 :                :      * managing a sync standby then just leave.
                                540                 :                :      */
 3373 fujii@postgresql.org      541   [ +  +  -  + ]:CBC         265 :     if (!got_recptr || !am_sync)
                                542                 :                :     {
 5488 simon@2ndQuadrant.co      543                 :              4 :         LWLockRelease(SyncRepLock);
 3630 fujii@postgresql.org      544                 :              4 :         announce_next_takeover = !am_sync;
 5488 simon@2ndQuadrant.co      545                 :              4 :         return;
                                546                 :                :     }
                                547                 :                : 
                                548                 :                :     /*
                                549                 :                :      * Set the lsn first so that when we wake backends they will release up to
                                550                 :                :      * this location.
                                551                 :                :      */
 3630 fujii@postgresql.org      552         [ +  + ]:            261 :     if (walsndctl->lsn[SYNC_REP_WAIT_WRITE] < writePtr)
                                553                 :                :     {
                                554                 :             52 :         walsndctl->lsn[SYNC_REP_WAIT_WRITE] = writePtr;
 5164 simon@2ndQuadrant.co      555                 :             52 :         numwrite = SyncRepWakeQueue(false, SYNC_REP_WAIT_WRITE);
                                556                 :                :     }
 3630 fujii@postgresql.org      557         [ +  + ]:            261 :     if (walsndctl->lsn[SYNC_REP_WAIT_FLUSH] < flushPtr)
                                558                 :                :     {
                                559                 :             57 :         walsndctl->lsn[SYNC_REP_WAIT_FLUSH] = flushPtr;
 5164 simon@2ndQuadrant.co      560                 :             57 :         numflush = SyncRepWakeQueue(false, SYNC_REP_WAIT_FLUSH);
                                561                 :                :     }
 3630 fujii@postgresql.org      562         [ +  + ]:            261 :     if (walsndctl->lsn[SYNC_REP_WAIT_APPLY] < applyPtr)
                                563                 :                :     {
                                564                 :             53 :         walsndctl->lsn[SYNC_REP_WAIT_APPLY] = applyPtr;
 3638 rhaas@postgresql.org      565                 :             53 :         numapply = SyncRepWakeQueue(false, SYNC_REP_WAIT_APPLY);
                                566                 :                :     }
                                567                 :                : 
 5488 simon@2ndQuadrant.co      568                 :            261 :     LWLockRelease(SyncRepLock);
                                569                 :                : 
  251 alvherre@kurilemu.de      570         [ -  + ]:GNC         261 :     elog(DEBUG3, "released %d procs up to write %X/%08X, %d procs up to flush %X/%08X, %d procs up to apply %X/%08X",
                                571                 :                :          numwrite, LSN_FORMAT_ARGS(writePtr),
                                572                 :                :          numflush, LSN_FORMAT_ARGS(flushPtr),
                                573                 :                :          numapply, LSN_FORMAT_ARGS(applyPtr));
                                574                 :                : }
                                575                 :                : 
                                576                 :                : /*
                                577                 :                :  * Calculate the synced Write, Flush and Apply positions among sync standbys.
                                578                 :                :  *
                                579                 :                :  * Return false if the number of sync standbys is less than
                                580                 :                :  * synchronous_standby_names specifies. Otherwise return true and
                                581                 :                :  * store the positions into *writePtr, *flushPtr and *applyPtr.
                                582                 :                :  *
                                583                 :                :  * On return, *am_sync is set to true if this walsender is connecting to
                                584                 :                :  * sync standby. Otherwise it's set to false.
                                585                 :                :  */
                                586                 :                : static bool
 3373 fujii@postgresql.org      587                 :CBC         265 : SyncRepGetSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr,
                                588                 :                :                      XLogRecPtr *applyPtr, bool *am_sync)
                                589                 :                : {
                                590                 :                :     SyncRepStandbyData *sync_standbys;
                                591                 :                :     int         num_standbys;
                                592                 :                :     int         i;
                                593                 :                : 
                                594                 :                :     /* Initialize default results */
 3630                           595                 :            265 :     *writePtr = InvalidXLogRecPtr;
                                596                 :            265 :     *flushPtr = InvalidXLogRecPtr;
                                597                 :            265 :     *applyPtr = InvalidXLogRecPtr;
                                598                 :            265 :     *am_sync = false;
                                599                 :                : 
                                600                 :                :     /* Quick out if not even configured to be synchronous */
 2157 tgl@sss.pgh.pa.us         601         [ -  + ]:            265 :     if (SyncRepConfig == NULL)
 2157 tgl@sss.pgh.pa.us         602                 :UBC           0 :         return false;
                                603                 :                : 
                                604                 :                :     /* Get standbys that are considered as synchronous at this moment */
 2157 tgl@sss.pgh.pa.us         605                 :CBC         265 :     num_standbys = SyncRepGetCandidateStandbys(&sync_standbys);
                                606                 :                : 
                                607                 :                :     /* Am I among the candidate sync standbys? */
                                608         [ +  + ]:            271 :     for (i = 0; i < num_standbys; i++)
                                609                 :                :     {
                                610         [ +  + ]:            268 :         if (sync_standbys[i].is_me)
                                611                 :                :         {
                                612                 :            262 :             *am_sync = true;
                                613                 :            262 :             break;
                                614                 :                :         }
                                615                 :                :     }
                                616                 :                : 
                                617                 :                :     /*
                                618                 :                :      * Nothing more to do if we are not managing a sync standby or there are
                                619                 :                :      * not enough synchronous standbys.
                                620                 :                :      */
 3609                           621         [ +  + ]:            265 :     if (!(*am_sync) ||
 2157                           622         [ +  + ]:            262 :         num_standbys < SyncRepConfig->num_sync)
                                623                 :                :     {
                                624                 :              4 :         pfree(sync_standbys);
 3630 fujii@postgresql.org      625                 :              4 :         return false;
                                626                 :                :     }
                                627                 :                : 
                                628                 :                :     /*
                                629                 :                :      * In a priority-based sync replication, the synced positions are the
                                630                 :                :      * oldest ones among sync standbys. In a quorum-based, they are the Nth
                                631                 :                :      * latest ones.
                                632                 :                :      *
                                633                 :                :      * SyncRepGetNthLatestSyncRecPtr() also can calculate the oldest
                                634                 :                :      * positions. But we use SyncRepGetOldestSyncRecPtr() for that calculation
                                635                 :                :      * because it's a bit more efficient.
                                636                 :                :      *
                                637                 :                :      * XXX If the numbers of current and requested sync standbys are the same,
                                638                 :                :      * we can use SyncRepGetOldestSyncRecPtr() to calculate the synced
                                639                 :                :      * positions even in a quorum-based sync replication.
                                640                 :                :      */
 3373                           641         [ +  - ]:            261 :     if (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY)
                                642                 :                :     {
                                643                 :            261 :         SyncRepGetOldestSyncRecPtr(writePtr, flushPtr, applyPtr,
                                644                 :                :                                    sync_standbys, num_standbys);
                                645                 :                :     }
                                646                 :                :     else
                                647                 :                :     {
 3373 fujii@postgresql.org      648                 :UBC           0 :         SyncRepGetNthLatestSyncRecPtr(writePtr, flushPtr, applyPtr,
                                649                 :                :                                       sync_standbys, num_standbys,
 2157 tgl@sss.pgh.pa.us         650                 :              0 :                                       SyncRepConfig->num_sync);
                                651                 :                :     }
                                652                 :                : 
 2157 tgl@sss.pgh.pa.us         653                 :CBC         261 :     pfree(sync_standbys);
 3373 fujii@postgresql.org      654                 :            261 :     return true;
                                655                 :                : }
                                656                 :                : 
                                657                 :                : /*
                                658                 :                :  * Calculate the oldest Write, Flush and Apply positions among sync standbys.
                                659                 :                :  */
                                660                 :                : static void
 2157 tgl@sss.pgh.pa.us         661                 :            261 : SyncRepGetOldestSyncRecPtr(XLogRecPtr *writePtr,
                                662                 :                :                            XLogRecPtr *flushPtr,
                                663                 :                :                            XLogRecPtr *applyPtr,
                                664                 :                :                            SyncRepStandbyData *sync_standbys,
                                665                 :                :                            int num_standbys)
                                666                 :                : {
                                667                 :                :     int         i;
                                668                 :                : 
                                669                 :                :     /*
                                670                 :                :      * Scan through all sync standbys and calculate the oldest Write, Flush
                                671                 :                :      * and Apply positions.  We assume *writePtr et al were initialized to
                                672                 :                :      * InvalidXLogRecPtr.
                                673                 :                :      */
                                674         [ +  + ]:            524 :     for (i = 0; i < num_standbys; i++)
                                675                 :                :     {
                                676                 :            263 :         XLogRecPtr  write = sync_standbys[i].write;
                                677                 :            263 :         XLogRecPtr  flush = sync_standbys[i].flush;
                                678                 :            263 :         XLogRecPtr  apply = sync_standbys[i].apply;
                                679                 :                : 
  129 alvherre@kurilemu.de      680   [ +  +  -  + ]:GNC         263 :         if (!XLogRecPtrIsValid(*writePtr) || *writePtr > write)
 3630 fujii@postgresql.org      681                 :CBC         261 :             *writePtr = write;
  129 alvherre@kurilemu.de      682   [ +  +  -  + ]:GNC         263 :         if (!XLogRecPtrIsValid(*flushPtr) || *flushPtr > flush)
 3630 fujii@postgresql.org      683                 :CBC         261 :             *flushPtr = flush;
  129 alvherre@kurilemu.de      684   [ +  +  -  + ]:GNC         263 :         if (!XLogRecPtrIsValid(*applyPtr) || *applyPtr > apply)
 3630 fujii@postgresql.org      685                 :CBC         261 :             *applyPtr = apply;
                                686                 :                :     }
 3373                           687                 :            261 : }
                                688                 :                : 
                                689                 :                : /*
                                690                 :                :  * Calculate the Nth latest Write, Flush and Apply positions among sync
                                691                 :                :  * standbys.
                                692                 :                :  */
                                693                 :                : static void
 2157 tgl@sss.pgh.pa.us         694                 :UBC           0 : SyncRepGetNthLatestSyncRecPtr(XLogRecPtr *writePtr,
                                695                 :                :                               XLogRecPtr *flushPtr,
                                696                 :                :                               XLogRecPtr *applyPtr,
                                697                 :                :                               SyncRepStandbyData *sync_standbys,
                                698                 :                :                               int num_standbys,
                                699                 :                :                               uint8 nth)
                                700                 :                : {
                                701                 :                :     XLogRecPtr *write_array;
                                702                 :                :     XLogRecPtr *flush_array;
                                703                 :                :     XLogRecPtr *apply_array;
                                704                 :                :     int         i;
                                705                 :                : 
                                706                 :                :     /* Should have enough candidates, or somebody messed up */
                                707   [ #  #  #  # ]:              0 :     Assert(nth > 0 && nth <= num_standbys);
                                708                 :                : 
   95 michael@paquier.xyz       709                 :UNC           0 :     write_array = palloc_array(XLogRecPtr, num_standbys);
                                710                 :              0 :     flush_array = palloc_array(XLogRecPtr, num_standbys);
                                711                 :              0 :     apply_array = palloc_array(XLogRecPtr, num_standbys);
                                712                 :                : 
 2157 tgl@sss.pgh.pa.us         713         [ #  # ]:UBC           0 :     for (i = 0; i < num_standbys; i++)
                                714                 :                :     {
                                715                 :              0 :         write_array[i] = sync_standbys[i].write;
                                716                 :              0 :         flush_array[i] = sync_standbys[i].flush;
                                717                 :              0 :         apply_array[i] = sync_standbys[i].apply;
                                718                 :                :     }
                                719                 :                : 
                                720                 :                :     /* Sort each array in descending order */
                                721                 :              0 :     qsort(write_array, num_standbys, sizeof(XLogRecPtr), cmp_lsn);
                                722                 :              0 :     qsort(flush_array, num_standbys, sizeof(XLogRecPtr), cmp_lsn);
                                723                 :              0 :     qsort(apply_array, num_standbys, sizeof(XLogRecPtr), cmp_lsn);
                                724                 :                : 
                                725                 :                :     /* Get Nth latest Write, Flush, Apply positions */
 3373 fujii@postgresql.org      726                 :              0 :     *writePtr = write_array[nth - 1];
                                727                 :              0 :     *flushPtr = flush_array[nth - 1];
                                728                 :              0 :     *applyPtr = apply_array[nth - 1];
                                729                 :                : 
                                730                 :              0 :     pfree(write_array);
                                731                 :              0 :     pfree(flush_array);
                                732                 :              0 :     pfree(apply_array);
                                733                 :              0 : }
                                734                 :                : 
                                735                 :                : /*
                                736                 :                :  * Compare lsn in order to sort array in descending order.
                                737                 :                :  */
                                738                 :                : static int
                                739                 :              0 : cmp_lsn(const void *a, const void *b)
                                740                 :                : {
 3224 bruce@momjian.us          741                 :              0 :     XLogRecPtr  lsn1 = *((const XLogRecPtr *) a);
                                742                 :              0 :     XLogRecPtr  lsn2 = *((const XLogRecPtr *) b);
                                743                 :                : 
  758 nathan@postgresql.or      744                 :              0 :     return pg_cmp_u64(lsn2, lsn1);
                                745                 :                : }
                                746                 :                : 
                                747                 :                : /*
                                748                 :                :  * Return data about walsenders that are candidates to be sync standbys.
                                749                 :                :  *
                                750                 :                :  * *standbys is set to a palloc'd array of structs of per-walsender data,
                                751                 :                :  * and the number of valid entries (candidate sync senders) is returned.
                                752                 :                :  * (This might be more or fewer than num_sync; caller must check.)
                                753                 :                :  */
                                754                 :                : int
 2157 tgl@sss.pgh.pa.us         755                 :CBC        1002 : SyncRepGetCandidateStandbys(SyncRepStandbyData **standbys)
                                756                 :                : {
                                757                 :                :     int         i;
                                758                 :                :     int         n;
                                759                 :                : 
                                760                 :                :     /* Create result array */
   95 michael@paquier.xyz       761                 :GNC        1002 :     *standbys = palloc_array(SyncRepStandbyData, max_wal_senders);
                                762                 :                : 
                                763                 :                :     /* Quick exit if sync replication is not requested */
 3373 fujii@postgresql.org      764         [ +  + ]:CBC        1002 :     if (SyncRepConfig == NULL)
 2157 tgl@sss.pgh.pa.us         765                 :            720 :         return 0;
                                766                 :                : 
                                767                 :                :     /* Collect raw data from shared memory */
                                768                 :            282 :     n = 0;
 3373 fujii@postgresql.org      769         [ +  + ]:           3102 :     for (i = 0; i < max_wal_senders; i++)
                                770                 :                :     {
                                771                 :                :         volatile WalSnd *walsnd;    /* Use volatile pointer to prevent code
                                772                 :                :                                      * rearrangement */
                                773                 :                :         SyncRepStandbyData *stby;
                                774                 :                :         WalSndState state;      /* not included in SyncRepStandbyData */
                                775                 :                : 
                                776                 :           2820 :         walsnd = &WalSndCtl->walsnds[i];
 2157 tgl@sss.pgh.pa.us         777                 :           2820 :         stby = *standbys + n;
                                778                 :                : 
 3180 alvherre@alvh.no-ip.      779         [ -  + ]:           2820 :         SpinLockAcquire(&walsnd->mutex);
 2157 tgl@sss.pgh.pa.us         780                 :           2820 :         stby->pid = walsnd->pid;
 3180 alvherre@alvh.no-ip.      781                 :           2820 :         state = walsnd->state;
 2157 tgl@sss.pgh.pa.us         782                 :           2820 :         stby->write = walsnd->write;
                                783                 :           2820 :         stby->flush = walsnd->flush;
                                784                 :           2820 :         stby->apply = walsnd->apply;
                                785                 :           2820 :         stby->sync_standby_priority = walsnd->sync_standby_priority;
 3180 alvherre@alvh.no-ip.      786                 :           2820 :         SpinLockRelease(&walsnd->mutex);
                                787                 :                : 
                                788                 :                :         /* Must be active */
 2157 tgl@sss.pgh.pa.us         789         [ +  + ]:           2820 :         if (stby->pid == 0)
 3373 fujii@postgresql.org      790                 :           2490 :             continue;
                                791                 :                : 
                                792                 :                :         /* Must be streaming or stopping */
 2663 michael@paquier.xyz       793   [ +  +  -  + ]:            330 :         if (state != WALSNDSTATE_STREAMING &&
                                794                 :                :             state != WALSNDSTATE_STOPPING)
 3373 fujii@postgresql.org      795                 :UBC           0 :             continue;
                                796                 :                : 
                                797                 :                :         /* Must be synchronous */
 2157 tgl@sss.pgh.pa.us         798         [ +  + ]:CBC         330 :         if (stby->sync_standby_priority == 0)
 3373 fujii@postgresql.org      799                 :             14 :             continue;
                                800                 :                : 
                                801                 :                :         /* Must have a valid flush position */
  129 alvherre@kurilemu.de      802         [ -  + ]:GNC         316 :         if (!XLogRecPtrIsValid(stby->flush))
 3373 fujii@postgresql.org      803                 :UBC           0 :             continue;
                                804                 :                : 
                                805                 :                :         /* OK, it's a candidate */
 2157 tgl@sss.pgh.pa.us         806                 :CBC         316 :         stby->walsnd_index = i;
                                807                 :            316 :         stby->is_me = (walsnd == MyWalSnd);
                                808                 :            316 :         n++;
                                809                 :                :     }
                                810                 :                : 
                                811                 :                :     /*
                                812                 :                :      * In quorum mode, we return all the candidates.  In priority mode, if we
                                813                 :                :      * have too many candidates then return only the num_sync ones of highest
                                814                 :                :      * priority.
                                815                 :                :      */
                                816         [ +  + ]:            282 :     if (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY &&
                                817         [ +  + ]:            281 :         n > SyncRepConfig->num_sync)
                                818                 :                :     {
                                819                 :                :         /* Sort by priority ... */
                                820                 :             14 :         qsort(*standbys, n, sizeof(SyncRepStandbyData),
                                821                 :                :               standby_priority_comparator);
                                822                 :                :         /* ... then report just the first num_sync ones */
                                823                 :             14 :         n = SyncRepConfig->num_sync;
                                824                 :                :     }
                                825                 :                : 
                                826                 :            282 :     return n;
                                827                 :                : }
                                828                 :                : 
                                829                 :                : /*
                                830                 :                :  * qsort comparator to sort SyncRepStandbyData entries by priority
                                831                 :                :  */
                                832                 :                : static int
                                833                 :             31 : standby_priority_comparator(const void *a, const void *b)
                                834                 :                : {
                                835                 :             31 :     const SyncRepStandbyData *sa = (const SyncRepStandbyData *) a;
                                836                 :             31 :     const SyncRepStandbyData *sb = (const SyncRepStandbyData *) b;
                                837                 :                : 
                                838                 :                :     /* First, sort by increasing priority value */
                                839         [ +  + ]:             31 :     if (sa->sync_standby_priority != sb->sync_standby_priority)
                                840                 :             14 :         return sa->sync_standby_priority - sb->sync_standby_priority;
                                841                 :                : 
                                842                 :                :     /*
                                843                 :                :      * We might have equal priority values; arbitrarily break ties by position
                                844                 :                :      * in the WalSnd array.  (This is utterly bogus, since that is arrival
                                845                 :                :      * order dependent, but there are regression tests that rely on it.)
                                846                 :                :      */
                                847                 :             17 :     return sa->walsnd_index - sb->walsnd_index;
                                848                 :                : }
                                849                 :                : 
                                850                 :                : 
                                851                 :                : /*
                                852                 :                :  * Check if we are in the list of sync standbys, and if so, determine
                                853                 :                :  * priority sequence. Return priority if set, or zero to indicate that
                                854                 :                :  * we are not a potential sync standby.
                                855                 :                :  *
                                856                 :                :  * Compare the parameter SyncRepStandbyNames against the application_name
                                857                 :                :  * for this WALSender, or allow any name if we find a wildcard "*".
                                858                 :                :  */
                                859                 :                : static int
 5488 simon@2ndQuadrant.co      860                 :            757 : SyncRepGetStandbyPriority(void)
                                861                 :                : {
                                862                 :                :     const char *standby_name;
                                863                 :                :     int         priority;
                                864                 :            757 :     bool        found = false;
                                865                 :                : 
                                866                 :                :     /*
                                867                 :                :      * Since synchronous cascade replication is not allowed, we always set the
                                868                 :                :      * priority of cascading walsender to zero.
                                869                 :                :      */
 5353                           870         [ +  + ]:            757 :     if (am_cascading_walsender)
                                871                 :             26 :         return 0;
                                872                 :                : 
 3609 tgl@sss.pgh.pa.us         873   [ +  -  +  +  :            731 :     if (!SyncStandbysDefined() || SyncRepConfig == NULL)
                                              -  + ]
 5488 simon@2ndQuadrant.co      874                 :            706 :         return 0;
                                875                 :                : 
 3609 tgl@sss.pgh.pa.us         876                 :             25 :     standby_name = SyncRepConfig->member_names;
                                877         [ +  + ]:             33 :     for (priority = 1; priority <= SyncRepConfig->nmembers; priority++)
                                878                 :                :     {
 5488 simon@2ndQuadrant.co      879         [ +  + ]:             32 :         if (pg_strcasecmp(standby_name, application_name) == 0 ||
 3609 tgl@sss.pgh.pa.us         880         [ +  + ]:             18 :             strcmp(standby_name, "*") == 0)
                                881                 :                :         {
 5488 simon@2ndQuadrant.co      882                 :             24 :             found = true;
                                883                 :             24 :             break;
                                884                 :                :         }
 3609 tgl@sss.pgh.pa.us         885                 :              8 :         standby_name += strlen(standby_name) + 1;
                                886                 :                :     }
                                887                 :                : 
 3245 fujii@postgresql.org      888         [ +  + ]:             25 :     if (!found)
                                889                 :              1 :         return 0;
                                890                 :                : 
                                891                 :                :     /*
                                892                 :                :      * In quorum-based sync replication, all the standbys in the list have the
                                893                 :                :      * same priority, one.
                                894                 :                :      */
                                895         [ +  - ]:             24 :     return (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY) ? priority : 1;
                                896                 :                : }
                                897                 :                : 
                                898                 :                : /*
                                899                 :                :  * Walk the specified queue from head.  Set the state of any backends that
                                900                 :                :  * need to be woken, remove them from the queue, and then wake them.
                                901                 :                :  * Pass all = true to wake whole queue; otherwise, just wake up to
                                902                 :                :  * the walsender's LSN.
                                903                 :                :  *
                                904                 :                :  * The caller must hold SyncRepLock in exclusive mode.
                                905                 :                :  */
                                906                 :                : static int
 5164 simon@2ndQuadrant.co      907                 :            165 : SyncRepWakeQueue(bool all, int mode)
                                908                 :                : {
 5488                           909                 :            165 :     volatile WalSndCtlData *walsndctl = WalSndCtl;
 5453 bruce@momjian.us          910                 :            165 :     int         numprocs = 0;
                                911                 :                :     dlist_mutable_iter iter;
                                912                 :                : 
 5164 simon@2ndQuadrant.co      913   [ +  -  -  + ]:            165 :     Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
 2326 michael@paquier.xyz       914         [ -  + ]:            165 :     Assert(LWLockHeldByMeInMode(SyncRepLock, LW_EXCLUSIVE));
 5164 simon@2ndQuadrant.co      915         [ -  + ]:            165 :     Assert(SyncRepQueueIsOrderedByLSN(mode));
                                916                 :                : 
 1152 andres@anarazel.de        917   [ +  -  +  + ]:            190 :     dlist_foreach_modify(iter, &WalSndCtl->SyncRepQueue[mode])
                                918                 :                :     {
 1031 tgl@sss.pgh.pa.us         919                 :             28 :         PGPROC     *proc = dlist_container(PGPROC, syncRepLinks, iter.cur);
                                920                 :                : 
                                921                 :                :         /*
                                922                 :                :          * Assume the queue is ordered by LSN
                                923                 :                :          */
 4825 alvherre@alvh.no-ip.      924   [ +  -  +  + ]:             28 :         if (!all && walsndctl->lsn[mode] < proc->waitLSN)
 5488 simon@2ndQuadrant.co      925                 :              3 :             return numprocs;
                                926                 :                : 
                                927                 :                :         /*
                                928                 :                :          * Remove from queue.
                                929                 :                :          */
 1152 andres@anarazel.de        930                 :             25 :         dlist_delete_thoroughly(&proc->syncRepLinks);
                                931                 :                : 
                                932                 :                :         /*
                                933                 :                :          * SyncRepWaitForLSN() reads syncRepState without holding the lock, so
                                934                 :                :          * make sure that it sees the queue link being removed before the
                                935                 :                :          * syncRepState change.
                                936                 :                :          */
 3168 heikki.linnakangas@i      937                 :             25 :         pg_write_barrier();
                                938                 :                : 
                                939                 :                :         /*
                                940                 :                :          * Set state to complete; see SyncRepWaitForLSN() for discussion of
                                941                 :                :          * the various states.
                                942                 :                :          */
 1152 andres@anarazel.de        943                 :             25 :         proc->syncRepState = SYNC_REP_WAIT_COMPLETE;
                                944                 :                : 
                                945                 :                :         /*
                                946                 :                :          * Wake only when we have set state and removed from queue.
                                947                 :                :          */
                                948                 :             25 :         SetLatch(&(proc->procLatch));
                                949                 :                : 
 5488 simon@2ndQuadrant.co      950                 :             25 :         numprocs++;
                                951                 :                :     }
                                952                 :                : 
                                953                 :            162 :     return numprocs;
                                954                 :                : }
                                955                 :                : 
                                956                 :                : /*
                                957                 :                :  * The checkpointer calls this as needed to update the shared
                                958                 :                :  * sync_standbys_status flag, so that backends don't remain permanently wedged
                                959                 :                :  * if synchronous_standby_names is unset.  It's safe to check the current value
                                960                 :                :  * without the lock, because it's only ever updated by one process.  But we
                                961                 :                :  * must take the lock to change it.
                                962                 :                :  */
                                963                 :                : void
 5477 rhaas@postgresql.org      964                 :            654 : SyncRepUpdateSyncStandbysDefined(void)
                                965                 :                : {
                                966   [ +  -  +  + ]:            654 :     bool        sync_standbys_defined = SyncStandbysDefined();
                                967                 :                : 
  338 michael@paquier.xyz       968                 :            654 :     if (sync_standbys_defined !=
                                969         [ +  + ]:            654 :         ((WalSndCtl->sync_standbys_status & SYNC_STANDBY_DEFINED) != 0))
                                970                 :                :     {
 5477 rhaas@postgresql.org      971                 :             14 :         LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
                                972                 :                : 
                                973                 :                :         /*
                                974                 :                :          * If synchronous_standby_names has been reset to empty, it's futile
                                975                 :                :          * for backends to continue waiting.  Since the user no longer wants
                                976                 :                :          * synchronous replication, we'd better wake them up.
                                977                 :                :          */
                                978         [ +  + ]:             14 :         if (!sync_standbys_defined)
                                979                 :                :         {
                                980                 :                :             int         i;
                                981                 :                : 
 5164 simon@2ndQuadrant.co      982         [ +  + ]:              4 :             for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
                                983                 :              3 :                 SyncRepWakeQueue(true, i);
                                984                 :                :         }
                                985                 :                : 
                                986                 :                :         /*
                                987                 :                :          * Only allow people to join the queue when there are synchronous
                                988                 :                :          * standbys defined.  Without this interlock, there's a race
                                989                 :                :          * condition: we might wake up all the current waiters; then, some
                                990                 :                :          * backend that hasn't yet reloaded its config might go to sleep on
                                991                 :                :          * the queue (and never wake up).  This prevents that.
                                992                 :                :          */
  338 michael@paquier.xyz       993         [ +  + ]:             14 :         WalSndCtl->sync_standbys_status = SYNC_STANDBY_INIT |
                                994                 :                :             (sync_standbys_defined ? SYNC_STANDBY_DEFINED : 0);
                                995                 :                : 
                                996                 :             14 :         LWLockRelease(SyncRepLock);
                                997                 :                :     }
                                998         [ +  + ]:            640 :     else if ((WalSndCtl->sync_standbys_status & SYNC_STANDBY_INIT) == 0)
                                999                 :                :     {
                               1000                 :            569 :         LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
                               1001                 :                : 
                               1002                 :                :         /*
                               1003                 :                :          * Note that there is no need to wake up the queues here.  We would
                               1004                 :                :          * reach this path only if SyncStandbysDefined() returns false, or it
                               1005                 :                :          * would mean that some backends are waiting with the GUC set.  See
                               1006                 :                :          * SyncRepWaitForLSN().
                               1007                 :                :          */
                               1008   [ +  -  -  + ]:            569 :         Assert(!SyncStandbysDefined());
                               1009                 :                : 
                               1010                 :                :         /*
                               1011                 :                :          * Even if there is no sync standby defined, let the readers of this
                               1012                 :                :          * information know that the sync standby data has been initialized.
                               1013                 :                :          * This can just be done once, hence the previous check on
                               1014                 :                :          * SYNC_STANDBY_INIT to avoid useless work.
                               1015                 :                :          */
                               1016                 :            569 :         WalSndCtl->sync_standbys_status |= SYNC_STANDBY_INIT;
                               1017                 :                : 
 5477 rhaas@postgresql.org     1018                 :            569 :         LWLockRelease(SyncRepLock);
                               1019                 :                :     }
                               1020                 :            654 : }
                               1021                 :                : 
                               1022                 :                : #ifdef USE_ASSERT_CHECKING
                               1023                 :                : static bool
 5164 simon@2ndQuadrant.co     1024                 :            200 : SyncRepQueueIsOrderedByLSN(int mode)
                               1025                 :                : {
                               1026                 :                :     XLogRecPtr  lastLSN;
                               1027                 :                :     dlist_iter  iter;
                               1028                 :                : 
                               1029   [ +  -  -  + ]:            200 :     Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
                               1030                 :                : 
   45 alvherre@kurilemu.de     1031                 :GNC         200 :     lastLSN = InvalidXLogRecPtr;
                               1032                 :                : 
 1152 andres@anarazel.de       1033   [ +  -  +  + ]:CBC         263 :     dlist_foreach(iter, &WalSndCtl->SyncRepQueue[mode])
                               1034                 :                :     {
                               1035                 :             63 :         PGPROC     *proc = dlist_container(PGPROC, syncRepLinks, iter.cur);
                               1036                 :                : 
                               1037                 :                :         /*
                               1038                 :                :          * Check the queue is ordered by LSN and that multiple procs don't
                               1039                 :                :          * have matching LSNs
                               1040                 :                :          */
 4825 alvherre@alvh.no-ip.     1041         [ -  + ]:             63 :         if (proc->waitLSN <= lastLSN)
 5488 simon@2ndQuadrant.co     1042                 :UBC           0 :             return false;
                               1043                 :                : 
 5488 simon@2ndQuadrant.co     1044                 :CBC          63 :         lastLSN = proc->waitLSN;
                               1045                 :                :     }
                               1046                 :                : 
                               1047                 :            200 :     return true;
                               1048                 :                : }
                               1049                 :                : #endif
                               1050                 :                : 
                               1051                 :                : /*
                               1052                 :                :  * ===========================================================
                               1053                 :                :  * Synchronous Replication functions executed by any process
                               1054                 :                :  * ===========================================================
                               1055                 :                :  */
                               1056                 :                : 
                               1057                 :                : bool
 5456 tgl@sss.pgh.pa.us        1058                 :           1272 : check_synchronous_standby_names(char **newval, void **extra, GucSource source)
                               1059                 :                : {
 3630 fujii@postgresql.org     1060   [ +  -  +  + ]:           1272 :     if (*newval != NULL && (*newval)[0] != '\0')
 5488 simon@2ndQuadrant.co     1061                 :             76 :     {
                               1062                 :                :         yyscan_t    scanner;
                               1063                 :                :         int         parse_rc;
                               1064                 :                :         SyncRepConfigData *pconf;
                               1065                 :                : 
                               1066                 :                :         /* Result of parsing is returned in one of these two variables */
  415 peter@eisentraut.org     1067                 :             76 :         SyncRepConfigData *syncrep_parse_result = NULL;
                               1068                 :             76 :         char       *syncrep_parse_error_msg = NULL;
                               1069                 :                : 
                               1070                 :                :         /* Parse the synchronous_standby_names string */
  468                          1071                 :             76 :         syncrep_scanner_init(*newval, &scanner);
  415                          1072                 :             76 :         parse_rc = syncrep_yyparse(&syncrep_parse_result, &syncrep_parse_error_msg, scanner);
  468                          1073                 :             76 :         syncrep_scanner_finish(scanner);
                               1074                 :                : 
 3609 tgl@sss.pgh.pa.us        1075   [ +  -  -  + ]:             76 :         if (parse_rc != 0 || syncrep_parse_result == NULL)
                               1076                 :                :         {
 3630 fujii@postgresql.org     1077                 :UBC           0 :             GUC_check_errcode(ERRCODE_SYNTAX_ERROR);
 3609 tgl@sss.pgh.pa.us        1078         [ #  # ]:              0 :             if (syncrep_parse_error_msg)
                               1079                 :              0 :                 GUC_check_errdetail("%s", syncrep_parse_error_msg);
                               1080                 :                :             else
                               1081                 :                :                 /* translator: %s is a GUC name */
  473 alvherre@alvh.no-ip.     1082                 :              0 :                 GUC_check_errdetail("\"%s\" parser failed.",
                               1083                 :                :                                     "synchronous_standby_names");
 3630 fujii@postgresql.org     1084                 :              0 :             return false;
                               1085                 :                :         }
                               1086                 :                : 
 3375 fujii@postgresql.org     1087         [ -  + ]:CBC          76 :         if (syncrep_parse_result->num_sync <= 0)
                               1088                 :                :         {
 3375 fujii@postgresql.org     1089                 :UBC           0 :             GUC_check_errmsg("number of synchronous standbys (%d) must be greater than zero",
                               1090                 :              0 :                              syncrep_parse_result->num_sync);
                               1091                 :              0 :             return false;
                               1092                 :                :         }
                               1093                 :                : 
                               1094                 :                :         /* GUC extra value must be guc_malloc'd, not palloc'd */
                               1095                 :                :         pconf = (SyncRepConfigData *)
 1248 tgl@sss.pgh.pa.us        1096                 :CBC          76 :             guc_malloc(LOG, syncrep_parse_result->config_size);
 3609                          1097         [ -  + ]:             76 :         if (pconf == NULL)
 3609 tgl@sss.pgh.pa.us        1098                 :UBC           0 :             return false;
 3609 tgl@sss.pgh.pa.us        1099                 :CBC          76 :         memcpy(pconf, syncrep_parse_result, syncrep_parse_result->config_size);
                               1100                 :                : 
  472 peter@eisentraut.org     1101                 :             76 :         *extra = pconf;
                               1102                 :                : 
                               1103                 :                :         /*
                               1104                 :                :          * We need not explicitly clean up syncrep_parse_result.  It, and any
                               1105                 :                :          * other cruft generated during parsing, will be freed when the
                               1106                 :                :          * current memory context is deleted.  (This code is generally run in
                               1107                 :                :          * a short-lived context used for config file processing, so that will
                               1108                 :                :          * not be very long.)
                               1109                 :                :          */
                               1110                 :                :     }
                               1111                 :                :     else
 3609 tgl@sss.pgh.pa.us        1112                 :           1196 :         *extra = NULL;
                               1113                 :                : 
 5456                          1114                 :           1272 :     return true;
                               1115                 :                : }
                               1116                 :                : 
                               1117                 :                : void
 3609                          1118                 :           1262 : assign_synchronous_standby_names(const char *newval, void *extra)
                               1119                 :                : {
                               1120                 :           1262 :     SyncRepConfig = (SyncRepConfigData *) extra;
                               1121                 :           1262 : }
                               1122                 :                : 
                               1123                 :                : void
 5164 simon@2ndQuadrant.co     1124                 :           1954 : assign_synchronous_commit(int newval, void *extra)
                               1125                 :                : {
                               1126   [ -  +  +  + ]:           1954 :     switch (newval)
                               1127                 :                :     {
 5164 simon@2ndQuadrant.co     1128                 :UBC           0 :         case SYNCHRONOUS_COMMIT_REMOTE_WRITE:
                               1129                 :              0 :             SyncRepWaitMode = SYNC_REP_WAIT_WRITE;
                               1130                 :              0 :             break;
 5164 simon@2ndQuadrant.co     1131                 :CBC        1323 :         case SYNCHRONOUS_COMMIT_REMOTE_FLUSH:
                               1132                 :           1323 :             SyncRepWaitMode = SYNC_REP_WAIT_FLUSH;
                               1133                 :           1323 :             break;
 3638 rhaas@postgresql.org     1134                 :              2 :         case SYNCHRONOUS_COMMIT_REMOTE_APPLY:
                               1135                 :              2 :             SyncRepWaitMode = SYNC_REP_WAIT_APPLY;
                               1136                 :              2 :             break;
 5164 simon@2ndQuadrant.co     1137                 :            629 :         default:
                               1138                 :            629 :             SyncRepWaitMode = SYNC_REP_NO_WAIT;
                               1139                 :            629 :             break;
                               1140                 :                :     }
                               1141                 :           1954 : }
        

Generated by: LCOV version 2.4-beta