PostgreSQL Source Code  git master
syncrep.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/xact.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "replication/walsender_private.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/ps_status.h"
Include dependency graph for syncrep.c:

Go to the source code of this file.

Macros

#define SyncStandbysDefined()   (SyncRepStandbyNames != NULL && SyncRepStandbyNames[0] != '\0')
 

Functions

static void SyncRepQueueInsert (int mode)
 
static void SyncRepCancelWait (void)
 
static int SyncRepWakeQueue (bool all, int mode)
 
static bool SyncRepGetSyncRecPtr (XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, bool *am_sync)
 
static void SyncRepGetOldestSyncRecPtr (XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys)
 
static void SyncRepGetNthLatestSyncRecPtr (XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys, uint8 nth)
 
static int SyncRepGetStandbyPriority (void)
 
static ListSyncRepGetSyncStandbysPriority (bool *am_sync)
 
static ListSyncRepGetSyncStandbysQuorum (bool *am_sync)
 
static int cmp_lsn (const void *a, const void *b)
 
void SyncRepWaitForLSN (XLogRecPtr lsn, bool commit)
 
void SyncRepCleanupAtProcExit (void)
 
void SyncRepInitConfig (void)
 
void SyncRepReleaseWaiters (void)
 
ListSyncRepGetSyncStandbys (bool *am_sync)
 
void SyncRepUpdateSyncStandbysDefined (void)
 
bool check_synchronous_standby_names (char **newval, void **extra, GucSource source)
 
void assign_synchronous_standby_names (const char *newval, void *extra)
 
void assign_synchronous_commit (int newval, void *extra)
 

Variables

char * SyncRepStandbyNames
 
static bool announce_next_takeover = true
 
SyncRepConfigDataSyncRepConfig = NULL
 
static int SyncRepWaitMode = SYNC_REP_NO_WAIT
 

Macro Definition Documentation

◆ SyncStandbysDefined

#define SyncStandbysDefined ( )    (SyncRepStandbyNames != NULL && SyncRepStandbyNames[0] != '\0')

Definition at line 92 of file syncrep.c.

Referenced by SyncRepGetStandbyPriority(), and SyncRepUpdateSyncStandbysDefined().

Function Documentation

◆ assign_synchronous_commit()

void assign_synchronous_commit ( int  newval,
void *  extra 
)

Definition at line 1197 of file syncrep.c.

References SYNC_REP_NO_WAIT, SYNC_REP_WAIT_APPLY, SYNC_REP_WAIT_FLUSH, SYNC_REP_WAIT_WRITE, SYNCHRONOUS_COMMIT_REMOTE_APPLY, SYNCHRONOUS_COMMIT_REMOTE_FLUSH, SYNCHRONOUS_COMMIT_REMOTE_WRITE, and SyncRepWaitMode.

1198 {
1199  switch (newval)
1200  {
1203  break;
1206  break;
1209  break;
1210  default:
1212  break;
1213  }
1214 }
#define SYNC_REP_NO_WAIT
Definition: syncrep.h:23
#define SYNC_REP_WAIT_APPLY
Definition: syncrep.h:26
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:25
#define newval
static int SyncRepWaitMode
Definition: syncrep.c:98
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24

◆ assign_synchronous_standby_names()

void assign_synchronous_standby_names ( const char *  newval,
void *  extra 
)

Definition at line 1191 of file syncrep.c.

1192 {
1193  SyncRepConfig = (SyncRepConfigData *) extra;
1194 }
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97

◆ check_synchronous_standby_names()

bool check_synchronous_standby_names ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 1134 of file syncrep.c.

References SyncRepConfigData::config_size, GUC_check_errcode(), GUC_check_errdetail, GUC_check_errmsg, malloc, SyncRepConfigData::num_sync, syncrep_parse_error_msg, syncrep_parse_result, syncrep_scanner_finish(), syncrep_scanner_init(), and syncrep_yyparse().

