PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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

Function Documentation

void assign_synchronous_commit ( int  newval,
void *  extra 
)

Definition at line 1203 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.

1204 {
1205  switch (newval)
1206  {
1209  break;
1212  break;
1215  break;
1216  default:
1218  break;
1219  }
1220 }
#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
void assign_synchronous_standby_names ( const char *  newval,
void *  extra 
)

Definition at line 1197 of file syncrep.c.

1198 {
1199  SyncRepConfig = (SyncRepConfigData *) extra;
1200 }
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
bool check_synchronous_standby_names ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 1140 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().

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

Definition at line 658 of file syncrep.c.

Referenced by SyncRepGetNthLatestSyncRecPtr().

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

Definition at line 353 of file syncrep.c.

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

Referenced by SyncRepWaitForLSN().

354 {
355  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
359  LWLockRelease(SyncRepLock);
360 }
PGPROC * MyProc
Definition: proc.c:67
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
SHM_QUEUE syncRepLinks
Definition: proc.h:150
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
int syncRepState
Definition: proc.h:149
void SyncRepCleanupAtProcExit ( void  )

Definition at line 363 of file syncrep.c.

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

Referenced by ProcKill().

364 {
366  {
367  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
369  LWLockRelease(SyncRepLock);
370  }
371 }
PGPROC * MyProc
Definition: proc.c:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
SHM_QUEUE syncRepLinks
Definition: proc.h:150
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
static void SyncRepGetNthLatestSyncRecPtr ( XLogRecPtr writePtr,
XLogRecPtr flushPtr,
XLogRecPtr applyPtr,
List sync_standbys,
uint8  nth 
)
static

Definition at line 611 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().

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

Definition at line 575 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().

577 {
578  ListCell *cell;
579 
580  /*
581  * Scan through all sync standbys and calculate the oldest Write, Flush
582  * and Apply positions.
583  */
584  foreach(cell, sync_standbys)
585  {
586  WalSnd *walsnd = &WalSndCtl->walsnds[lfirst_int(cell)];
588  XLogRecPtr flush;
589  XLogRecPtr apply;
590 
591  SpinLockAcquire(&walsnd->mutex);
592  write = walsnd->write;
593  flush = walsnd->flush;
594  apply = walsnd->apply;
595  SpinLockRelease(&walsnd->mutex);
596 
597  if (XLogRecPtrIsInvalid(*writePtr) || *writePtr > write)
598  *writePtr = write;
599  if (XLogRecPtrIsInvalid(*flushPtr) || *flushPtr > flush)
600  *flushPtr = flush;
601  if (XLogRecPtrIsInvalid(*applyPtr) || *applyPtr > apply)
602  *applyPtr = apply;
603  }
604 }
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:107
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr apply
static int SyncRepGetStandbyPriority ( void  )
static

Definition at line 949 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().

950 {
951  const char *standby_name;
952  int priority;
953  bool found = false;
954 
955  /*
956  * Since synchronous cascade replication is not allowed, we always set the
957  * priority of cascading walsender to zero.
958  */
960  return 0;
961 
962  if (!SyncStandbysDefined() || SyncRepConfig == NULL)
963  return 0;
964 
965  standby_name = SyncRepConfig->member_names;
966  for (priority = 1; priority <= SyncRepConfig->nmembers; priority++)
967  {
968  if (pg_strcasecmp(standby_name, application_name) == 0 ||
969  strcmp(standby_name, "*") == 0)
970  {
971  found = true;
972  break;
973  }
974  standby_name += strlen(standby_name) + 1;
975  }
976 
977  if (!found)
978  return 0;
979 
980  /*
981  * In quorum-based sync replication, all the standbys in the list have the
982  * same priority, one.
983  */
984  return (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY) ? priority : 1;
985 }
#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:469
bool am_cascading_walsender
Definition: walsender.c:115
static bool SyncRepGetSyncRecPtr ( XLogRecPtr writePtr,
XLogRecPtr flushPtr,
XLogRecPtr applyPtr,
bool am_sync 
)
static

Definition at line 518 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().

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

Definition at line 680 of file syncrep.c.

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

