PostgreSQL Source Code  git master
syncrep.h File Reference
#include "access/xlogdefs.h"
#include "utils/guc.h"
Include dependency graph for syncrep.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SyncRepStandbyData
 
struct  SyncRepConfigData
 

Macros

#define SyncRepRequested()   (max_wal_senders > 0 && synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
 
#define SYNC_REP_NO_WAIT   (-1)
 
#define SYNC_REP_WAIT_WRITE   0
 
#define SYNC_REP_WAIT_FLUSH   1
 
#define SYNC_REP_WAIT_APPLY   2
 
#define NUM_SYNC_REP_WAIT_MODE   3
 
#define SYNC_REP_NOT_WAITING   0
 
#define SYNC_REP_WAITING   1
 
#define SYNC_REP_WAIT_COMPLETE   2
 
#define SYNC_REP_PRIORITY   0
 
#define SYNC_REP_QUORUM   1
 

Typedefs

typedef struct SyncRepStandbyData SyncRepStandbyData
 
typedef struct SyncRepConfigData SyncRepConfigData
 

Functions

void SyncRepWaitForLSN (XLogRecPtr lsn, bool commit)
 
void SyncRepCleanupAtProcExit (void)
 
void SyncRepInitConfig (void)
 
void SyncRepReleaseWaiters (void)
 
int SyncRepGetCandidateStandbys (SyncRepStandbyData **standbys)
 
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)
 
int syncrep_yyparse (void)
 
int syncrep_yylex (void)
 
void syncrep_yyerror (const char *str)
 
void syncrep_scanner_init (const char *query_string)
 
void syncrep_scanner_finish (void)
 

Variables

SyncRepConfigDataSyncRepConfig
 
SyncRepConfigDatasyncrep_parse_result
 
char * syncrep_parse_error_msg
 
char * SyncRepStandbyNames
 

Macro Definition Documentation

◆ NUM_SYNC_REP_WAIT_MODE

#define NUM_SYNC_REP_WAIT_MODE   3

◆ SYNC_REP_NO_WAIT

#define SYNC_REP_NO_WAIT   (-1)

Definition at line 23 of file syncrep.h.

Referenced by assign_synchronous_commit().

◆ SYNC_REP_NOT_WAITING

#define SYNC_REP_NOT_WAITING   0

Definition at line 31 of file syncrep.h.

Referenced by InitProcess(), SyncRepCancelWait(), and SyncRepWaitForLSN().

◆ SYNC_REP_PRIORITY

#define SYNC_REP_PRIORITY   0

◆ SYNC_REP_QUORUM

#define SYNC_REP_QUORUM   1

Definition at line 37 of file syncrep.h.

◆ SYNC_REP_WAIT_APPLY

#define SYNC_REP_WAIT_APPLY   2

◆ SYNC_REP_WAIT_COMPLETE

#define SYNC_REP_WAIT_COMPLETE   2

Definition at line 33 of file syncrep.h.

Referenced by SyncRepWaitForLSN(), and SyncRepWakeQueue().

◆ SYNC_REP_WAIT_FLUSH

#define SYNC_REP_WAIT_FLUSH   1

◆ SYNC_REP_WAIT_WRITE

#define SYNC_REP_WAIT_WRITE   0

◆ SYNC_REP_WAITING

#define SYNC_REP_WAITING   1

Definition at line 32 of file syncrep.h.

Referenced by SyncRepWaitForLSN().

◆ SyncRepRequested

#define SyncRepRequested ( )    (max_wal_senders > 0 && synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)

Definition at line 19 of file syncrep.h.

Referenced by SyncRepWaitForLSN().

Typedef Documentation

◆ SyncRepConfigData

◆ SyncRepStandbyData

Function Documentation

◆ assign_synchronous_commit()

void assign_synchronous_commit ( int  newval,
void *  extra 
)

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

1087 {
1088  switch (newval)
1089  {
1092  break;
1095  break;
1098  break;
1099  default:
1101  break;
1102  }
1103 }
#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 1080 of file syncrep.c.

1081 {
1082  SyncRepConfig = (SyncRepConfigData *) extra;
1083 }
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97

◆ check_synchronous_standby_names()

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

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

