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  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 SyncRepConfigData SyncRepConfigData
 

Functions

void SyncRepWaitForLSN (XLogRecPtr lsn, bool commit)
 
void SyncRepCleanupAtProcExit (void)
 
void SyncRepInitConfig (void)
 
void SyncRepReleaseWaiters (void)
 
ListSyncRepGetSyncStandbys (bool *am_sync)
 
void SyncRepUpdateSyncStandbysDefined (void)
 
bool check_synchronous_standby_names (char **newval, void **extra, GucSource source)
 
void assign_synchronous_standby_names (const char *newval, void *extra)
 
void assign_synchronous_commit (int newval, void *extra)
 
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

◆ SYNC_REP_QUORUM

#define SYNC_REP_QUORUM   1

Definition at line 37 of file syncrep.h.

Referenced by SyncRepGetSyncStandbysQuorum().

◆ 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

Function Documentation

◆ assign_synchronous_commit()

void assign_synchronous_commit ( int  newval,
void *  extra 
)

Definition at line 1197 of file syncrep.c.

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

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

◆ assign_synchronous_standby_names()

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

Definition at line 1191 of file syncrep.c.

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

◆ check_synchronous_standby_names()

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

Definition at line 1134 of file syncrep.c.

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

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

◆ 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 362 of file syncrep.c.

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

Referenced by ProcKill().

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

◆ SyncRepGetSyncStandbys()

List* SyncRepGetSyncStandbys ( bool am_sync)

Definition at line 681 of file syncrep.c.

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

Referenced by pg_stat_get_wal_senders(), and SyncRepGetSyncRecPtr().

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

◆ SyncRepInitConfig()

void SyncRepInitConfig ( void  )

Definition at line 383 of file syncrep.c.

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

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

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

◆ SyncRepReleaseWaiters()

void SyncRepReleaseWaiters ( void  )

Definition at line 411 of file syncrep.c.

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

Referenced by ProcessStandbyReplyMessage().

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

◆ SyncRepUpdateSyncStandbysDefined()

void SyncRepUpdateSyncStandbysDefined ( void  )

Definition at line 1058 of file syncrep.c.

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

Referenced by UpdateSharedMemoryConfig().

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

◆ SyncRepWaitForLSN()

void SyncRepWaitForLSN ( XLogRecPtr  lsn,
bool  commit 
)

Definition at line 146 of file syncrep.c.

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

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

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

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.