Referenced by pg_stat_get_wal_senders(), and SyncRepGetSyncRecPtr().

681 {
682  /* Set default result */
683  if (am_sync != NULL)
684  *am_sync = false;
685 
686  /* Quick exit if sync replication is not requested */
687  if (SyncRepConfig == NULL)
688  return NIL;
689 
693 }
#define NIL
Definition: pg_list.h:69
static List * SyncRepGetSyncStandbysPriority(bool *am_sync)
Definition: syncrep.c:771
#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:706
static List * SyncRepGetSyncStandbysPriority ( bool am_sync)
static

Definition at line 771 of file syncrep.c.

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

Referenced by SyncRepGetSyncStandbys().

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

Definition at line 706 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_STREAMING, and XLogRecPtrIsInvalid.

Referenced by SyncRepGetSyncStandbys().

707 {
708  List *result = NIL;
709  int i;
710  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
711  * rearrangement */
712 
714 
715  for (i = 0; i < max_wal_senders; i++)
716  {
717  XLogRecPtr flush;
719  int pid;
720 
721  walsnd = &WalSndCtl->walsnds[i];
722 
723  SpinLockAcquire(&walsnd->mutex);
724  pid = walsnd->pid;
725  flush = walsnd->flush;
726  state = walsnd->state;
727  SpinLockRelease(&walsnd->mutex);
728 
729  /* Must be active */
730  if (pid == 0)
731  continue;
732 
733  /* Must be streaming */
734  if (state != WALSNDSTATE_STREAMING)
735  continue;
736 
737  /* Must be synchronous */
738  if (walsnd->sync_standby_priority == 0)
739  continue;
740 
741  /* Must have a valid flush position */
742  if (XLogRecPtrIsInvalid(flush))
743  continue;
744 
745  /*
746  * Consider this standby as a candidate for quorum sync standbys and
747  * append it to the result.
748  */
749  result = lappend_int(result, i);
750  if (am_sync != NULL && walsnd == MyWalSnd)
751  *am_sync = true;
752  }
753 
754  return result;
755 }
#define NIL
Definition: pg_list.h:69
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:146
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:681
int sync_standby_priority
Definition: regguts.h:298
WalSndState
int i
Definition: pg_list.h:45
void SyncRepInitConfig ( void  )

Definition at line 384 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().

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

Definition at line 320 of file syncrep.c.

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

Referenced by SyncRepWaitForLSN().

321 {
322  PGPROC *proc;
323 
324  Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
325  proc = (PGPROC *) SHMQueuePrev(&(WalSndCtl->SyncRepQueue[mode]),
326  &(WalSndCtl->SyncRepQueue[mode]),
327  offsetof(PGPROC, syncRepLinks));
328 
329  while (proc)
330  {
331  /*
332  * Stop at the queue element that we should after to ensure the queue
333  * is ordered by LSN.
334  */
335  if (proc->waitLSN < MyProc->waitLSN)
336  break;
337 
338  proc = (PGPROC *) SHMQueuePrev(&(WalSndCtl->SyncRepQueue[mode]),
339  &(proc->syncRepLinks),
340  offsetof(PGPROC, syncRepLinks));
341  }
342 
343  if (proc)
345  else
347 }
Pointer SHMQueuePrev(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:164
PGPROC * MyProc
Definition: proc.c:67
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:681
SHM_QUEUE syncRepLinks
Definition: proc.h:150
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
Definition: proc.h:95
XLogRecPtr waitLSN
Definition: proc.h:148
#define offsetof(type, field)
Definition: c.h:549
void SyncRepReleaseWaiters ( void  )

Definition at line 412 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_STREAMING, and XLogRecPtrIsInvalid.

Referenced by ProcessStandbyReplyMessage().

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

Definition at line 1064 of file syncrep.c.

References i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), NUM_SYNC_REP_WAIT_MODE, WalSndCtlData::sync_standbys_defined, SyncRepWakeQueue(), SyncStandbysDefined, and WalSndCtl.

Referenced by UpdateSharedMemoryConfig().