1135 {
1136  if (*newval != NULL && (*newval)[0] != '\0')
1137  {
1138  int parse_rc;
1139  SyncRepConfigData *pconf;
1140 
1141  /* Reset communication variables to ensure a fresh start */
1142  syncrep_parse_result = NULL;
1143  syncrep_parse_error_msg = NULL;
1144 
1145  /* Parse the synchronous_standby_names string */
1147  parse_rc = syncrep_yyparse();
1149 
1150  if (parse_rc != 0 || syncrep_parse_result == NULL)
1151  {
1152  GUC_check_errcode(ERRCODE_SYNTAX_ERROR);
1155  else
1156  GUC_check_errdetail("synchronous_standby_names parser failed");
1157  return false;
1158  }
1159 
1160  if (syncrep_parse_result->num_sync <= 0)
1161  {
1162  GUC_check_errmsg("number of synchronous standbys (%d) must be greater than zero",
1164  return false;
1165  }
1166 
1167  /* GUC extra value must be malloc'd, not palloc'd */
1168  pconf = (SyncRepConfigData *)
1170  if (pconf == NULL)
1171  return false;
1173 
1174  *extra = (void *) pconf;
1175 
1176  /*
1177  * We need not explicitly clean up syncrep_parse_result. It, and any
1178  * other cruft generated during parsing, will be freed when the
1179  * current memory context is deleted. (This code is generally run in
1180  * a short-lived context used for config file processing, so that will
1181  * not be very long.)
1182  */
1183  }
1184  else
1185  *extra = NULL;
1186 
1187  return true;
1188 }
void syncrep_scanner_finish(void)
void syncrep_scanner_init(const char *query_string)
#define GUC_check_errdetail
Definition: guc.h:409
char * syncrep_parse_error_msg
#define GUC_check_errmsg
Definition: guc.h:405
void GUC_check_errcode(int sqlerrcode)
Definition: guc.c:10677
#define malloc(a)
Definition: header.h:50
SyncRepConfigData * syncrep_parse_result
#define newval
int syncrep_yyparse(void)

◆ cmp_lsn()

static int cmp_lsn ( const void *  a,
const void *  b 
)
static

Definition at line 659 of file syncrep.c.

Referenced by SyncRepGetNthLatestSyncRecPtr().

660 {
661  XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
662  XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
663 
664  if (lsn1 > lsn2)
665  return -1;
666  else if (lsn1 == lsn2)
667  return 0;
668  else
669  return 1;
670 }
uint64 XLogRecPtr
Definition: xlogdefs.h:21

◆ SyncRepCancelWait()

static void SyncRepCancelWait ( void  )
static

Definition at line 352 of file syncrep.c.

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, SHMQueueDelete(), SHMQueueIsDetached(), SYNC_REP_NOT_WAITING, PGPROC::syncRepLinks, and PGPROC::syncRepState.

Referenced by SyncRepWaitForLSN().

353 {
354  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
358  LWLockRelease(SyncRepLock);
359 }
PGPROC * MyProc
Definition: proc.c:68
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
int syncRepState
Definition: proc.h:152

◆ SyncRepCleanupAtProcExit()

void SyncRepCleanupAtProcExit ( void  )

Definition at line 362 of file syncrep.c.

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, SHMQueueDelete(), SHMQueueIsDetached(), and PGPROC::syncRepLinks.

Referenced by ProcKill().

363 {
365  {
366  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
368  LWLockRelease(SyncRepLock);
369  }
370 }
PGPROC * MyProc
Definition: proc.c:68
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68

◆ SyncRepGetNthLatestSyncRecPtr()

static void SyncRepGetNthLatestSyncRecPtr ( XLogRecPtr writePtr,
XLogRecPtr flushPtr,
XLogRecPtr applyPtr,
List sync_standbys,
uint8  nth 
)
static

Definition at line 612 of file syncrep.c.

References WalSnd::apply, cmp_lsn(), WalSnd::flush, i, lfirst_int, list_length(), WalSnd::mutex, palloc(), pfree(), qsort, SpinLockAcquire, SpinLockRelease, WalSndCtl, WalSndCtlData::walsnds, and WalSnd::write.

Referenced by SyncRepGetSyncRecPtr().

614 {
615  ListCell *cell;
616  XLogRecPtr *write_array;
617  XLogRecPtr *flush_array;
618  XLogRecPtr *apply_array;
619  int len;
620  int i = 0;
621 
622  len = list_length(sync_standbys);
623  write_array = (XLogRecPtr *) palloc(sizeof(XLogRecPtr) * len);
624  flush_array = (XLogRecPtr *) palloc(sizeof(XLogRecPtr) * len);
625  apply_array = (XLogRecPtr *) palloc(sizeof(XLogRecPtr) * len);
626 
627  foreach(cell, sync_standbys)
628  {
629  WalSnd *walsnd = &WalSndCtl->walsnds[lfirst_int(cell)];
630 
631  SpinLockAcquire(&walsnd->mutex);
632  write_array[i] = walsnd->write;
633  flush_array[i] = walsnd->flush;
634  apply_array[i] = walsnd->apply;
635  SpinLockRelease(&walsnd->mutex);
636 
637  i++;
638  }
639 
640  /* Sort each array in descending order */
641  qsort(write_array, len, sizeof(XLogRecPtr), cmp_lsn);
642  qsort(flush_array, len, sizeof(XLogRecPtr), cmp_lsn);
643  qsort(apply_array, len, sizeof(XLogRecPtr), cmp_lsn);
644 
645  /* Get Nth latest Write, Flush, Apply positions */
646  *writePtr = write_array[nth - 1];
647  *flushPtr = flush_array[nth - 1];
648  *applyPtr = apply_array[nth - 1];
649 
650  pfree(write_array);
651  pfree(flush_array);
652  pfree(apply_array);
653 }
XLogRecPtr write
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
slock_t mutex
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
void pfree(void *pointer)
Definition: mcxt.c:1031
static int cmp_lsn(const void *a, const void *b)
Definition: syncrep.c:659
#define lfirst_int(lc)
Definition: pg_list.h:191
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
static int list_length(const List *l)
Definition: pg_list.h:169
void * palloc(Size size)
Definition: mcxt.c:924
int i
#define qsort(a, b, c, d)
Definition: port.h:488
XLogRecPtr apply

◆ SyncRepGetOldestSyncRecPtr()

static void SyncRepGetOldestSyncRecPtr ( XLogRecPtr writePtr,
XLogRecPtr flushPtr,
XLogRecPtr applyPtr,
List sync_standbys 
)
static

Definition at line 576 of file syncrep.c.

References WalSnd::apply, WalSnd::flush, lfirst_int, WalSnd::mutex, SpinLockAcquire, SpinLockRelease, WalSndCtl, WalSndCtlData::walsnds, write, WalSnd::write, and XLogRecPtrIsInvalid.

Referenced by SyncRepGetSyncRecPtr().

578 {
579  ListCell *cell;
580 
581  /*
582  * Scan through all sync standbys and calculate the oldest Write, Flush
583  * and Apply positions.
584  */
585  foreach(cell, sync_standbys)
586  {
587  WalSnd *walsnd = &WalSndCtl->walsnds[lfirst_int(cell)];
589  XLogRecPtr flush;
590  XLogRecPtr apply;
591 
592  SpinLockAcquire(&walsnd->mutex);
593  write = walsnd->write;
594  flush = walsnd->flush;
595  apply = walsnd->apply;
596  SpinLockRelease(&walsnd->mutex);
597 
598  if (XLogRecPtrIsInvalid(*writePtr) || *writePtr > write)
599  *writePtr = write;
600  if (XLogRecPtrIsInvalid(*flushPtr) || *flushPtr > flush)
601  *flushPtr = flush;
602  if (XLogRecPtrIsInvalid(*applyPtr) || *applyPtr > apply)
603  *applyPtr = apply;
604  }
605 }
XLogRecPtr write
#define write(a, b, c)
Definition: win32.h:14
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
slock_t mutex
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
#define lfirst_int(lc)
Definition: pg_list.h:191
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr apply

◆ SyncRepGetStandbyPriority()

static int SyncRepGetStandbyPriority ( void  )
static

Definition at line 943 of file syncrep.c.

References am_cascading_walsender, application_name, SyncRepConfigData::member_names, SyncRepConfigData::nmembers, pg_strcasecmp(), SYNC_REP_PRIORITY, SyncRepConfigData::syncrep_method, and SyncStandbysDefined.

Referenced by SyncRepInitConfig().

944 {
945  const char *standby_name;
946  int priority;
947  bool found = false;
948 
949  /*
950  * Since synchronous cascade replication is not allowed, we always set the
951  * priority of cascading walsender to zero.
952  */
954  return 0;
955 
956  if (!SyncStandbysDefined() || SyncRepConfig == NULL)
957  return 0;
958 
959  standby_name = SyncRepConfig->member_names;
960  for (priority = 1; priority <= SyncRepConfig->nmembers; priority++)
961  {
962  if (pg_strcasecmp(standby_name, application_name) == 0 ||
963  strcmp(standby_name, "*") == 0)
964  {
965  found = true;
966  break;
967  }
968  standby_name += strlen(standby_name) + 1;
969  }
970 
971  if (!found)
972  return 0;
973 
974  /*
975  * In quorum-based sync replication, all the standbys in the list have the
976  * same priority, one.
977  */
978  return (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY) ? priority : 1;
979 }
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:51
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
#define SyncStandbysDefined()
Definition: syncrep.c:92
char member_names[FLEXIBLE_ARRAY_MEMBER]
Definition: syncrep.h:54
char * application_name
Definition: guc.c:529
bool am_cascading_walsender
Definition: walsender.c:115

◆ SyncRepGetSyncRecPtr()

static bool SyncRepGetSyncRecPtr ( XLogRecPtr writePtr,
XLogRecPtr flushPtr,
XLogRecPtr applyPtr,
bool am_sync 
)
static

Definition at line 519 of file syncrep.c.

References InvalidXLogRecPtr, list_free(), list_length(), SyncRepConfigData::num_sync, SYNC_REP_PRIORITY, SyncRepConfigData::syncrep_method, SyncRepGetNthLatestSyncRecPtr(), SyncRepGetOldestSyncRecPtr(), and SyncRepGetSyncStandbys().

Referenced by SyncRepReleaseWaiters().

521 {
522  List *sync_standbys;
523 
524  *writePtr = InvalidXLogRecPtr;
525  *flushPtr = InvalidXLogRecPtr;
526  *applyPtr = InvalidXLogRecPtr;
527  *am_sync = false;
528 
529  /* Get standbys that are considered as synchronous at this moment */
530  sync_standbys = SyncRepGetSyncStandbys(am_sync);
531 
532  /*
533  * Quick exit if we are not managing a sync standby or there are not
534  * enough synchronous standbys.
535  */
536  if (!(*am_sync) ||
537  SyncRepConfig == NULL ||
538  list_length(sync_standbys) < SyncRepConfig->num_sync)
539  {
540  list_free(sync_standbys);
541  return false;
542  }
543 
544  /*
545  * In a priority-based sync replication, the synced positions are the
546  * oldest ones among sync standbys. In a quorum-based, they are the Nth
547  * latest ones.
548  *
549  * SyncRepGetNthLatestSyncRecPtr() also can calculate the oldest
550  * positions. But we use SyncRepGetOldestSyncRecPtr() for that calculation
551  * because it's a bit more efficient.
552  *
553  * XXX If the numbers of current and requested sync standbys are the same,
554  * we can use SyncRepGetOldestSyncRecPtr() to calculate the synced
555  * positions even in a quorum-based sync replication.
556  */
558  {
559  SyncRepGetOldestSyncRecPtr(writePtr, flushPtr, applyPtr,
560  sync_standbys);
561  }
562  else
563  {
564  SyncRepGetNthLatestSyncRecPtr(writePtr, flushPtr, applyPtr,
565  sync_standbys, SyncRepConfig->num_sync);
566  }
567 
568  list_free(sync_standbys);
569  return true;
570 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:51
static void SyncRepGetNthLatestSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys, uint8 nth)
Definition: syncrep.c:612
List * SyncRepGetSyncStandbys(bool *am_sync)
Definition: syncrep.c:681
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
static int list_length(const List *l)
Definition: pg_list.h:169
void list_free(List *list)
Definition: list.c:1373
static void SyncRepGetOldestSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys)
Definition: syncrep.c:576
Definition: pg_list.h:50

