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

1220 {
1221  switch (newval)
1222  {
1225  break;
1228  break;
1231  break;
1232  default:
1234  break;
1235  }
1236 }
#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 1213 of file syncrep.c.

1214 {
1215  SyncRepConfig = (SyncRepConfigData *) extra;
1216 }
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97

◆ check_synchronous_standby_names()

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

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

1157 {
1158  if (*newval != NULL && (*newval)[0] != '\0')
1159  {
1160  int parse_rc;
1161  SyncRepConfigData *pconf;
1162 
1163  /* Reset communication variables to ensure a fresh start */
1164  syncrep_parse_result = NULL;
1165  syncrep_parse_error_msg = NULL;
1166 
1167  /* Parse the synchronous_standby_names string */
1169  parse_rc = syncrep_yyparse();
1171 
1172  if (parse_rc != 0 || syncrep_parse_result == NULL)
1173  {
1174  GUC_check_errcode(ERRCODE_SYNTAX_ERROR);
1177  else
1178  GUC_check_errdetail("synchronous_standby_names parser failed");
1179  return false;
1180  }
1181 
1182  if (syncrep_parse_result->num_sync <= 0)
1183  {
1184  GUC_check_errmsg("number of synchronous standbys (%d) must be greater than zero",
1186  return false;
1187  }
1188 
1189  /* GUC extra value must be malloc'd, not palloc'd */
1190  pconf = (SyncRepConfigData *)
1192  if (pconf == NULL)
1193  return false;
1195 
1196  *extra = (void *) pconf;
1197 
1198  /*
1199  * We need not explicitly clean up syncrep_parse_result. It, and any
1200  * other cruft generated during parsing, will be freed when the
1201  * current memory context is deleted. (This code is generally run in
1202  * a short-lived context used for config file processing, so that will
1203  * not be very long.)
1204  */
1205  }
1206  else
1207  *extra = NULL;
1208 
1209  return true;
1210 }
void syncrep_scanner_finish(void)
void syncrep_scanner_init(const char *query_string)
#define GUC_check_errdetail
Definition: guc.h:413
char * syncrep_parse_error_msg
#define GUC_check_errmsg
Definition: guc.h:409
void GUC_check_errcode(int sqlerrcode)
Definition: guc.c:10759
#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 369 of file syncrep.c.

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

Referenced by ProcKill().

370 {
371  /*
372  * First check if we are removed from the queue without the lock to not
373  * slow down backend exit.
374  */
376  {
377  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
378 
379  /* maybe we have just been removed, so recheck */
382 
383  LWLockRelease(SyncRepLock);
384  }
385 }
PGPROC * MyProc
Definition: proc.c:67
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 700 of file syncrep.c.

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

Referenced by pg_stat_get_wal_senders(), and SyncRepGetSyncRecPtr().

701 {
702  Assert(LWLockHeldByMe(SyncRepLock));
703 
704  /* Set default result */
705  if (am_sync != NULL)
706  *am_sync = false;
707 
708  /* Quick exit if sync replication is not requested */
709  if (SyncRepConfig == NULL)
710  return NIL;
711 
715 }
#define NIL
Definition: pg_list.h:65
static List * SyncRepGetSyncStandbysPriority(bool *am_sync)
Definition: syncrep.c:794
#define SYNC_REP_PRIORITY
Definition: syncrep.h:36
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
uint8 syncrep_method
Definition: syncrep.h:51
SyncRepConfigData * SyncRepConfig
Definition: syncrep.c:97
static List * SyncRepGetSyncStandbysQuorum(bool *am_sync)
Definition: syncrep.c:728
#define Assert(condition)
Definition: c.h:739

◆ SyncRepInitConfig()

void SyncRepInitConfig ( void  )

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

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

◆ SyncRepReleaseWaiters()

void SyncRepReleaseWaiters ( void  )

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

427 {
428  volatile WalSndCtlData *walsndctl = WalSndCtl;
429  XLogRecPtr writePtr;
430  XLogRecPtr flushPtr;
431  XLogRecPtr applyPtr;
432  bool got_recptr;
433  bool am_sync;
434  int numwrite = 0;
435  int numflush = 0;
436  int numapply = 0;
437 
438  /*
439  * If this WALSender is serving a standby that is not on the list of
440  * potential sync standbys then we have nothing to do. If we are still
441  * starting up, still running base backup or the current flush position is
442  * still invalid, then leave quickly also. Streaming or stopping WAL
443  * senders are allowed to release waiters.
444  */
445  if (MyWalSnd->sync_standby_priority == 0 ||
449  {
450  announce_next_takeover = true;
451  return;
452  }
453 
454  /*
455  * We're a potential sync standby. Release waiters if there are enough
456  * sync standbys and we are considered as sync.
457  */
458  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
459 
460  /*
461  * Check whether we are a sync standby or not, and calculate the synced
462  * positions among all sync standbys.
463  */
464  got_recptr = SyncRepGetSyncRecPtr(&writePtr, &flushPtr, &applyPtr, &am_sync);
465 
466  /*
467  * If we are managing a sync standby, though we weren't prior to this,
468  * then announce we are now a sync standby.
469  */
470  if (announce_next_takeover && am_sync)
471  {
472  announce_next_takeover = false;
473 
475  ereport(LOG,
476  (errmsg("standby \"%s\" is now a synchronous standby with priority %u",
478  else
479  ereport(LOG,
480  (errmsg("standby \"%s\" is now a candidate for quorum synchronous standby",
481  application_name)));
482  }
483 
484  /*
485  * If the number of sync standbys is less than requested or we aren't
486  * managing a sync standby then just leave.
487  */
488  if (!got_recptr || !am_sync)
489  {
490  LWLockRelease(SyncRepLock);
491  announce_next_takeover = !am_sync;
492  return;
493  }
494 
495  /*
496  * Set the lsn first so that when we wake backends they will release up to
497  * this location.
498  */
499  if (walsndctl->lsn[SYNC_REP_WAIT_WRITE] < writePtr)
500  {
501  walsndctl->lsn[SYNC_REP_WAIT_WRITE] = writePtr;
502  numwrite = SyncRepWakeQueue(false, SYNC_REP_WAIT_WRITE);
503  }
504  if (walsndctl->lsn[SYNC_REP_WAIT_FLUSH] < flushPtr)
505  {
506  walsndctl->lsn[SYNC_REP_WAIT_FLUSH] = flushPtr;
507  numflush = SyncRepWakeQueue(false, SYNC_REP_WAIT_FLUSH);
508  }
509  if (walsndctl->lsn[SYNC_REP_WAIT_APPLY] < applyPtr)
510  {
511  walsndctl->lsn[SYNC_REP_WAIT_APPLY] = applyPtr;
512  numapply = SyncRepWakeQueue(false, SYNC_REP_WAIT_APPLY);
513  }
514 
515  LWLockRelease(SyncRepLock);
516 
517  elog(DEBUG3, "released %d procs up to write %X/%X, %d procs up to flush %X/%X, %d procs up to apply %X/%X",
518  numwrite, (uint32) (writePtr >> 32), (uint32) writePtr,
519  numflush, (uint32) (flushPtr >> 32), (uint32) flushPtr,
520  numapply, (uint32) (applyPtr >> 32), (uint32) applyPtr);
521 }
static bool SyncRepGetSyncRecPtr(XLogRecPtr *writePtr, XLogRecPtr *flushPtr, XLogRecPtr *applyPtr, bool *am_sync)
Definition: syncrep.c:536
#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:1011
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:359
#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:536
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define SYNC_REP_WAIT_WRITE
Definition: syncrep.h:24

◆ SyncRepUpdateSyncStandbysDefined()

void SyncRepUpdateSyncStandbysDefined ( void  )

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

1081 {
1082  bool sync_standbys_defined = SyncStandbysDefined();
1083 
1084  if (sync_standbys_defined != WalSndCtl->sync_standbys_defined)
1085  {
1086  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
1087 
1088  /*
1089  * If synchronous_standby_names has been reset to empty, it's futile
1090  * for backends to continue waiting. Since the user no longer wants
1091  * synchronous replication, we'd better wake them up.
1092  */
1093  if (!sync_standbys_defined)
1094  {
1095  int i;
1096 
1097  for (i = 0; i < NUM_SYNC_REP_WAIT_MODE; i++)
1098  SyncRepWakeQueue(true, i);
1099  }
1100 
1101  /*
1102  * Only allow people to join the queue when there are synchronous
1103  * standbys defined. Without this interlock, there's a race
1104  * condition: we might wake up all the current waiters; then, some
1105  * backend that hasn't yet reloaded its config might go to sleep on
1106  * the queue (and never wake up). This prevents that.
1107  */
1108  WalSndCtl->sync_standbys_defined = sync_standbys_defined;
1109 
1110  LWLockRelease(SyncRepLock);
1111  }
1112 }
WalSndCtlData * WalSndCtl
Definition: walsender.c:108
static int SyncRepWakeQueue(bool all, int mode)
Definition: syncrep.c:1011
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(), 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().

147 {
148  char *new_status = NULL;
149  const char *old_status;
150  int mode;
151 
152  /*
153  * This should be called while holding interrupts during a transaction
154  * commit to prevent the follow-up shared memory queue cleanups to be
155  * influenced by external interruptions.
156  */
158 
159  /* Cap the level for anything other than commit to remote flush only. */
160  if (commit)
161  mode = SyncRepWaitMode;
162  else
164 
165  /*
166  * Fast exit if user has not requested sync replication.
167  */
168  if (!SyncRepRequested())
169  return;
170 
172  Assert(WalSndCtl != NULL);
173 
174  LWLockAcquire(SyncRepLock, LW_EXCLUSIVE);
176 
177  /*
178  * We don't wait for sync rep if WalSndCtl->sync_standbys_defined is not
179  * set. See SyncRepUpdateSyncStandbysDefined.
180  *
181  * Also check that the standby hasn't already replied. Unlikely race
182  * condition but we'll be fetching that cache line anyway so it's likely
183  * to be a low cost check.
184  */
186  lsn <= WalSndCtl->lsn[mode])
187  {
188  LWLockRelease(SyncRepLock);
189  return;
190  }
191 
192  /*
193  * Set our waitLSN so WALSender will know when to wake us, and add
194  * ourselves to the queue.
195  */
196  MyProc->waitLSN = lsn;
198  SyncRepQueueInsert(mode);
199  Assert(SyncRepQueueIsOrderedByLSN(mode));
200  LWLockRelease(SyncRepLock);
201 
202  /* Alter ps display to show waiting for sync rep. */
204  {
205  int len;
206 
207  old_status = get_ps_display(&len);
208  new_status = (char *) palloc(len + 32 + 1);
209  memcpy(new_status, old_status, len);
210  sprintf(new_status + len, " waiting for %X/%X",
211  (uint32) (lsn >> 32), (uint32) lsn);
212  set_ps_display(new_status, false);
213  new_status[len] = '\0'; /* truncate off " waiting ..." */
214  }
215 
216  /*
217  * Wait for specified LSN to be confirmed.
218  *
219  * Each proc has its own wait latch, so we perform a normal latch
220  * check/wait loop here.
221  */
222  for (;;)
223  {
224  int rc;
225 
226  /* Must reset the latch before testing state. */
228 
229  /*
230  * Acquiring the lock is not needed, the latch ensures proper
231  * barriers. If it looks like we're done, we must really be done,
232  * because once walsender changes the state to SYNC_REP_WAIT_COMPLETE,
233  * it will never update it again, so we can't be seeing a stale value
234  * in that case.
235  */
237  break;
238 
239  /*
240  * If a wait for synchronous replication is pending, we can neither
241  * acknowledge the commit nor raise ERROR or FATAL. The latter would
242  * lead the client to believe that the transaction aborted, which is
243  * not true: it's already committed locally. The former is no good
244  * either: the client has requested synchronous replication, and is
245  * entitled to assume that an acknowledged commit is also replicated,
246  * which might not be true. So in this case we issue a WARNING (which
247  * some clients may be able to interpret) and shut off further output.
248  * We do NOT reset ProcDiePending, so that the process will die after
249  * the commit is cleaned up.
250  */
251  if (ProcDiePending)
252  {
254  (errcode(ERRCODE_ADMIN_SHUTDOWN),
255  errmsg("canceling the wait for synchronous replication and terminating connection due to administrator command"),
256  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
259  break;
260  }
261 
262  /*
263  * It's unclear what to do if a query cancel interrupt arrives. We
264  * can't actually abort at this point, but ignoring the interrupt
265  * altogether is not helpful, so we just terminate the wait with a
266  * suitable warning.
267  */
268  if (QueryCancelPending)
269  {
270  QueryCancelPending = false;
272  (errmsg("canceling wait for synchronous replication due to user request"),
273  errdetail("The transaction has already committed locally, but might not have been replicated to the standby.")));
275  break;
276  }
277 
278  /*
279  * Wait on latch. Any condition that should wake us up will set the
280  * latch, so no need for timeout.
281  */
284 
285  /*
286  * If the postmaster dies, we'll probably never get an acknowledgment,
287  * because all the wal sender processes will exit. So just bail out.
288  */
289  if (rc & WL_POSTMASTER_DEATH)
290  {
291  ProcDiePending = true;
294  break;
295  }
296  }
297 
298  /*
299  * WalSender has checked our LSN and has removed us from queue. Clean up
300  * state and leave. It's OK to reset these shared memory fields without
301  * holding SyncRepLock, because any walsenders will ignore us anyway when
302  * we're not on the queue. We need a read barrier to make sure we see the
303  * changes to the queue link (this might be unnecessary without
304  * assertions, but better safe than sorry).
305  */
306  pg_read_barrier();
309  MyProc->waitLSN = 0;
310 
311  if (new_status)
312  {
313  /* Reset ps display */
314  set_ps_display(new_status, false);
315  pfree(new_status);
316  }
317 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
volatile uint32 InterruptHoldoffCount
Definition: globals.c:36
static void SyncRepCancelWait(void)
Definition: syncrep.c:359
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:326
PGPROC * MyProc
Definition: proc.c:67
#define SYNC_REP_WAITING
Definition: syncrep.h:32
#define Min(x, y)
Definition: c.h:911
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:608
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:1056
Definition: dest.h:88
const char * get_ps_display(int *displen)
Definition: ps_status.c:412
int errdetail(const char *fmt,...)
Definition: elog.c:955
unsigned int uint32
Definition: c.h:359
#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:739
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:1122
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
struct Latch * MyLatch
Definition: globals.c:54
int syncRepState
Definition: proc.h:152
CommandDest whereToSendOutput
Definition: postgres.c:89
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.