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 1075 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.

1076 {
1077  switch (newval)
1078  {
1081  break;
1084  break;
1087  break;
1088  default:
1090  break;
1091  }
1092 }
#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 1069 of file syncrep.c.

1070 {
1071  SyncRepConfig = (SyncRepConfigData *) extra;
1072 }
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97

◆ check_synchronous_standby_names()

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

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

1013 {
1014  if (*newval != NULL && (*newval)[0] != '\0')
1015  {
1016  int parse_rc;
1017  SyncRepConfigData *pconf;
1018 
1019  /* Reset communication variables to ensure a fresh start */
1020  syncrep_parse_result = NULL;
1021  syncrep_parse_error_msg = NULL;
1022 
1023  /* Parse the synchronous_standby_names string */
1025  parse_rc = syncrep_yyparse();
1027 
1028  if (parse_rc != 0 || syncrep_parse_result == NULL)
1029  {
1030  GUC_check_errcode(ERRCODE_SYNTAX_ERROR);
1033  else
1034  GUC_check_errdetail("synchronous_standby_names parser failed");
1035  return false;
1036  }
1037 
1038  if (syncrep_parse_result->num_sync <= 0)
1039  {
1040  GUC_check_errmsg("number of synchronous standbys (%d) must be greater than zero",
1042  return false;
1043  }
1044 
1045  /* GUC extra value must be malloc'd, not palloc'd */
1046  pconf = (SyncRepConfigData *)
1048  if (pconf == NULL)
1049  return false;
1051 
1052  *extra = (void *) pconf;
1053 
1054  /*
1055  * We need not explicitly clean up syncrep_parse_result. It, and any
1056  * other cruft generated during parsing, will be freed when the
1057  * current memory context is deleted. (This code is generally run in
1058  * a short-lived context used for config file processing, so that will
1059  * not be very long.)
1060  */
1061  }
1062  else
1063  *extra = NULL;
1064 
1065  return true;
1066 }
void syncrep_scanner_finish(void)
void syncrep_scanner_init(const char *query_string)
#define GUC_check_errdetail
Definition: guc.h:415
char * syncrep_parse_error_msg
#define GUC_check_errmsg
Definition: guc.h:411
void GUC_check_errcode(int sqlerrcode)
Definition: guc.c:10899
#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 371 of file syncrep.c.

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

Referenced by ProcKill().

372 {
373  /*
374  * First check if we are removed from the queue without the lock to not
375  * slow down backend exit.
376  */
378  {
379  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
380 
381  /* maybe we have just been removed, so recheck */
384 
385  LWLockRelease(SyncRepLock);
386  }
387 }
PGPROC * MyProc
Definition: proc.c:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
bool SHMQueueIsDetached(const SHM_QUEUE *queue)
Definition: shmqueue.c:47
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
SHM_QUEUE syncRepLinks
Definition: proc.h:162
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68

◆ SyncRepGetCandidateStandbys()

int SyncRepGetCandidateStandbys ( SyncRepStandbyData **  standbys)

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

715 {
716  int i;
717  int n;
718 
719  /* Create result array */
720  *standbys = (SyncRepStandbyData *)
722 
723  /* Quick exit if sync replication is not requested */
724  if (SyncRepConfig == NULL)
725  return 0;
726 
727  /* Collect raw data from shared memory */
728  n = 0;
729  for (i = 0; i < max_wal_senders; i++)
730  {
731  volatile WalSnd *walsnd; /* Use volatile pointer to prevent code
732  * rearrangement */
733  SyncRepStandbyData *stby;
734  WalSndState state; /* not included in SyncRepStandbyData */
735 
736  walsnd = &WalSndCtl->walsnds[i];
737  stby = *standbys + n;
738 
739  SpinLockAcquire(&walsnd->mutex);
740  stby->pid = walsnd->pid;
741  state = walsnd->state;
742  stby->write = walsnd->write;
743  stby->flush = walsnd->flush;
744  stby->apply = walsnd->apply;
746  SpinLockRelease(&walsnd->mutex);
747 
748  /* Must be active */
749  if (stby->pid == 0)
750  continue;
751 
752  /* Must be streaming or stopping */
753  if (state != WALSNDSTATE_STREAMING &&
754  state != WALSNDSTATE_STOPPING)
755  continue;
756 
757  /* Must be synchronous */
758  if (stby->sync_standby_priority == 0)
759  continue;
760 
761  /* Must have a valid flush position */
762  if (XLogRecPtrIsInvalid(stby->flush))
763  continue;
764 
765  /* OK, it's a candidate */
766  stby->walsnd_index = i;
767  stby->is_me = (walsnd == MyWalSnd);
768  n++;
769  }
770 
771  /*
772  * In quorum mode, we return all the candidates. In priority mode, if we
773  * have too many candidates then return only the num_sync ones of highest
774  * priority.
775  */
777  n > SyncRepConfig->num_sync)
778  {
779  /* Sort by priority ... */
780  qsort(*standbys, n, sizeof(SyncRepStandbyData),
782  /* ... then report just the first num_sync ones */
783  n = SyncRepConfig->num_sync;
784  }
785 
786  return n;
787 }
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:793
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:298
WalSndState
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define qsort(a, b, c, d)
Definition: port.h:479
XLogRecPtr apply

◆ SyncRepInitConfig()

void SyncRepInitConfig ( void  )

Definition at line 400 of file syncrep.c.

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

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

401 {
402  int priority;
403 
404  /*
405  * Determine if we are a potential sync standby and remember the result
406  * for handling replies from standby.
407  */
408  priority = SyncRepGetStandbyPriority();
409  if (MyWalSnd->sync_standby_priority != priority)
410  {
412  MyWalSnd->sync_standby_priority = priority;
414 
415  ereport(DEBUG1,
416  (errmsg("standby \"%s\" now has synchronous standby priority %u",
417  application_name, priority)));
418  }
419 }
#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:144
int sync_standby_priority
char * application_name
Definition: guc.c:559
int errmsg(const char *fmt,...)
Definition: elog.c:824
static int SyncRepGetStandbyPriority(void)
Definition: syncrep.c:820

◆ SyncRepReleaseWaiters()

void SyncRepReleaseWaiters ( void  )

Definition at line 429 of file syncrep.c.

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

Referenced by ProcessStandbyReplyMessage().

430 {
431  volatile WalSndCtlData *walsndctl = WalSndCtl;
432  XLogRecPtr writePtr;
433  XLogRecPtr flushPtr;
434  XLogRecPtr applyPtr;
435  bool got_recptr;
436  bool am_sync;
437  int numwrite = 0;
438  int numflush = 0;
439  int numapply = 0;
440 
441  /*
442  * If this WALSender is serving a standby that is not on the list of
443  * potential sync standbys then we have nothing to do. If we are still
444  * starting up, still running base backup or the current flush position is
445  * still invalid, then leave quickly also. Streaming or stopping WAL
446  * senders are allowed to release waiters.
447  */
448  if (MyWalSnd->sync_standby_priority == 0 ||
452  {
453  announce_next_takeover = true;
454  return;
455  }
456 
457  /*
458  * We're a potential sync standby. Release waiters if there are enough
459  * sync standbys and we are considered as sync.
460  */
461  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
462 
463  /*
464  * Check whether we are a sync standby or not, and calculate the synced
465  * positions among all sync standbys. (Note: although this step does not
466  * of itself require holding SyncRepLock, it seems like a good idea to do
467  * it after acquiring the lock. This ensures that the WAL pointers we use
468  * to release waiters are newer than any previous execution of this
469  * routine used.)
470  */
471  got_recptr = SyncRepGetSyncRecPtr(&writePtr, &flushPtr, &applyPtr, &am_sync);
472 
473  /*
474  * If we are managing a sync standby, though we weren't prior to this,
475  * then announce we are now a sync standby.
476  */
477  if (announce_next_takeover && am_sync)
478  {
479  announce_next_takeover = false;
480 
482  ereport(LOG,
483  (errmsg("standby \"%s\" is now a synchronous standby with priority %u",
485  else
486  ereport(LOG,
487  (errmsg("standby \"%s\" is now a candidate for quorum synchronous standby",
488  application_name)));
489  }
490 
491  /*
492  * If the number of sync standbys is less than requested or we aren't
493  * managing a sync standby then just leave.
494  */
495  if (!got_recptr || !am_sync)
496  {
497  LWLockRelease(SyncRepLock);
498  announce_next_takeover = !am_sync;
499  return;
500  }
501 
502  /*
503  * Set the lsn first so that when we wake backends they will release up to
504  * this location.
505  */
506  if (walsndctl->lsn[SYNC_REP_WAIT_WRITE] < writePtr)
507  {
508  walsndctl->lsn[SYNC_REP_WAIT_WRITE] = writePtr;
509  numwrite = SyncRepWakeQueue(false, SYNC_REP_WAIT_WRITE);
510  }
511  if (walsndctl->lsn[SYNC_REP_WAIT_FLUSH] < flushPtr)
512  {
513  walsndctl->lsn[SYNC_REP_WAIT_FLUSH] = flushPtr;
514  numflush = SyncRepWakeQueue(false, SYNC_REP_WAIT_FLUSH);
515  }
516  if (walsndctl->lsn[SYNC_REP_WAIT_APPLY] < applyPtr)
517  {
518  walsndctl->lsn[SYNC_REP_WAIT_APPLY] = applyPtr;
519  numapply = SyncRepWakeQueue(false, SYNC_REP_WAIT_APPLY);
520  }
521 
522  LWLockRelease(SyncRepLock);
523 
524  elog(DEBUG3, "released %d procs up to write %X/%X, %d procs up to flush %X/%X, %d procs up to apply %X/%X",
525  numwrite, (uint32) (writePtr >> 32), (uint32) writePtr,
526  numflush, (uint32) (flushPtr >> 32), (uint32) flushPtr,
527  numapply, (uint32) (applyPtr >> 32), (uint32) applyPtr);
528 }
static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, bool *am_sync)
Definition: syncrep.c:541
#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:867
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
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:367
#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:144
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int sync_standby_priority
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
XLogRecPtr lsn[NUM_SYNC_REP_WAIT_MODE]
char * application_name
Definition: guc.c:559
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24

◆ SyncRepUpdateSyncStandbysDefined()

void SyncRepUpdateSyncStandbysDefined ( void  )

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

937 {
938  bool sync_standbys_defined = SyncStandbysDefined();
939 
940  if (sync_standbys_defined != WalSndCtl->sync_standbys_defined)
941  {
942  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
943 
944  /*
945  * If synchronous_standby_names has been reset to empty, it's futile
946  * for backends to continue waiting. Since the user no longer wants
947  * synchronous replication, we'd better wake them up.
948  */
949  if (!sync_standbys_defined)
950  {
951  int i;
952 
953  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
954  SyncRepWakeQueue(true, i);
955  }
956 
957  /*
958  * Only allow people to join the queue when there are synchronous
959  * standbys defined. Without this interlock, there's a race
960  * condition: we might wake up all the current waiters; then, some
961  * backend that hasn't yet reloaded its config might go to sleep on
962  * the queue (and never wake up). This prevents that.
963  */
964  WalSndCtl->sync_standbys_defined = sync_standbys_defined;
965 
966  LWLockRelease(SyncRepLock);
967  }
968 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:867
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define SyncStandbysDefined()
Definition: syncrep.c:92
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
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, 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  /* Cap the level for anything other than commit to remote flush only. */
162  if (commit)
163  mode = SyncRepWaitMode;
164  else
166 
167  /*
168  * Fast exit if user has not requested sync replication.
169  */
170  if (!SyncRepRequested())
171  return;
172 
174  Assert(WalSndCtl != NULL);
175 
176  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
178 
179  /*
180  * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is not
181  * set. See SyncRepUpdateSyncStandbysDefined.
182  *
183  * Also check that the standby hasn't already replied. Unlikely race
184  * condition but we'll be fetching that cache line anyway so it's likely
185  * to be a low cost check.
186  */
188  lsn <= WalSndCtl->lsn[mode])
189  {
190  LWLockRelease(SyncRepLock);
191  return;
192  }
193 
194  /*
195  * Set our waitLSN so WALSender will know when to wake us, and add
196  * ourselves to the queue.
197  */
198  MyProc->waitLSN = lsn;
200  SyncRepQueueInsert(mode);
201  Assert(SyncRepQueueIsOrderedByLSN(mode));
202  LWLockRelease(SyncRepLock);
203 
204  /* Alter ps display to show waiting for sync rep. */
206  {
207  int len;
208 
209  old_status = get_ps_display(&len);
210  new_status = (char *) palloc(len + 32 + 1);
211  memcpy(new_status, old_status, len);
212  sprintf(new_status + len, " waiting for %X/%X",
213  (uint32) (lsn >> 32), (uint32) lsn);
214  set_ps_display(new_status);
215  new_status[len] = '\0'; /* truncate off " waiting ..." */
216  }
217 
218  /*
219  * Wait for specified LSN to be confirmed.
220  *
221  * Each proc has its own wait latch, so we perform a normal latch
222  * check/wait loop here.
223  */
224  for (;;)
225  {
226  int rc;
227 
228  /* Must reset the latch before testing state. */
230 
231  /*
232  * Acquiring the lock is not needed, the latch ensures proper
233  * barriers. If it looks like we're done, we must really be done,
234  * because once walsender changes the state to SYNC_REP_WAIT_COMPLETE,
235  * it will never update it again, so we can't be seeing a stale value
236  * in that case.
237  */
239  break;
240 
241  /*
242  * If a wait for synchronous replication is pending, we can neither
243  * acknowledge the commit nor raise ERROR or FATAL. The latter would
244  * lead the client to believe that the transaction aborted, which is
245  * not true: it's already committed locally. The former is no good
246  * either: the client has requested synchronous replication, and is
247  * entitled to assume that an acknowledged commit is also replicated,
248  * which might not be true. So in this case we issue a WARNING (which
249  * some clients may be able to interpret) and shut off further output.
250  * We do NOT reset ProcDiePending, so that the process will die after
251  * the commit is cleaned up.
252  */
253  if (ProcDiePending)
254  {
256  (errcode(ERRCODE_ADMIN_SHUTDOWN),
257  errmsg("canceling the wait for synchronous replication and terminating connection due to administrator command"),
258  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
261  break;
262  }
263 
264  /*
265  * It's unclear what to do if a query cancel interrupt arrives. We
266  * can't actually abort at this point, but ignoring the interrupt
267  * altogether is not helpful, so we just terminate the wait with a
268  * suitable warning.
269  */
270  if (QueryCancelPending)
271  {
272  QueryCancelPending = false;
274  (errmsg("canceling wait for synchronous replication due to user request"),
275  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
277  break;
278  }
279 
280  /*
281  * Wait on latch. Any condition that should wake us up will set the
282  * latch, so no need for timeout.
283  */
286 
287  /*
288  * If the postmaster dies, we'll probably never get an acknowledgment,
289  * because all the wal sender processes will exit. So just bail out.
290  */
291  if (rc & WL_POSTMASTER_DEATH)
292  {
293  ProcDiePending = true;
296  break;
297  }
298  }
299 
300  /*
301  * WalSender has checked our LSN and has removed us from queue. Clean up
302  * state and leave. It's OK to reset these shared memory fields without
303  * holding SyncRepLock, because any walsenders will ignore us anyway when
304  * we're not on the queue. We need a read barrier to make sure we see the
305  * changes to the queue link (this might be unnecessary without
306  * assertions, but better safe than sorry).
307  */
308  pg_read_barrier();
311  MyProc->waitLSN = 0;
312 
313  if (new_status)
314  {
315  /* Reset ps display */
316  set_ps_display(new_status);
317  pfree(new_status);
318  }
319 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
volatile uint32 InterruptHoldoffCount
Definition: globals.c:36
static void SyncRepCancelWait(void)
Definition: syncrep.c:361
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:328
PGPROC * MyProc
Definition: proc.c:67
#define SYNC_REP_WAITING
Definition: syncrep.h:32
#define Min(x, y)
Definition: c.h:920
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
WalSndCtlData * WalSndCtl
Definition: walsender.c:109
int errcode(int sqlerrcode)
Definition: elog.c:610
void ResetLatch(Latch *latch)
Definition: latch.c:540
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:365
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void set_ps_display(const char *activity)
Definition: ps_status.c:349
#define sprintf
Definition: port.h:195
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: dest.h:89
const char * get_ps_display(int *displen)
Definition: ps_status.c:430
int errdetail(const char *fmt,...)
Definition: elog.c:957
unsigned int uint32
Definition: c.h:367
#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 ereport(elevel,...)
Definition: elog.h:144
#define SYNC_REP_WAIT_COMPLETE
Definition: syncrep.h:33
#define Assert(condition)
Definition: c.h:738
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:1208
SHM_QUEUE syncRepLinks
Definition: proc.h:162
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
struct Latch * MyLatch
Definition: globals.c:54
int syncRepState
Definition: proc.h:161
CommandDest whereToSendOutput
Definition: postgres.c:91
static int SyncRepWaitMode
Definition: syncrep.c:98
#define WL_LATCH_SET
Definition: latch.h:124
XLogRecPtr waitLSN
Definition: proc.h:160

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.