◆ SyncRepGetSyncStandbys()

List* SyncRepGetSyncStandbys ( bool am_sync)

Definition at line 681 of file syncrep.c.

References NIL, SYNC_REP_PRIORITY, SyncRepConfigData::syncrep_method, SyncRepGetSyncStandbysPriority(), and SyncRepGetSyncStandbysQuorum().

Referenced by pg_stat_get_wal_senders(), and SyncRepGetSyncRecPtr().

682 {
683  /* Set default result */
684  if (am_sync != NULL)
685  *am_sync = false;
686 
687  /* Quick exit if sync replication is not requested */
688  if (SyncRepConfig == NULL)
689  return NIL;
690 
694 }
#define NIL
Definition: pg_list.h:65
static List * SyncRepGetSyncStandbysPriority(bool *am_sync)
Definition: syncrep.c:773
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:51
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
static List * SyncRepGetSyncStandbysQuorum(bool *am_sync)
Definition: syncrep.c:707

◆ SyncRepGetSyncStandbysPriority()

static List * SyncRepGetSyncStandbysPriority ( bool am_sync)
static

Definition at line 773 of file syncrep.c.

References Assert, WalSnd::flush, foreach_delete_current, i, lappend_int(), lfirst_int, list_concat(), list_free(), list_length(), max_wal_senders, WalSnd::mutex, MyWalSnd, NIL, SyncRepConfigData::nmembers, SyncRepConfigData::num_sync, WalSnd::pid, SpinLockAcquire, SpinLockRelease, WalSnd::state, SYNC_REP_PRIORITY, WalSnd::sync_standby_priority, SyncRepConfigData::syncrep_method, WalSndCtl, WalSndCtlData::walsnds, WALSNDSTATE_STOPPING, WALSNDSTATE_STREAMING, and XLogRecPtrIsInvalid.

