PostgreSQL Source Code  git master
syncrep.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * syncrep.c
4  *
5  * Synchronous replication is new as of PostgreSQL 9.1.
6  *
7  * If requested, transaction commits wait until their commit LSN are
8  * acknowledged by the synchronous standbys.
9  *
10  * This module contains the code for waiting and release of backends.
11  * All code in this module executes on the primary. The core streaming
12  * replication transport remains within WALreceiver/WALsender modules.
13  *
14  * The essence of this design is that it isolates all logic about
15  * waiting/releasing onto the primary. The primary defines which standbys
16  * it wishes to wait for. The standbys are completely unaware of the
17  * durability requirements of transactions on the primary, reducing the
18  * complexity of the code and streamlining both standby operations and
19  * network bandwidth because there is no requirement to ship
20  * per-transaction state information.
21  *
22  * Replication is either synchronous or not synchronous (async). If it is
23  * async, we just fastpath out of here. If it is sync, then we wait for
24  * the write, flush or apply location on the standby before releasing
25  * the waiting backend. Further complexity in that interaction is
26  * expected in later releases.
27  *
28  * The best performing way to manage the waiting backends is to have a
29  * single ordered queue of waiting backends, so that we can avoid
30  * searching the through all waiters each time we receive a reply.
31  *
32  * In 9.5 or before only a single standby could be considered as
33  * synchronous. In 9.6 we support a priority-based multiple synchronous
34  * standbys. In 10.0 a quorum-based multiple synchronous standbys is also
35  * supported. The number of synchronous standbys that transactions
36  * must wait for replies from is specified in synchronous_standby_names.
37  * This parameter also specifies a list of standby names and the method
38  * (FIRST and ANY) to choose synchronous standbys from the listed ones.
39  *
40  * The method FIRST specifies a priority-based synchronous replication
41  * and makes transaction commits wait until their WAL records are
42  * replicated to the requested number of synchronous standbys chosen based
43  * on their priorities. The standbys whose names appear earlier in the list
44  * are given higher priority and will be considered as synchronous.
45  * Other standby servers appearing later in this list represent potential
46  * synchronous standbys. If any of the current synchronous standbys
47  * disconnects for whatever reason, it will be replaced immediately with
48  * the next-highest-priority standby.
49  *
50  * The method ANY specifies a quorum-based synchronous replication
51  * and makes transaction commits wait until their WAL records are
52  * replicated to at least the requested number of synchronous standbys
53  * in the list. All the standbys appearing in the list are considered as
54  * candidates for quorum synchronous standbys.
55  *
56  * If neither FIRST nor ANY is specified, FIRST is used as the method.
57  * This is for backward compatibility with 9.6 or before where only a
58  * priority-based sync replication was supported.
59  *
60  * Before the standbys chosen from synchronous_standby_names can
61  * become the synchronous standbys they must have caught up with
62  * the primary; that may take some time. Once caught up,
63  * the standbys which are considered as synchronous at that moment
64  * will release waiters from the queue.
65  *
66  * Portions Copyright (c) 2010-2019, PostgreSQL Global Development Group
67  *
68  * IDENTIFICATION
69  * src/backend/replication/syncrep.c
70  *
71  *-------------------------------------------------------------------------
72  */
73 #include "postgres.h"
74 
75 #include <unistd.h>
76 
77 #include "access/xact.h"
78 #include "miscadmin.h"
79 #include "pgstat.h"
80 #include "replication/syncrep.h"
81 #include "replication/walsender.h"
83 #include "storage/pmsignal.h"
84 #include "storage/proc.h"
85 #include "tcop/tcopprot.h"
86 #include "utils/builtins.h"
87 #include "utils/ps_status.h"
88 
89 /* User-settable parameters for sync rep */
91 
92 #define SyncStandbysDefined() \
93  (SyncRepStandbyNames != NULL && SyncRepStandbyNames[0] != '\0')
94 
95 static bool announce_next_takeover = true;
96 
99 
100 static void SyncRepQueueInsert(int mode);
101 static void SyncRepCancelWait(void);
102 static int SyncRepWakeQueue(bool all, int mode);
103 
104 static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr,
105  XLogRecPtr *flushPtr,
106  XLogRecPtr *applyPtr,
107  bool *am_sync);
108 static void SyncRepGetOldestSyncRecPtr(XLogRecPtr *writePtr,
109  XLogRecPtr *flushPtr,
110  XLogRecPtr *applyPtr,
111  List *sync_standbys);
112 static void SyncRepGetNthLatestSyncRecPtr(XLogRecPtr *writePtr,
113  XLogRecPtr *flushPtr,
114  XLogRecPtr *applyPtr,
115  List *sync_standbys, uint8 nth);
116 static int SyncRepGetStandbyPriority(void);
117 static List *SyncRepGetSyncStandbysPriority(bool *am_sync);
118 static List *SyncRepGetSyncStandbysQuorum(bool *am_sync);
119 static int cmp_lsn(const void *a, const void *b);
120 
121 #ifdef USE_ASSERT_CHECKING
122 static bool SyncRepQueueIsOrderedByLSN(int mode);
123 #endif
124 
125 /*
126  * ===========================================================
127  * Synchronous Replication functions for normal user backends
128  * ===========================================================
129  */
130 
131 /*
132  * Wait for synchronous replication, if requested by user.
133  *
134  * Initially backends start in state SYNC_REP_NOT_WAITING and then
135  * change that state to SYNC_REP_WAITING before adding ourselves
136  * to the wait queue. During SyncRepWakeQueue() a WALSender changes
137  * the state to SYNC_REP_WAIT_COMPLETE once replication is confirmed.
138  * This backend then resets its state to SYNC_REP_NOT_WAITING.
139  *
140  * 'lsn' represents the LSN to wait for. 'commit' indicates whether this LSN
141  * represents a commit record. If it doesn't, then we wait only for the WAL
142  * to be flushed if synchronous_commit is set to the higher level of
143  * remote_apply, because only commit records provide apply feedback.
144  */
145 void
146 SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
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 }
311 
312 /*
313  * Insert MyProc into the specified SyncRepQueue, maintaining sorted invariant.
314  *
315  * Usually we will go at tail of queue, though it's possible that we arrive
316  * here out of order, so start at tail and work back to insertion point.
317  */
318 static void
320 {
321  PGPROC *proc;
322 
323  Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
324  proc = (PGPROC *) SHMQueuePrev(&(WalSndCtl->SyncRepQueue[mode]),
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 
337  proc = (PGPROC *) SHMQueuePrev(&(WalSndCtl->SyncRepQueue[mode]),
338  &(proc->syncRepLinks),
339  offsetof(PGPROC, syncRepLinks));
340  }
341 
342  if (proc)
344  else
346 }
347 
348 /*
349  * Acquire SyncRepLock and cancel any wait currently in progress.
350  */
351 static void
353 {
354  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
358  LWLockRelease(SyncRepLock);
359 }
360 
361 void
363 {
365  {
366  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
368  LWLockRelease(SyncRepLock);
369  }
370 }
371 
372 /*
373  * ===========================================================
374  * Synchronous Replication functions for wal sender processes
375  * ===========================================================
376  */
377 
378 /*
379  * Take any action required to initialise sync rep state from config
380  * data. Called at WALSender startup and after each SIGHUP.
381  */
382 void
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 }
402 
403 /*
404  * Update the LSNs on each queue based upon our latest state. This
405  * implements a simple policy of first-valid-sync-standby-releases-waiter.
406  *
407  * Other policies are possible, which would change what we do here and
408  * perhaps also which information we store as well.
409  */
410 void
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 
459  if (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY)
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 }
507 
508 /*
509  * Calculate the synced Write, Flush and Apply positions among sync standbys.
510  *
511  * Return false if the number of sync standbys is less than
512  * synchronous_standby_names specifies. Otherwise return true and
513  * store the positions into *writePtr, *flushPtr and *applyPtr.
514  *
515  * On return, *am_sync is set to true if this walsender is connecting to
516  * sync standby. Otherwise it's set to false.
517  */
518 static bool
520  XLogRecPtr *applyPtr, bool *am_sync)
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  */
557  if (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY)
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 }
571 
572 /*
573  * Calculate the oldest Write, Flush and Apply positions among sync standbys.
574  */
575 static void
577  XLogRecPtr *applyPtr, List *sync_standbys)
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 }
606 
607 /*
608  * Calculate the Nth latest Write, Flush and Apply positions among sync
609  * standbys.
610  */
611 static void
613  XLogRecPtr *applyPtr, List *sync_standbys, uint8 nth)
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 }
654 
655 /*
656  * Compare lsn in order to sort array in descending order.
657  */
658 static int
659 cmp_lsn(const void *a, const void *b)
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 }
671 
672 /*
673  * Return the list of sync standbys, or NIL if no sync standby is connected.
674  *
675  * The caller must hold SyncRepLock.
676  *
677  * On return, *am_sync is set to true if this walsender is connecting to
678  * sync standby. Otherwise it's set to false.
679  */
680 List *
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 
691  return (SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY) ?
694 }
695 
696 /*
697  * Return the list of all the candidates for quorum sync standbys,
698  * or NIL if no such standby is connected.
699  *
700  * The caller must hold SyncRepLock. This function must be called only in
701  * a quorum-based sync replication.
702  *
703  * On return, *am_sync is set to true if this walsender is connecting to
704  * sync standby. Otherwise it's set to false.
705  */
706 static List *
708 {
709  List *result = NIL;
710  int i;
711  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
712  * rearrangement */
713 
714  Assert(SyncRepConfig->syncrep_method == SYNC_REP_QUORUM);
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 }
758 
759 /*
760  * Return the list of sync standbys chosen based on their priorities,
761  * or NIL if no sync standby is connected.
762  *
763  * If there are multiple standbys with the same priority,
764  * the first one found is selected preferentially.
765  *
766  * The caller must hold SyncRepLock. This function must be called only in
767  * a priority-based sync replication.
768  *
769  * On return, *am_sync is set to true if this walsender is connecting to
770  * sync standby. Otherwise it's set to false.
771  */
772 static List *
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 
786  Assert(SyncRepConfig->syncrep_method == SYNC_REP_PRIORITY);
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 }
933 
934 /*
935  * Check if we are in the list of sync standbys, and if so, determine
936  * priority sequence. Return priority if set, or zero to indicate that
937  * we are not a potential sync standby.
938  *
939  * Compare the parameter SyncRepStandbyNames against the application_name
940  * for this WALSender, or allow any name if we find a wildcard "*".
941  */
942 static int
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 }
980 
981 /*
982  * Walk the specified queue from head. Set the state of any backends that
983  * need to be woken, remove them from the queue, and then wake them.
984  * Pass all = true to wake whole queue; otherwise, just wake up to
985  * the walsender's LSN.
986  *
987  * Must hold SyncRepLock.
988  */
989 static int
990 SyncRepWakeQueue(bool all, int mode)
991 {
992  volatile WalSndCtlData *walsndctl = WalSndCtl;
993  PGPROC *proc = NULL;
994  PGPROC *thisproc = NULL;
995  int numprocs = 0;
996 
997  Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
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 }
1049 
1050 /*
1051  * The checkpointer calls this as needed to update the shared
1052  * sync_standbys_defined flag, so that backends don't remain permanently wedged
1053  * if synchronous_standby_names is unset. It's safe to check the current value
1054  * without the lock, because it's only ever updated by one process. But we
1055  * must take the lock to change it.
1056  */
1057 void
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 }
1091 
1092 #ifdef USE_ASSERT_CHECKING
1093 static bool
1094 SyncRepQueueIsOrderedByLSN(int mode)
1095 {
1096  PGPROC *proc = NULL;
1097  XLogRecPtr lastLSN;
1098 
1099  Assert(mode >= 0 && mode < NUM_SYNC_REP_WAIT_MODE);
1100 
1101  lastLSN = 0;
1102 
1103  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1105  offsetof(PGPROC, syncRepLinks));
1106 
1107  while (proc)
1108  {
1109  /*
1110  * Check the queue is ordered by LSN and that multiple procs don't
1111  * have matching LSNs
1112  */
1113  if (proc->waitLSN <= lastLSN)
1114  return false;
1115 
1116  lastLSN = proc->waitLSN;
1117 
1118  proc = (PGPROC *) SHMQueueNext(&(WalSndCtl->SyncRepQueue[mode]),
1119  &(proc->syncRepLinks),
1120  offsetof(PGPROC, syncRepLinks));
1121  }
1122 
1123  return true;
1124 }
1125 #endif
1126 
1127 /*
1128  * ===========================================================
1129  * Synchronous Replication functions executed by any process
1130  * ===========================================================
1131  */
1132 
1133 bool
1134 check_synchronous_standby_names(char **newval, void **extra, GucSource source)
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 */
1146  syncrep_scanner_init(*newval);
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 }
1189 
1190 void
1191 assign_synchronous_standby_names(const char *newval, void *extra)
1192 {
1193  SyncRepConfig = (SyncRepConfigData *) extra;
1194 }
1195 
1196 void
1198 {
1199  switch (newval)
1200  {
1203  break;
1206  break;
1209  break;
1210  default:
1212  break;
1213  }
1214 }
void syncrep_scanner_finish(void)
#define NIL
Definition: pg_list.h:65
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:1058
XLogRecPtr write
static PgChecksumMode mode
Definition: pg_checksums.c:61
void assign_synchronous_standby_names(const char *newval, void *extra)
Definition: syncrep.c:1191
Pointer SHMQueuePrev(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:164
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static List * SyncRepGetSyncStandbysPriority(bool *am_sync)
Definition: syncrep.c:773
static void SyncRepCancelWait(void)
Definition: syncrep.c:352
volatile sig_atomic_t QueryCancelPending
Definition: globals.c:31
#define DEBUG1
Definition: elog.h:25
void syncrep_scanner_init(const char *query_string)
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
bool update_process_title
Definition: ps_status.c:35
#define write(a, b, c)
Definition: win32.h:14
#define GUC_check_errdetail
Definition: guc.h:409
static void SyncRepQueueInsert(int mode)
Definition: syncrep.c:319
PGPROC * MyProc
Definition: proc.c:68
char * syncrep_parse_error_msg
#define SYNC_REP_WAITING
Definition: syncrep.h:32
void SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
Definition: syncrep.c:146
uint8 syncrep_method
Definition: syncrep.h:51
#define Min(x, y)
Definition: c.h:904
unsigned char uint8
Definition: c.h:356
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
static void SyncRepGetNthLatestSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys, uint8 nth)
Definition: syncrep.c:612
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
int errcode(int sqlerrcode)
Definition: elog.c:570
#define GUC_check_errmsg
Definition: guc.h:405
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
List * SyncRepGetSyncStandbys(bool *am_sync)
Definition: syncrep.c:681
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define LOG
Definition: elog.h:26
void SetLatch(Latch *latch)
Definition: latch.c:436
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
void GUC_check_errcode(int sqlerrcode)
Definition: guc.c:10706
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:990
void ResetLatch(Latch *latch)
Definition: latch.c:519
slock_t mutex
Latch procLatch
Definition: proc.h:104
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
GucSource
Definition: guc.h:105
#define malloc(a)
Definition: header.h:50
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:368
#define sprintf
Definition: port.h:194
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
void pfree(void *pointer)
Definition: mcxt.c:1056
static int cmp_lsn(const void *a, const void *b)
Definition: syncrep.c:659
#define SYNC_REP_NO_WAIT
Definition: syncrep.h:23
#define SYNC_REP_WAIT_APPLY
Definition: syncrep.h:26
#define lfirst_int(lc)
Definition: pg_list.h:191
void SHMQueueInsertAfter(SHM_QUEUE *queue, SHM_QUEUE *elem)
Definition: shmqueue.c:108
static bool announce_next_takeover
Definition: syncrep.c:95
Definition: dest.h:88
const char * get_ps_display(int *displen)
Definition: ps_status.c:412
WalSndState state
void assign_synchronous_commit(int newval, void *extra)
Definition: syncrep.c:1197
#define SyncStandbysDefined()
Definition: syncrep.c:92
int errdetail(const char *fmt,...)
Definition: elog.c:860
unsigned int uint32
Definition: c.h:358
SHM_QUEUE SyncRepQueue[NUM_SYNC_REP_WAIT_MODE]
void SyncRepInitConfig(void)
Definition: syncrep.c:383
#define ereport(elevel, rest)
Definition: elog.h:141
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:25
List * lappend_int(List *list, int datum)
Definition: list.c:340
#define SyncRepRequested()
Definition: syncrep.h:19
int max_wal_senders
Definition: walsender.c:120
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define WARNING
Definition: elog.h:40
#define SpinLockRelease(lock)
Definition: spin.h:64
char * SyncRepStandbyNames
Definition: syncrep.c:90
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
Pointer SHMQueueNext(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:145
WalSnd * MyWalSnd
Definition: walsender.c:111
static List * SyncRepGetSyncStandbysQuorum(bool *am_sync)
Definition: syncrep.c:707
#define SYNC_REP_WAIT_COMPLETE
Definition: syncrep.h:33
#define SYNC_REP_QUORUM
Definition: syncrep.h:37
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
char member_names[FLEXIBLE_ARRAY_MEMBER]
Definition: syncrep.h:54
int sync_standby_priority
volatile sig_atomic_t ProcDiePending
Definition: globals.c:32
Definition: regguts.h:298
#define pg_read_barrier()
Definition: atomics.h:158
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
SyncRepConfigData * syncrep_parse_result
static int list_length(const List *l)
Definition: pg_list.h:169
#define newval
bool check_synchronous_standby_names(char **newval, void **extra, GucSource source)
Definition: syncrep.c:1134
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void SyncRepCleanupAtProcExit(void)
Definition: syncrep.c:362
WalSndState
XLogRecPtr lsn[NUM_SYNC_REP_WAIT_MODE]
char * application_name
Definition: guc.c:529
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
void list_free(List *list)
Definition: list.c:1377
#define elog(elevel,...)
Definition: elog.h:226
int i
#define pg_write_barrier()
Definition: atomics.h:159
static void SyncRepGetOldestSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, List *sync_standbys)
Definition: syncrep.c:576
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
struct Latch * MyLatch
Definition: globals.c:54
int syncrep_yyparse(void)
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28
#define qsort(a, b, c, d)
Definition: port.h:492
int syncRepState
Definition: proc.h:152
CommandDest whereToSendOutput
Definition: postgres.c:90
XLogRecPtr apply
Definition: proc.h:95
Definition: pg_list.h:50
static int SyncRepWaitMode
Definition: syncrep.c:98
#define WL_LATCH_SET
Definition: latch.h:124
void SyncRepReleaseWaiters(void)
Definition: syncrep.c:411
XLogRecPtr waitLSN
Definition: proc.h:151
#define offsetof(type, field)
Definition: c.h:655
bool am_cascading_walsender
Definition: walsender.c:115
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24
static int SyncRepGetStandbyPriority(void)
Definition: syncrep.c:943