同步复制注意点

       同步复制情况下,先刷写日志,然后记录clog并持久化,最后才等待备机接收日志后返回的ACK,如果备机有问题的话或复制链路异常,日志传输失败,此时用户客户端的commit会挂住,一旦主机异常宕机重启后,这个异常的事务会从日志中恢复出来,因为事务在日志中显示已提交,而相对客户而言是未提交的。这一点需要注意。

RecordTransactionCommit
  if ((wrote_xlog && markXidCommitted &&
     synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
    forceSyncCommit || nrels > 0){//同步
    XLogFlush(XactLastRecEnd);
    /*
     * Now we may update the CLOG, if we wrote a COMMIT record above
     */
    if (markXidCommitted)
      TransactionIdCommitTree(xid, nchildren, children);
  }else{
    XLogSetAsyncXactLSN(XactLastRecEnd);
    if (markXidCommitted)
      TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
  }
  ...
  if (wrote_xlog && markXidCommitted)
    SyncRepWaitForLSN(XactLastRecEnd, true);
    |--  if (!SyncRepRequested())
    |    return;
    |  for(;;){
    |    等待确认
    |--  }
  ...
  
//同步复制
#define SyncRepRequested() \
  (max_wal_senders > 0 && synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)

你可能感兴趣的:(PostgreSQL源码分析)