1065 {
1066  bool sync_standbys_defined = SyncStandbysDefined();
1067 
1068  if (sync_standbys_defined != WalSndCtl->sync_standbys_defined)
1069  {
1070  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
1071 
1072  /*
1073  * If synchronous_standby_names has been reset to empty, it's futile
1074  * for backends to continue to waiting. Since the user no longer
1075  * wants synchronous replication, we'd better wake them up.
1076  */
1077  if (!sync_standbys_defined)
1078  {
1079  int i;
1080 
1081  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
1082  SyncRepWakeQueue(true, i);
1083  }
1084 
1085  /*
1086  * Only allow people to join the queue when there are synchronous
1087  * standbys defined. Without this interlock, there's a race
1088  * condition: we might wake up all the current waiters; then, some
1089  * backend that hasn't yet reloaded its config might go to sleep on
1090  * the queue (and never wake up). This prevents that.
1091  */
1092  WalSndCtl->sync_standbys_defined = sync_standbys_defined;
1093 
1094  LWLockRelease(SyncRepLock);
1095  }
1096 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:996
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define SyncStandbysDefined()
Definition: syncrep.c:92
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
int i
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
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, MyLatch, MyProc, palloc(), pfree(), pg_read_barrier, PostmasterIsAlive(), ProcDiePending, QueryCancelPending, ResetLatch(), set_ps_display(), SHMQueueIsDetached(), 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, SyncStandbysDefined, 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, or there are no
160  * sync replication standby names defined. Note that those standbys don't
161  * need to be connected.
162  */
164  return;
165 
167  Assert(WalSndCtl != NULL);
168 
169  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
171 
172  /*
173  * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is not
174  * set. See SyncRepUpdateSyncStandbysDefined.
175  *
176  * Also check that the standby hasn't already replied. Unlikely race
177  * condition but we'll be fetching that cache line anyway so it's likely
178  * to be a low cost check.
179  */
181  lsn <= WalSndCtl->lsn[mode])
182  {
183  LWLockRelease(SyncRepLock);
184  return;
185  }
186 
187  /*
188  * Set our waitLSN so WALSender will know when to wake us, and add
189  * ourselves to the queue.
190  */
191  MyProc->waitLSN = lsn;
193  SyncRepQueueInsert(mode);
194  Assert(SyncRepQueueIsOrderedByLSN(mode));
195  LWLockRelease(SyncRepLock);
196 
197  /* Alter ps display to show waiting for sync rep. */
199  {
200  int len;
201 
202  old_status = get_ps_display(&len);
203  new_status = (char *) palloc(len + 32 + 1);
204  memcpy(new_status, old_status, len);
205  sprintf(new_status + len, " waiting for %X/%X",
206  (uint32) (lsn >> 32), (uint32) lsn);
207  set_ps_display(new_status, false);
208  new_status[len] = '\0'; /* truncate off " waiting ..." */
209  }
210 
211  /*
212  * Wait for specified LSN to be confirmed.
213  *
214  * Each proc has its own wait latch, so we perform a normal latch
215  * check/wait loop here.
216  */
217  for (;;)
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  * If the postmaster dies, we'll probably never get an
273  * acknowledgement, because all the wal sender processes will exit. So
274  * just bail out.
275  */
276  if (!PostmasterIsAlive())
277  {
278  ProcDiePending = true;
281  break;
282  }
283 
284  /*
285  * Wait on latch. Any condition that should wake us up will set the
286  * latch, so no need for timeout.
287  */
290  }
291 
292  /*
293  * WalSender has checked our LSN and has removed us from queue. Clean up
294  * state and leave. It's OK to reset these shared memory fields without
295  * holding SyncRepLock, because any walsenders will ignore us anyway when
296  * we're not on the queue. We need a read barrier to make sure we see the
297  * changes to the queue link (this might be unnecessary without
298  * assertions, but better safe than sorry).
299  */
300  pg_read_barrier();
303  MyProc->waitLSN = 0;
304 
305  if (new_status)
306  {
307  /* Reset ps display */
308  set_ps_display(new_status, false);
309  pfree(new_status);
310  }
311 }
static void SyncRepCancelWait(void)
Definition: syncrep.c:353
bool update_process_title
Definition: ps_status.c:35
static void SyncRepQueueInsert(int mode)
Definition: syncrep.c:320
PGPROC * MyProc
Definition: proc.c:67
#define SYNC_REP_WAITING
Definition: syncrep.h:32
#define Min(x, y)
Definition: c.h:812
void set_ps_display(const char *activity, bool force)
Definition: ps_status.c:326
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
int errcode(int sqlerrcode)
Definition: elog.c:575
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
volatile bool QueryCancelPending
Definition: globals.c:30
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
void pfree(void *pointer)
Definition: mcxt.c:949
Definition: dest.h:88
const char * get_ps_display(int *displen)
Definition: ps_status.c:405
#define SyncStandbysDefined()
Definition: syncrep.c:92
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
int errdetail(const char *fmt,...)
Definition: elog.c:873
unsigned int uint32
Definition: c.h:258
#define ereport(elevel, rest)
Definition: elog.h:122
#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:681
#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:1117
SHM_QUEUE syncRepLinks
Definition: proc.h:150
volatile bool ProcDiePending
Definition: globals.c:31
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:52
int syncRepState
Definition: proc.h:149
CommandDest whereToSendOutput
Definition: postgres.c:88
static int SyncRepWaitMode
Definition: syncrep.c:98
#define WL_LATCH_SET
Definition: latch.h:124
XLogRecPtr waitLSN
Definition: proc.h:148
static int SyncRepWakeQueue ( bool  all,
int  mode 
)
static