Referenced by SyncRepGetSyncStandbys().

774 {
775  List *result = NIL;
776  List *pending = NIL;
777  int lowest_priority;
778  int next_highest_priority;
779  int this_priority;
780  int priority;
781  int i;
782  bool am_in_pending = false;
783  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
784  * rearrangement */
785 
787 
788  lowest_priority = SyncRepConfig->nmembers;
789  next_highest_priority = lowest_priority + 1;
790 
791  /*
792  * Find the sync standbys which have the highest priority (i.e, 1). Also
793  * store all the other potential sync standbys into the pending list, in
794  * order to scan it later and find other sync standbys from it quickly.
795  */
796  for (i = 0; i < max_wal_senders; i++)
797  {
798  XLogRecPtr flush;
800  int pid;
801 
802  walsnd = &WalSndCtl->walsnds[i];
803 
804  SpinLockAcquire(&walsnd->mutex);
805  pid = walsnd->pid;
806  flush = walsnd->flush;
807  state = walsnd->state;
808  SpinLockRelease(&walsnd->mutex);
809 
810  /* Must be active */
811  if (pid == 0)
812  continue;
813 
814  /* Must be streaming or stopping */
815  if (state != WALSNDSTATE_STREAMING &&
816  state != WALSNDSTATE_STOPPING)
817  continue;
818 
819  /* Must be synchronous */
820  this_priority = walsnd->sync_standby_priority;
821  if (this_priority == 0)
822  continue;
823 
824  /* Must have a valid flush position */
825  if (XLogRecPtrIsInvalid(flush))
826  continue;
827 
828  /*
829  * If the priority is equal to 1, consider this standby as sync and
830  * append it to the result. Otherwise append this standby to the
831  * pending list to check if it's actually sync or not later.
832  */
833  if (this_priority == 1)
834  {
835  result = lappend_int(result, i);
836  if (am_sync != NULL && walsnd == MyWalSnd)
837  *am_sync = true;
838  if (list_length(result) == SyncRepConfig->num_sync)
839  {
840  list_free(pending);
841  return result; /* Exit if got enough sync standbys */
842  }
843  }
844  else
845  {
846  pending = lappend_int(pending, i);
847  if (am_sync != NULL && walsnd == MyWalSnd)
848  am_in_pending = true;
849 
850  /*
851  * Track the highest priority among the standbys in the pending
852  * list, in order to use it as the starting priority for later
853  * scan of the list. This is useful to find quickly the sync
854  * standbys from the pending list later because we can skip
855  * unnecessary scans for the unused priorities.
856  */
857  if (this_priority < next_highest_priority)
858  next_highest_priority = this_priority;
859  }
860  }
861 
862  /*
863  * Consider all pending standbys as sync if the number of them plus
864  * already-found sync ones is lower than the configuration requests.
865  */
866  if (list_length(result) + list_length(pending) <= SyncRepConfig->num_sync)
867  {
868  /*
869  * Set *am_sync to true if this walsender is in the pending list
870  * because all pending standbys are considered as sync.
871  */
872  if (am_sync != NULL && !(*am_sync))
873  *am_sync = am_in_pending;
874 
875  result = list_concat(result, pending);
876  list_free(pending);
877  return result;
878  }
879 
880  /*
881  * Find the sync standbys from the pending list.
882  */
883  priority = next_highest_priority;
884  while (priority <= lowest_priority)
885  {
886  ListCell *cell;
887 
888  next_highest_priority = lowest_priority + 1;
889 
890  foreach(cell, pending)
891  {
892  i = lfirst_int(cell);
893  walsnd = &WalSndCtl->walsnds[i];
894 
895  this_priority = walsnd->sync_standby_priority;
896  if (this_priority == priority)
897  {
898  result = lappend_int(result, i);
899  if (am_sync != NULL && walsnd == MyWalSnd)
900  *am_sync = true;
901 
902  /*
903  * We should always exit here after the scan of pending list
904  * starts because we know that the list has enough elements to
905  * reach SyncRepConfig->num_sync.
906  */
907  if (list_length(result) == SyncRepConfig->num_sync)
908  {
909  list_free(pending);
910  return result; /* Exit if got enough sync standbys */
911  }
912 
913  /*
914  * Remove the entry for this sync standby from the list to
915  * prevent us from looking at the same entry again.
916  */
917  pending = foreach_delete_current(pending, cell);
918 
919  continue; /* don't adjust next_highest_priority */
920  }
921 
922  if (this_priority < next_highest_priority)
923  next_highest_priority = this_priority;
924  }
925 
926  priority = next_highest_priority;
927  }
928 
929  /* never reached, but keep compiler quiet */
930  Assert(false);
931  return result;
932 }
#define NIL
Definition: pg_list.h:65
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:51
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
slock_t mutex
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
#define lfirst_int(lc)
Definition: pg_list.h:191
WalSndState state
List * lappend_int(List *list, int datum)
Definition: list.c:339
int max_wal_senders
Definition: walsender.c:120
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
WalSnd * MyWalSnd
Definition: walsender.c:111
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
int sync_standby_priority
Definition: regguts.h:298
static int list_length(const List *l)
Definition: pg_list.h:169
WalSndState
void list_free(List *list)
Definition: list.c:1373
int i
Definition: pg_list.h:50