1024 {
1025  if (*newval != NULL && (*newval)[0] != '\0')
1026  {
1027  int parse_rc;
1028  SyncRepConfigData *pconf;
1029 
1030  /* Reset communication variables to ensure a fresh start */
1031  syncrep_parse_result = NULL;
1032  syncrep_parse_error_msg = NULL;
1033 
1034  /* Parse the synchronous_standby_names string */
1036  parse_rc = syncrep_yyparse();
1038 
1039  if (parse_rc != 0 || syncrep_parse_result == NULL)
1040  {
1041  GUC_check_errcode(ERRCODE_SYNTAX_ERROR);
1044  else
1045  GUC_check_errdetail("synchronous_standby_names parser failed");
1046  return false;
1047  }
1048 
1049  if (syncrep_parse_result->num_sync <= 0)
1050  {
1051  GUC_check_errmsg("number of synchronous standbys (%d) must be greater than zero",
1053  return false;
1054  }
1055 
1056  /* GUC extra value must be malloc'd, not palloc'd */
1057  pconf = (SyncRepConfigData *)
1059  if (pconf == NULL)
1060  return false;
1062 
1063  *extra = (void *) pconf;
1064 
1065  /*
1066  * We need not explicitly clean up syncrep_parse_result. It, and any
1067  * other cruft generated during parsing, will be freed when the
1068  * current memory context is deleted. (This code is generally run in
1069  * a short-lived context used for config file processing, so that will
1070  * not be very long.)
1071  */
1072  }
1073  else
1074  *extra = NULL;
1075 
1076  return true;
1077 }
void syncrep_scanner_finish(void)
void syncrep_scanner_init(const char *query_string)
#define GUC_check_errdetail
Definition: guc.h:422
char * syncrep_parse_error_msg
#define GUC_check_errmsg
Definition: guc.h:418
void GUC_check_errcode(int sqlerrcode)
Definition: guc.c:11403
#define malloc(a)
Definition: header.h:50
SyncRepConfigData * syncrep_parse_result
#define newval
int syncrep_yyparse(void)

◆ syncrep_scanner_finish()

void syncrep_scanner_finish ( void  )

◆ syncrep_scanner_init()

void syncrep_scanner_init ( const char *  query_string)

◆ syncrep_yyerror()

void syncrep_yyerror ( const char *  str)

◆ syncrep_yylex()

int syncrep_yylex ( void  )

◆ syncrep_yyparse()

int syncrep_yyparse ( void  )

◆ SyncRepCleanupAtProcExit()

void SyncRepCleanupAtProcExit ( void  )

Definition at line 382 of file syncrep.c.

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

Referenced by ProcKill().

383 {
384  /*
385  * First check if we are removed from the queue without the lock to not
386  * slow down backend exit.
387  */
389  {
390  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
391 
392  /* maybe we have just been removed, so recheck */
395 
396  LWLockRelease(SyncRepLock);
397  }
398 }
PGPROC * MyProc
Definition: proc.c:68
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
SHM_QUEUE syncRepLinks
Definition: proc.h:201
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68

◆ SyncRepGetCandidateStandbys()

int SyncRepGetCandidateStandbys ( SyncRepStandbyData **  standbys)

Definition at line 725 of file syncrep.c.

References SyncRepStandbyData::apply, WalSnd::apply, SyncRepStandbyData::flush, WalSnd::flush, i, SyncRepStandbyData::is_me, max_wal_senders, WalSnd::mutex, MyWalSnd, SyncRepConfigData::num_sync, palloc(), WalSnd::pid, SyncRepStandbyData::pid, qsort, SpinLockAcquire, SpinLockRelease, standby_priority_comparator(), WalSnd::state, SYNC_REP_PRIORITY, SyncRepStandbyData::sync_standby_priority, WalSnd::sync_standby_priority, SyncRepConfigData::syncrep_method, SyncRepStandbyData::walsnd_index, WalSndCtl, WalSndCtlData::walsnds, WALSNDSTATE_STOPPING, WALSNDSTATE_STREAMING, SyncRepStandbyData::write, WalSnd::write, and XLogRecPtrIsInvalid.

Referenced by pg_stat_get_wal_senders(), and SyncRepGetSyncRecPtr().

726 {
727  int i;
728  int n;
729 
730  /* Create result array */
731  *standbys = (SyncRepStandbyData *)
733 
734  /* Quick exit if sync replication is not requested */
735  if (SyncRepConfig == NULL)
736  return 0;
737 
738  /* Collect raw data from shared memory */
739  n = 0;
740  for (i = 0; i < max_wal_senders; i++)
741  {
742  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
743  * rearrangement */
744  SyncRepStandbyData *stby;
745  WalSndState state; /* not included in SyncRepStandbyData */
746 
747  walsnd = &WalSndCtl->walsnds[i];
748  stby = *standbys + n;
749 
750  SpinLockAcquire(&walsnd->mutex);
751  stby->pid = walsnd->pid;
752  state = walsnd->state;
753  stby->write = walsnd->write;
754  stby->flush = walsnd->flush;
755  stby->apply = walsnd->apply;
757  SpinLockRelease(&walsnd->mutex);
758 
759  /* Must be active */
760  if (stby->pid == 0)
761  continue;
762 
763  /* Must be streaming or stopping */
764  if (state != WALSNDSTATE_STREAMING &&
765  state != WALSNDSTATE_STOPPING)
766  continue;
767 
768  /* Must be synchronous */
769  if (stby->sync_standby_priority == 0)
770  continue;
771 
772  /* Must have a valid flush position */
773  if (XLogRecPtrIsInvalid(stby->flush))
774  continue;
775 
776  /* OK, it's a candidate */
777  stby->walsnd_index = i;
778  stby->is_me = (walsnd == MyWalSnd);
779  n++;
780  }
781 
782  /*
783  * In quorum mode, we return all the candidates. In priority mode, if we
784  * have too many candidates then return only the num_sync ones of highest
785  * priority.
786  */
788  n > SyncRepConfig->num_sync)
789  {
790  /* Sort by priority ... */
791  qsort(*standbys, n, sizeof(SyncRepStandbyData),
793  /* ... then report just the first num_sync ones */
794  n = SyncRepConfig->num_sync;
795  }
796 
797  return n;
798 }
XLogRecPtr write
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:69
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
WalSnd walsnds[FLEXIBLE_ARRAY_MEMBER]
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
int sync_standby_priority
Definition: syncrep.h:50
slock_t mutex
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr flush
static int standby_priority_comparator(const void *a, const void *b)
Definition: syncrep.c:804
XLogRecPtr flush
Definition: syncrep.h:48
XLogRecPtr write
Definition: syncrep.h:47
WalSndState state
XLogRecPtr apply
Definition: syncrep.h:49
int max_wal_senders
Definition: walsender.c:121
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define SpinLockRelease(lock)
Definition: spin.h:64
WalSnd * MyWalSnd
Definition: walsender.c:112
int sync_standby_priority
Definition: regguts.h:317
WalSndState
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define qsort(a, b, c, d)
Definition: port.h:504
XLogRecPtr apply

◆ SyncRepInitConfig()

void SyncRepInitConfig ( void  )

Definition at line 411 of file syncrep.c.

References application_name, DEBUG1, ereport, errmsg_internal(), WalSnd::mutex, MyWalSnd, SpinLockAcquire, SpinLockRelease, WalSnd::sync_standby_priority, and SyncRepGetStandbyPriority().

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

412 {
413  int priority;
414 
415  /*
416  * Determine if we are a potential sync standby and remember the result
417  * for handling replies from standby.
418  */
419  priority = SyncRepGetStandbyPriority();
420  if (MyWalSnd->sync_standby_priority != priority)
421  {
423  MyWalSnd->sync_standby_priority = priority;
425 
426  ereport(DEBUG1,
427  (errmsg_internal("standby \"%s\" now has synchronous standby priority %u",
428  application_name, priority)));
429  }
430 }
#define DEBUG1
Definition: elog.h:25
slock_t mutex
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
WalSnd * MyWalSnd
Definition: walsender.c:112
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
int sync_standby_priority
char * application_name
Definition: guc.c:621
static int SyncRepGetStandbyPriority(void)
Definition: syncrep.c:831

◆ SyncRepReleaseWaiters()

void SyncRepReleaseWaiters ( void  )

Definition at line 440 of file syncrep.c.

References announce_next_takeover, application_name, DEBUG3, elog, ereport, errmsg(), WalSnd::flush, LOG, WalSndCtlData::lsn, LSN_FORMAT_ARGS, 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().

441 {
442  volatile WalSndCtlData *walsndctl = WalSndCtl;
443  XLogRecPtr writePtr;
444  XLogRecPtr flushPtr;
445  XLogRecPtr applyPtr;
446  bool got_recptr;
447  bool am_sync;
448  int numwrite = 0;
449  int numflush = 0;
450  int numapply = 0;
451 
452  /*
453  * If this WALSender is serving a standby that is not on the list of
454  * potential sync standbys then we have nothing to do. If we are still
455  * starting up, still running base backup or the current flush position is
456  * still invalid, then leave quickly also. Streaming or stopping WAL
457  * senders are allowed to release waiters.
458  */
459  if (MyWalSnd->sync_standby_priority == 0 ||
463  {
464  announce_next_takeover = true;
465  return;
466  }
467 
468  /*
469  * We're a potential sync standby. Release waiters if there are enough
470  * sync standbys and we are considered as sync.
471  */
472  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
473 
474  /*
475  * Check whether we are a sync standby or not, and calculate the synced
476  * positions among all sync standbys. (Note: although this step does not
477  * of itself require holding SyncRepLock, it seems like a good idea to do
478  * it after acquiring the lock. This ensures that the WAL pointers we use
479  * to release waiters are newer than any previous execution of this
480  * routine used.)
481  */
482  got_recptr = SyncRepGetSyncRecPtr(&writePtr, &flushPtr, &applyPtr, &am_sync);
483 
484  /*
485  * If we are managing a sync standby, though we weren't prior to this,
486  * then announce we are now a sync standby.
487  */
488  if (announce_next_takeover && am_sync)
489  {
490  announce_next_takeover = false;
491 
493  ereport(LOG,
494  (errmsg("standby \"%s\" is now a synchronous standby with priority %u",
496  else
497  ereport(LOG,
498  (errmsg("standby \"%s\" is now a candidate for quorum synchronous standby",
499  application_name)));
500  }
501 
502  /*
503  * If the number of sync standbys is less than requested or we aren't
504  * managing a sync standby then just leave.
505  */
506  if (!got_recptr || !am_sync)
507  {
508  LWLockRelease(SyncRepLock);
509  announce_next_takeover = !am_sync;
510  return;
511  }
512 
513  /*
514  * Set the lsn first so that when we wake backends they will release up to
515  * this location.
516  */
517  if (walsndctl->lsn[SYNC_REP_WAIT_WRITE] < writePtr)
518  {
519  walsndctl->lsn[SYNC_REP_WAIT_WRITE] = writePtr;
520  numwrite = SyncRepWakeQueue(false, SYNC_REP_WAIT_WRITE);
521  }
522  if (walsndctl->lsn[SYNC_REP_WAIT_FLUSH] < flushPtr)
523  {
524  walsndctl->lsn[SYNC_REP_WAIT_FLUSH] = flushPtr;
525  numflush = SyncRepWakeQueue(false, SYNC_REP_WAIT_FLUSH);
526  }
527  if (walsndctl->lsn[SYNC_REP_WAIT_APPLY] < applyPtr)
528  {
529  walsndctl->lsn[SYNC_REP_WAIT_APPLY] = applyPtr;
530  numapply = SyncRepWakeQueue(false, SYNC_REP_WAIT_APPLY);
531  }
532 
533  LWLockRelease(SyncRepLock);
534 
535  elog(DEBUG3, "released %d procs up to write %X/%X, %d procs up to flush %X/%X, %d procs up to apply %X/%X",
536  numwrite, LSN_FORMAT_ARGS(writePtr),
537  numflush, LSN_FORMAT_ARGS(flushPtr),
538  numapply, LSN_FORMAT_ARGS(applyPtr));
539 }
static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, bool *am_sync)
Definition: syncrep.c:552
#define DEBUG3
Definition: elog.h:23
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
uint8 syncrep_method
Definition: syncrep.h:69
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
#define LOG
Definition: elog.h:26
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:878
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecPtr flush
#define SYNC_REP_WAIT_APPLY
Definition: syncrep.h:26
static bool announce_next_takeover
Definition: syncrep.c:95
WalSndState state
#define SYNC_REP_WAIT_FLUSH
Definition: syncrep.h:25
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
WalSnd * MyWalSnd
Definition: walsender.c:112
#define ereport(elevel,...)
Definition: elog.h:157
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int sync_standby_priority
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
XLogRecPtr lsn[NUM_SYNC_REP_WAIT_MODE]
char * application_name
Definition: guc.c:621
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24

◆ SyncRepUpdateSyncStandbysDefined()

void SyncRepUpdateSyncStandbysDefined ( void  )

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

948 {
949  bool sync_standbys_defined = SyncStandbysDefined();
950 
951  if (sync_standbys_defined != WalSndCtl->sync_standbys_defined)
952  {
953  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
954 
955  /*
956  * If synchronous_standby_names has been reset to empty, it's futile
957  * for backends to continue waiting. Since the user no longer wants
958  * synchronous replication, we'd better wake them up.
959  */
960  if (!sync_standbys_defined)
961  {
962  int i;
963 
964  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
965  SyncRepWakeQueue(true, i);
966  }
967 
968  /*
969  * Only allow people to join the queue when there are synchronous
970  * standbys defined. Without this interlock, there's a race
971  * condition: we might wake up all the current waiters; then, some
972  * backend that hasn't yet reloaded its config might go to sleep on
973  * the queue (and never wake up). This prevents that.
974  */
975  WalSndCtl->sync_standbys_defined = sync_standbys_defined;
976 
977  LWLockRelease(SyncRepLock);
978  }
979 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:878
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SyncStandbysDefined()
Definition: syncrep.c:92
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
#define NUM_SYNC_REP_WAIT_MODE
Definition: syncrep.h:28

◆ SyncRepWaitForLSN()

void SyncRepWaitForLSN ( XLogRecPtr  lsn,
bool  commit 
)

Definition at line 148 of file syncrep.c.

References Assert, DestNone, ereport, errcode(), errdetail(), errmsg(), get_ps_display(), InterruptHoldoffCount, LSN_FORMAT_ARGS, 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().

149 {
150  char *new_status = NULL;
151  const char *old_status;
152  int mode;
153 
154  /*
155  * This should be called while holding interrupts during a transaction
156  * commit to prevent the follow-up shared memory queue cleanups to be
157  * influenced by external interruptions.
158  */
160 
161  /*
162  * Fast exit if user has not requested sync replication, or there are no
163  * sync replication standby names defined.
164  *
165  * Since this routine gets called every commit time, it's important to
166  * exit quickly if sync replication is not requested. So we check
167  * WalSndCtl->sync_standbys_defined flag without the lock and exit
168  * immediately if it's false. If it's true, we need to check it again
169  * later while holding the lock, to check the flag and operate the sync
170  * rep queue atomically. This is necessary to avoid the race condition
171  * described in SyncRepUpdateSyncStandbysDefined(). On the other hand, if
172  * it's false, the lock is not necessary because we don't touch the queue.
173  */
174  if (!SyncRepRequested() ||
175  !((volatile WalSndCtlData *) WalSndCtl)->sync_standbys_defined)
176  return;
177 
178  /* Cap the level for anything other than commit to remote flush only. */
179  if (commit)
180  mode = SyncRepWaitMode;
181  else
183 
185  Assert(WalSndCtl != NULL);
186 
187  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
189 
190  /*
191  * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is not
192  * set. See SyncRepUpdateSyncStandbysDefined.
193  *
194  * Also check that the standby hasn't already replied. Unlikely race
195  * condition but we'll be fetching that cache line anyway so it's likely
196  * to be a low cost check.
197  */
198  if (!WalSndCtl->sync_standbys_defined ||
199  lsn <= WalSndCtl->lsn[mode])
200  {
201  LWLockRelease(SyncRepLock);
202  return;
203  }
204 
205  /*
206  * Set our waitLSN so WALSender will know when to wake us, and add
207  * ourselves to the queue.
208  */
209  MyProc->waitLSN = lsn;
211  SyncRepQueueInsert(mode);
212  Assert(SyncRepQueueIsOrderedByLSN(mode));
213  LWLockRelease(SyncRepLock);
214 
215  /* Alter ps display to show waiting for sync rep. */
217  {
218  int len;
219 
220  old_status = get_ps_display(&len);
221  new_status = (char *) palloc(len + 32 + 1);
222  memcpy(new_status, old_status, len);
223  sprintf(new_status + len, " waiting for %X/%X",
224  LSN_FORMAT_ARGS(lsn));
225  set_ps_display(new_status);
226  new_status[len] = '\0'; /* truncate off " waiting ..." */
227  }
228 
229  /*
230  * Wait for specified LSN to be confirmed.
231  *
232  * Each proc has its own wait latch, so we perform a normal latch
233  * check/wait loop here.
234  */
235  for (;;)
236  {
237  int rc;
238 
239  /* Must reset the latch before testing state. */
241 
242  /*
243  * Acquiring the lock is not needed, the latch ensures proper
244  * barriers. If it looks like we're done, we must really be done,
245  * because once walsender changes the state to SYNC_REP_WAIT_COMPLETE,
246  * it will never update it again, so we can't be seeing a stale value
247  * in that case.
248  */
250  break;
251 
252  /*
253  * If a wait for synchronous replication is pending, we can neither
254  * acknowledge the commit nor raise ERROR or FATAL. The latter would
255  * lead the client to believe that the transaction aborted, which is
256  * not true: it's already committed locally. The former is no good
257  * either: the client has requested synchronous replication, and is
258  * entitled to assume that an acknowledged commit is also replicated,
259  * which might not be true. So in this case we issue a WARNING (which
260  * some clients may be able to interpret) and shut off further output.
261  * We do NOT reset ProcDiePending, so that the process will die after
262  * the commit is cleaned up.
263  */
264  if (ProcDiePending)
265  {
267  (errcode(ERRCODE_ADMIN_SHUTDOWN),
268  errmsg("canceling the wait for synchronous replication and terminating connection due to administrator command"),
269  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
272  break;
273  }
274 
275  /*
276  * It's unclear what to do if a query cancel interrupt arrives. We
277  * can't actually abort at this point, but ignoring the interrupt
278  * altogether is not helpful, so we just terminate the wait with a
279  * suitable warning.
280  */
281  if (QueryCancelPending)
282  {
283  QueryCancelPending = false;
285  (errmsg("canceling wait for synchronous replication due to user request"),
286  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
288  break;
289  }
290 
291  /*
292  * Wait on latch. Any condition that should wake us up will set the
293  * latch, so no need for timeout.
294  */
297 
298  /*
299  * If the postmaster dies, we'll probably never get an acknowledgment,
300  * because all the wal sender processes will exit. So just bail out.
301  */
302  if (rc & WL_POSTMASTER_DEATH)
303  {
304  ProcDiePending = true;
307  break;
308  }
309  }
310 
311  /*
312  * WalSender has checked our LSN and has removed us from queue. Clean up
313  * state and leave. It's OK to reset these shared memory fields without
314  * holding SyncRepLock, because any walsenders will ignore us anyway when
315  * we're not on the queue. We need a read barrier to make sure we see the
316  * changes to the queue link (this might be unnecessary without
317  * assertions, but better safe than sorry).
318  */
319  pg_read_barrier();
322  MyProc->waitLSN = 0;
323 
324  if (new_status)
325  {
326  /* Reset ps display */
327  set_ps_display(new_status);
328  pfree(new_status);
329  }
330 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
volatile uint32 InterruptHoldoffCount
Definition: globals.c:39
static void SyncRepCancelWait(void)
Definition: syncrep.c:372
volatile sig_atomic_t QueryCancelPending
Definition: globals.c:31
bool update_process_title
Definition: ps_status.c:36
static void SyncRepQueueInsert(int mode)
Definition: syncrep.c:339
PGPROC * MyProc
Definition: proc.c:68
#define SYNC_REP_WAITING
Definition: syncrep.h:32
#define Min(x, y)
Definition: c.h:986
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
int errcode(int sqlerrcode)
Definition: elog.c:698
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void set_ps_display(const char *activity)
Definition: ps_status.c:349
#define sprintf
Definition: port.h:218
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
void pfree(void *pointer)
Definition: mcxt.c:1169
Definition: dest.h:89
const char * get_ps_display(int *displen)
Definition: ps_status.c:430
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#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:129
#define ereport(elevel,...)
Definition: elog.h:157
#define SYNC_REP_WAIT_COMPLETE
Definition: syncrep.h:33
#define Assert(condition)
Definition: c.h:804
volatile sig_atomic_t ProcDiePending
Definition: globals.c:32
#define pg_read_barrier()
Definition: atomics.h:158
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
SHM_QUEUE syncRepLinks
Definition: proc.h:201
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
struct Latch * MyLatch
Definition: globals.c:57
int syncRepState
Definition: proc.h:200
CommandDest whereToSendOutput
Definition: postgres.c:92
static int SyncRepWaitMode
Definition: syncrep.c:98
#define WL_LATCH_SET
Definition: latch.h:125
XLogRecPtr waitLSN
Definition: proc.h:199

Variable Documentation

◆ syncrep_parse_error_msg

char* syncrep_parse_error_msg

◆ syncrep_parse_result

SyncRepConfigData* syncrep_parse_result

◆ SyncRepConfig

SyncRepConfigData* SyncRepConfig

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.