Definition at line 996 of file syncrep.c.

References Assert, WalSndCtlData::lsn, 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().

997 {
998  volatile WalSndCtlData *walsndctl = WalSndCtl;
999  PGPROC *proc = NULL;
1000  PGPROC *thisproc = NULL;
1001  int numprocs = 0;
1002 
1003  Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
1004  Assert(SyncRepQueueIsOrderedByLSN(mode));
1005 
1006  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1007  &(WalSndCtl->SyncRepQueue[mode]),
1008  offsetof(PGPROC, syncRepLinks));
1009 
1010  while (proc)
1011  {
1012  /*
1013  * Assume the queue is ordered by LSN
1014  */
1015  if (!all && walsndctl->lsn[mode] < proc->waitLSN)
1016  return numprocs;
1017 
1018  /*
1019  * Move to next proc, so we can delete thisproc from the queue.
1020  * thisproc is valid, proc may be NULL after this.
1021  */
1022  thisproc = proc;
1023  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1024  &(proc->syncRepLinks),
1025  offsetof(PGPROC, syncRepLinks));
1026 
1027  /*
1028  * Remove thisproc from queue.
1029  */
1030  SHMQueueDelete(&(thisproc->syncRepLinks));
1031 
1032  /*
1033  * SyncRepWaitForLSN() reads syncRepState without holding the lock, so
1034  * make sure that it sees the queue link being removed before the
1035  * syncRepState change.
1036  */
1037  pg_write_barrier();
1038 
1039  /*
1040  * Set state to complete; see SyncRepWaitForLSN() for discussion of
1041  * the various states.
1042  */
1044 
1045  /*
1046  * Wake only when we have set state and removed from queue.
1047  */
1048  SetLatch(&(thisproc->procLatch));
1049 
1050  numprocs++;
1051  }
1052 
1053  return numprocs;
1054 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
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
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
#define Assert(condition)
Definition: c.h:681
SHM_QUEUE syncRepLinks
Definition: proc.h:150
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:149
Definition: proc.h:95
XLogRecPtr waitLSN
Definition: proc.h:148
#define offsetof(type, field)
Definition: c.h:549

Variable Documentation

bool announce_next_takeover = true
static

Definition at line 95 of file syncrep.c.

Referenced by SyncRepReleaseWaiters().

SyncRepConfigData* SyncRepConfig = NULL

Definition at line 97 of file syncrep.c.

Referenced by pg_stat_get_wal_senders().

char* SyncRepStandbyNames

Definition at line 90 of file syncrep.c.

int SyncRepWaitMode = SYNC_REP_NO_WAIT
static

Definition at line 98 of file syncrep.c.

Referenced by assign_synchronous_commit(), and SyncRepWaitForLSN().