◆ SyncRepGetSyncStandbysQuorum()

static List * SyncRepGetSyncStandbysQuorum ( bool am_sync)
static

Definition at line 707 of file syncrep.c.

References Assert, WalSnd::flush, i, lappend_int(), max_wal_senders, WalSnd::mutex, MyWalSnd, NIL, WalSnd::pid, SpinLockAcquire, SpinLockRelease, WalSnd::state, SYNC_REP_QUORUM, WalSnd::sync_standby_priority, SyncRepConfigData::syncrep_method, WalSndCtl, WalSndCtlData::walsnds, WALSNDSTATE_STOPPING, WALSNDSTATE_STREAMING, and XLogRecPtrIsInvalid.

Referenced by SyncRepGetSyncStandbys().

708 {
709  List *result = NIL;
710  int i;
711  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
712  * rearrangement */
713 
715 
716  for (i = 0; i < max_wal_senders; i++)
717  {
718  XLogRecPtr flush;
720  int pid;
721 
722  walsnd = &WalSndCtl->walsnds[i];
723 
724  SpinLockAcquire(&walsnd->mutex);
725  pid = walsnd->pid;
726  flush = walsnd->flush;
727  state = walsnd->state;
728  SpinLockRelease(&walsnd->mutex);
729 
730  /* Must be active */
731  if (pid == 0)
732  continue;
733 
734  /* Must be streaming or stopping */
735  if (state != WALSNDSTATE_STREAMING &&
736  state != WALSNDSTATE_STOPPING)
737  continue;
738 
739  /* Must be synchronous */
740  if (walsnd->sync_standby_priority == 0)
741  continue;
742 
743  /* Must have a valid flush position */
744  if (XLogRecPtrIsInvalid(flush))
745  continue;
746 
747  /*
748  * Consider this standby as a candidate for quorum sync standbys and
749  * append it to the result.
750  */
751  result = lappend_int(result, i);
752  if (am_sync != NULL && walsnd == MyWalSnd)
753  *am_sync = true;
754  }
755 
756  return result;
757 }
#define NIL
Definition: pg_list.h:65
uint8 syncrep_method
Definition: syncrep.h:51
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
slock_t mutex
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
WalSndState state
List * lappend_int(List *list, int datum)
Definition: list.c:339
int max_wal_senders
Definition: walsender.c:120
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
WalSnd * MyWalSnd
Definition: walsender.c:111
#define SYNC_REP_QUORUM
Definition: syncrep.h:37
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
int sync_standby_priority
Definition: regguts.h:298
WalSndState
int i
Definition: pg_list.h:50

◆ SyncRepInitConfig()

void SyncRepInitConfig ( void  )

Definition at line 383 of file syncrep.c.

References application_name, DEBUG1, ereport, errmsg(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyWalSnd, WalSnd::sync_standby_priority, and SyncRepGetStandbyPriority().

Referenced by StartLogicalReplication(), StartReplication(), WalSndLoop(), WalSndWaitForWal(), and WalSndWriteData().

384 {
385  int priority;
386 
387  /*
388  * Determine if we are a potential sync standby and remember the result
389  * for handling replies from standby.
390  */
391  priority = SyncRepGetStandbyPriority();
392  if (MyWalSnd->sync_standby_priority != priority)
393  {
394  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
395  MyWalSnd->sync_standby_priority = priority;
396  LWLockRelease(SyncRepLock);
397  ereport(DEBUG1,
398  (errmsg("standby \"%s\" now has synchronous standby priority %u",
399  application_name, priority)));
400  }
401 }
#define DEBUG1
Definition: elog.h:25
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ereport(elevel, rest)
Definition: elog.h:141
WalSnd * MyWalSnd
Definition: walsender.c:111
int sync_standby_priority
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
char * application_name
Definition: guc.c:529
int errmsg(const char *fmt,...)
Definition: elog.c:784
static int SyncRepGetStandbyPriority(void)
Definition: syncrep.c:943

◆ SyncRepQueueInsert()

static void SyncRepQueueInsert ( int  mode)
static

Definition at line 319 of file syncrep.c.

References Assert, mode, MyProc, NUM_SYNC_REP_WAIT_MODE, offsetof, SHMQueueInsertAfter(), SHMQueuePrev(), PGPROC::syncRepLinks, WalSndCtlData::SyncRepQueue, PGPROC::waitLSN, and WalSndCtl.

Referenced by SyncRepWaitForLSN().

320 {
321  PGPROC *proc;
322 
326  offsetof(PGPROC, syncRepLinks));
327 
328  while (proc)
329  {
330  /*
331  * Stop at the queue element that we should after to ensure the queue
332  * is ordered by LSN.
333  */
334  if (proc->waitLSN < MyProc->waitLSN)
335  break;
336 
338  &(proc->syncRepLinks),
339  offsetof(PGPROC, syncRepLinks));
340  }
341 
342  if (proc)
344  else
346 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
Pointer SHMQueuePrev(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:164
PGPROC * MyProc
Definition: proc.c:68
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
void SHMQueueInsertAfter(SHM_QUEUE *queue, SHM_QUEUE *elem)
Definition: shmqueue.c:108
SHM_QUEUE SyncRepQueue[NUM_SYNC_REP_WAIT_MODE]
#define Assert(condition)
Definition: c.h:732
SHM_QUEUE syncRepLinks
Definition: proc.h:153
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
Definition: proc.h:95
XLogRecPtr waitLSN
Definition: proc.h:151
#define offsetof(type, field)
Definition: c.h:655

◆ SyncRepReleaseWaiters()

void SyncRepReleaseWaiters ( void  )

Definition at line 411 of file syncrep.c.

References announce_next_takeover, application_name, DEBUG3, elog, ereport, errmsg(), WalSnd::flush, LOG, WalSndCtlData::lsn, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyWalSnd, WalSnd::state, SYNC_REP_PRIORITY, SYNC_REP_WAIT_APPLY, SYNC_REP_WAIT_FLUSH, SYNC_REP_WAIT_WRITE, WalSnd::sync_standby_priority, SyncRepConfigData::syncrep_method, SyncRepGetSyncRecPtr(), SyncRepWakeQueue(), WalSndCtl, WALSNDSTATE_STOPPING, WALSNDSTATE_STREAMING, and XLogRecPtrIsInvalid.

Referenced by ProcessStandbyReplyMessage().

412 {
413  volatile WalSndCtlData *walsndctl = WalSndCtl;
414  XLogRecPtr writePtr;
415  XLogRecPtr flushPtr;
416  XLogRecPtr applyPtr;
417  bool got_recptr;
418  bool am_sync;
419  int numwrite = 0;
420  int numflush = 0;
421  int numapply = 0;
422 
423  /*
424  * If this WALSender is serving a standby that is not on the list of
425  * potential sync standbys then we have nothing to do. If we are still
426  * starting up, still running base backup or the current flush position is
427  * still invalid, then leave quickly also. Streaming or stopping WAL
428  * senders are allowed to release waiters.
429  */
430  if (MyWalSnd->sync_standby_priority == 0 ||
434  {
435  announce_next_takeover = true;
436  return;
437  }
438 
439  /*
440  * We're a potential sync standby. Release waiters if there are enough
441  * sync standbys and we are considered as sync.
442  */
443  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
444 
445  /*
446  * Check whether we are a sync standby or not, and calculate the synced
447  * positions among all sync standbys.
448  */
449  got_recptr = SyncRepGetSyncRecPtr(&writePtr, &flushPtr, &applyPtr, &am_sync);
450 
451  /*
452  * If we are managing a sync standby, though we weren't prior to this,
453  * then announce we are now a sync standby.
454  */
455  if (announce_next_takeover && am_sync)
456  {
457  announce_next_takeover = false;
458 
460  ereport(LOG,
461  (errmsg("standby \"%s\" is now a synchronous standby with priority %u",
463  else
464  ereport(LOG,
465  (errmsg("standby \"%s\" is now a candidate for quorum synchronous standby",
466  application_name)));
467  }
468 
469  /*
470  * If the number of sync standbys is less than requested or we aren't
471  * managing a sync standby then just leave.
472  */
473  if (!got_recptr || !am_sync)
474  {
475  LWLockRelease(SyncRepLock);
476  announce_next_takeover = !am_sync;
477  return;
478  }
479 
480  /*
481  * Set the lsn first so that when we wake backends they will release up to
482  * this location.
483  */
484  if (walsndctl->lsn[SYNC_REP_WAIT_WRITE] < writePtr)
485  {
486  walsndctl->lsn[SYNC_REP_WAIT_WRITE] = writePtr;
487  numwrite = SyncRepWakeQueue(false, SYNC_REP_WAIT_WRITE);
488  }
489  if (walsndctl->lsn[SYNC_REP_WAIT_FLUSH] < flushPtr)
490  {
491  walsndctl->lsn[SYNC_REP_WAIT_FLUSH] = flushPtr;
492  numflush = SyncRepWakeQueue(false, SYNC_REP_WAIT_FLUSH);
493  }
494  if (walsndctl->lsn[SYNC_REP_WAIT_APPLY] < applyPtr)
495  {
496  walsndctl->lsn[SYNC_REP_WAIT_APPLY] = applyPtr;
497  numapply = SyncRepWakeQueue(false, SYNC_REP_WAIT_APPLY);
498  }
499 
500  LWLockRelease(SyncRepLock);
501 
502  elog(DEBUG3, "released %d procs up to write %X/%X, %d procs up to flush %X/%X, %d procs up to apply %X/%X",
503  numwrite, (uint32) (writePtr >> 32), (uint32) writePtr,
504  numflush, (uint32) (flushPtr >> 32), (uint32) flushPtr,
505  numapply, (uint32) (applyPtr >> 32), (uint32) applyPtr);
506 }
static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, bool *am_sync)
Definition: syncrep.c:519
#define DEBUG3
Definition: elog.h:23
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:51
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
#define LOG
Definition: elog.h:26
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:990
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
XLogRecPtr flush
#define SYNC_REP_WAIT_APPLY
Definition: syncrep.h:26
static bool announce_next_takeover
Definition: syncrep.c:95
WalSndState state
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:25
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
WalSnd * MyWalSnd
Definition: walsender.c:111
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int sync_standby_priority
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
XLogRecPtr lsn[NUM_SYNC_REP_WAIT_MODE]
char * application_name
Definition: guc.c:529
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24

◆ SyncRepUpdateSyncStandbysDefined()

void SyncRepUpdateSyncStandbysDefined ( void  )

Definition at line 1058 of file syncrep.c.

References Assert, i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), mode, NUM_SYNC_REP_WAIT_MODE, offsetof, SHMQueueNext(), WalSndCtlData::sync_standbys_defined, PGPROC::syncRepLinks, WalSndCtlData::SyncRepQueue, SyncRepWakeQueue(), SyncStandbysDefined, PGPROC::waitLSN, and WalSndCtl.

Referenced by UpdateSharedMemoryConfig().

1059 {
1060  bool sync_standbys_defined = SyncStandbysDefined();
1061 
1062  if (sync_standbys_defined != WalSndCtl->sync_standbys_defined)
1063  {
1064  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
1065 
1066  /*
1067  * If synchronous_standby_names has been reset to empty, it's futile
1068  * for backends to continue to waiting. Since the user no longer
1069  * wants synchronous replication, we'd better wake them up.
1070  */
1071  if (!sync_standbys_defined)
1072  {
1073  int i;
1074 
1075  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
1076  SyncRepWakeQueue(true, i);
1077  }
1078 
1079  /*
1080  * Only allow people to join the queue when there are synchronous
1081  * standbys defined. Without this interlock, there's a race
1082  * condition: we might wake up all the current waiters; then, some
1083  * backend that hasn't yet reloaded its config might go to sleep on
1084  * the queue (and never wake up). This prevents that.
1085  */
1086  WalSndCtl->sync_standbys_defined = sync_standbys_defined;
1087 
1088  LWLockRelease(SyncRepLock);
1089  }
1090 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:990
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define SyncStandbysDefined()
Definition: syncrep.c:92
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int i
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28

◆ SyncRepWaitForLSN()

void SyncRepWaitForLSN ( XLogRecPtr  lsn,
bool  commit 
)

Definition at line 146 of file syncrep.c.

References Assert, DestNone, ereport, errcode(), errdetail(), errmsg(), get_ps_display(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), Min, mode, MyLatch, MyProc, palloc(), pfree(), pg_read_barrier, ProcDiePending, QueryCancelPending, ResetLatch(), set_ps_display(), SHMQueueIsDetached(), sprintf, SYNC_REP_NOT_WAITING, SYNC_REP_WAIT_COMPLETE, SYNC_REP_WAIT_FLUSH, SYNC_REP_WAITING, WalSndCtlData::sync_standbys_defined, SyncRepCancelWait(), PGPROC::syncRepLinks, SyncRepQueueInsert(), SyncRepRequested, PGPROC::syncRepState, SyncRepWaitMode, update_process_title, WAIT_EVENT_SYNC_REP, WaitLatch(), PGPROC::waitLSN, WalSndCtl, WARNING, whereToSendOutput, WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by EndPrepare(), RecordTransactionAbortPrepared(), RecordTransactionCommit(), and RecordTransactionCommitPrepared().

147 {
148  char *new_status = NULL;
149  const char *old_status;
150  int mode;
151 
152  /* Cap the level for anything other than commit to remote flush only. */
153  if (commit)
154  mode = SyncRepWaitMode;
155  else
157 
158  /*
159  * Fast exit if user has not requested sync replication.
160  */
161  if (!SyncRepRequested())
162  return;
163 
165  Assert(WalSndCtl != NULL);
166 
167  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
169 
170  /*
171  * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is not
172  * set. See SyncRepUpdateSyncStandbysDefined.
173  *
174  * Also check that the standby hasn't already replied. Unlikely race
175  * condition but we'll be fetching that cache line anyway so it's likely
176  * to be a low cost check.
177  */
179  lsn <= WalSndCtl->lsn[mode])
180  {
181  LWLockRelease(SyncRepLock);
182  return;
183  }
184 
185  /*
186  * Set our waitLSN so WALSender will know when to wake us, and add
187  * ourselves to the queue.
188  */
189  MyProc->waitLSN = lsn;
191  SyncRepQueueInsert(mode);
192  Assert(SyncRepQueueIsOrderedByLSN(mode));
193  LWLockRelease(SyncRepLock);
194 
195  /* Alter ps display to show waiting for sync rep. */
197  {
198  int len;
199 
200  old_status = get_ps_display(&len);
201  new_status = (char *) palloc(len + 32 + 1);
202  memcpy(new_status, old_status, len);
203  sprintf(new_status + len, " waiting for %X/%X",
204  (uint32) (lsn >> 32), (uint32) lsn);
205  set_ps_display(new_status, false);
206  new_status[len] = '\0'; /* truncate off " waiting ..." */
207  }
208 
209  /*
210  * Wait for specified LSN to be confirmed.
211  *
212  * Each proc has its own wait latch, so we perform a normal latch
213  * check/wait loop here.
214  */
215  for (;;)
216  {
217  int rc;
218 
219  /* Must reset the latch before testing state. */
221 
222  /*
223  * Acquiring the lock is not needed, the latch ensures proper
224  * barriers. If it looks like we're done, we must really be done,
225  * because once walsender changes the state to SYNC_REP_WAIT_COMPLETE,
226  * it will never update it again, so we can't be seeing a stale value
227  * in that case.
228  */
230  break;
231 
232  /*
233  * If a wait for synchronous replication is pending, we can neither
234  * acknowledge the commit nor raise ERROR or FATAL. The latter would
235  * lead the client to believe that the transaction aborted, which is
236  * not true: it's already committed locally. The former is no good
237  * either: the client has requested synchronous replication, and is
238  * entitled to assume that an acknowledged commit is also replicated,
239  * which might not be true. So in this case we issue a WARNING (which
240  * some clients may be able to interpret) and shut off further output.
241  * We do NOT reset ProcDiePending, so that the process will die after
242  * the commit is cleaned up.
243  */
244  if (ProcDiePending)
245  {
247  (errcode(ERRCODE_ADMIN_SHUTDOWN),
248  errmsg("canceling the wait for synchronous replication and terminating connection due to administrator command"),
249  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
252  break;
253  }
254 
255  /*
256  * It's unclear what to do if a query cancel interrupt arrives. We
257  * can't actually abort at this point, but ignoring the interrupt
258  * altogether is not helpful, so we just terminate the wait with a
259  * suitable warning.
260  */
261  if (QueryCancelPending)
262  {
263  QueryCancelPending = false;
265  (errmsg("canceling wait for synchronous replication due to user request"),
266  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
268  break;
269  }
270 
271  /*
272  * Wait on latch. Any condition that should wake us up will set the
273  * latch, so no need for timeout.
274  */
277 
278  /*
279  * If the postmaster dies, we'll probably never get an acknowledgment,
280  * because all the wal sender processes will exit. So just bail out.
281  */
282  if (rc & WL_POSTMASTER_DEATH)
283  {
284  ProcDiePending = true;
287  break;
288  }
289  }
290 
291  /*
292  * WalSender has checked our LSN and has removed us from queue. Clean up
293  * state and leave. It's OK to reset these shared memory fields without
294  * holding SyncRepLock, because any walsenders will ignore us anyway when
295  * we're not on the queue. We need a read barrier to make sure we see the
296  * changes to the queue link (this might be unnecessary without
297  * assertions, but better safe than sorry).
298  */
299  pg_read_barrier();
302  MyProc->waitLSN = 0;
303 
304  if (new_status)
305  {
306  /* Reset ps display */
307  set_ps_display(new_status, false);
308  pfree(new_status);
309  }
310 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static void SyncRepCancelWait(void)
Definition: syncrep.c:352
volatile sig_atomic_t QueryCancelPending
Definition: globals.c:31
bool update_process_title
Definition: ps_status.c:35
static void SyncRepQueueInsert(int mode)
Definition: syncrep.c:319
PGPROC * MyProc
Definition: proc.c:68
#define SYNC_REP_WAITING
Definition: syncrep.h:32
#define Min(x, y)
Definition: c.h:904
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:331
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
int errcode(int sqlerrcode)
Definition: elog.c:570
void ResetLatch(Latch *latch)
Definition: latch.c:519
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define sprintf
Definition: port.h:194
void pfree(void *pointer)
Definition: mcxt.c:1031
Definition: dest.h:88
const char * get_ps_display(int *displen)
Definition: ps_status.c:412
int errdetail(const char *fmt,...)
Definition: elog.c:860
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:25
#define SyncRepRequested()
Definition: syncrep.h:19
#define WARNING
Definition: elog.h:40
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define SYNC_REP_WAIT_COMPLETE
Definition: syncrep.h:33
#define Assert(condition)
Definition: c.h:732
volatile sig_atomic_t ProcDiePending
Definition: globals.c:32
#define pg_read_barrier()
Definition: atomics.h:161
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:784
struct Latch * MyLatch
Definition: globals.c:54
int syncRepState
Definition: proc.h:152
CommandDest whereToSendOutput
Definition: postgres.c:90
static int SyncRepWaitMode
Definition: syncrep.c:98
#define WL_LATCH_SET
Definition: latch.h:124
XLogRecPtr waitLSN
Definition: proc.h:151

◆ SyncRepWakeQueue()

static int SyncRepWakeQueue ( bool  all,
int  mode 
)
static

Definition at line 990 of file syncrep.c.

References Assert, WalSndCtlData::lsn, mode, NUM_SYNC_REP_WAIT_MODE, offsetof, pg_write_barrier, PGPROC::procLatch, SetLatch(), SHMQueueDelete(), SHMQueueNext(), SYNC_REP_WAIT_COMPLETE, PGPROC::syncRepLinks, WalSndCtlData::SyncRepQueue, PGPROC::syncRepState, PGPROC::waitLSN, and WalSndCtl.

Referenced by SyncRepReleaseWaiters(), and SyncRepUpdateSyncStandbysDefined().

991 {
992  volatile WalSndCtlData *walsndctl = WalSndCtl;
993  PGPROC *proc = NULL;
994  PGPROC *thisproc = NULL;
995  int numprocs = 0;
996 
998  Assert(SyncRepQueueIsOrderedByLSN(mode));
999 
1000  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1002  offsetof(PGPROC, syncRepLinks));
1003 
1004  while (proc)
1005  {
1006  /*
1007  * Assume the queue is ordered by LSN
1008  */
1009  if (!all && walsndctl->lsn[mode] < proc->waitLSN)
1010  return numprocs;
1011 
1012  /*
1013  * Move to next proc, so we can delete thisproc from the queue.
1014  * thisproc is valid, proc may be NULL after this.
1015  */
1016  thisproc = proc;
1017  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1018  &(proc->syncRepLinks),
1019  offsetof(PGPROC, syncRepLinks));
1020 
1021  /*
1022  * Remove thisproc from queue.
1023  */
1024  SHMQueueDelete(&(thisproc->syncRepLinks));
1025 
1026  /*
1027  * SyncRepWaitForLSN() reads syncRepState without holding the lock, so
1028  * make sure that it sees the queue link being removed before the
1029  * syncRepState change.
1030  */
1031  pg_write_barrier();
1032 
1033  /*
1034  * Set state to complete; see SyncRepWaitForLSN() for discussion of
1035  * the various states.
1036  */
1038 
1039  /*
1040  * Wake only when we have set state and removed from queue.
1041  */
1042  SetLatch(&(thisproc->procLatch));
1043 
1044  numprocs++;
1045  }
1046 
1047  return numprocs;
1048 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
void SetLatch(Latch *latch)
Definition: latch.c:436
Latch procLatch
Definition: proc.h:104
SHM_QUEUE SyncRepQueue[NUM_SYNC_REP_WAIT_MODE]
Pointer SHMQueueNext(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:145
#define SYNC_REP_WAIT_COMPLETE
Definition: syncrep.h:33
#define Assert(condition)
Definition: c.h:732
SHM_QUEUE syncRepLinks
Definition: proc.h:153
XLogRecPtr lsn[NUM_SYNC_REP_WAIT_MODE]
#define pg_write_barrier()
Definition: atomics.h:162
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
int syncRepState
Definition: proc.h:152
Definition: proc.h:95
XLogRecPtr waitLSN
Definition: proc.h:151
#define offsetof(type, field)
Definition: c.h:655

Variable Documentation

◆ announce_next_takeover

bool announce_next_takeover = true
static

Definition at line 95 of file syncrep.c.

Referenced by SyncRepReleaseWaiters().

◆ SyncRepConfig

SyncRepConfigData* SyncRepConfig = NULL

Definition at line 97 of file syncrep.c.

Referenced by pg_stat_get_wal_senders().

◆ SyncRepStandbyNames

char* SyncRepStandbyNames

Definition at line 90 of file syncrep.c.

◆ SyncRepWaitMode

int SyncRepWaitMode = SYNC_REP_NO_WAIT
static

Definition at line 98 of file syncrep.c.

Referenced by assign_synchronous_commit(), and SyncRepWaitForLSN().