diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 9cf9eb0..0597d5d 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -114,6 +114,8 @@ int wal_sender_timeout = 60 * 1000; /* maximum time to send one * WAL data message */ bool log_replication_commands = false; +bool async_walsender_delay = false; + /* * State for WalSndWakeupRequest */ @@ -219,6 +221,7 @@ static long WalSndComputeSleeptime(TimestampTz now); static void WalSndPrepareWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write); static void WalSndWriteData(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xid, bool last_write); static XLogRecPtr WalSndWaitForWal(XLogRecPtr loc); +static bool is_async_wait_sync(XLogRecPtr SendRqstPtr); static void XLogRead(char *buf, XLogRecPtr startptr, Size count); @@ -2278,6 +2281,17 @@ XLogSendPhysical(void) return; } + /* If async_walsender_delay parameter is set, walsender for asynchronous + * replication waits until other walsender for synchronous repliation + * send WAL. */ + if (async_walsender_delay) + { + if (is_async_wait_sync(SendRqstPtr)) + { + return; + } + } + /* * Figure out how much to send in one message. If there's no more than * MAX_SEND_SIZE bytes to send, send everything. Otherwise send @@ -2885,3 +2899,38 @@ WalSndKeepaliveIfNecessary(TimestampTz now) WalSndShutdown(); } } + +/* + * This function is called when async_walsender_delay is set. + * walsender of asynchronous replication waits to send wal + * until other walsender of synchronous replication has sent. + * */ +static bool +is_async_wait_sync(XLogRecPtr SendRqstPtr) +{ + + WalSnd *mywalsnd = MyWalSnd; + int priority; + int i; + + /* if this process is for sync, not wait */ + priority = mywalsnd->sync_standby_priority; + if (priority != 0) + { + return false; + } + + /* Check if other walsenders has flushed WAL. */ + for (i = 0; i < max_wal_senders; i++) + { + WalSnd *walsnd = &WalSndCtl->walsnds[i]; + + priority = walsnd->sync_standby_priority; + if (priority != 0 && SendRqstPtr > walsnd->flush) + { + return true; + } + } + + return false; +} diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 0707f66..36db5a2 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -1657,6 +1657,16 @@ static struct config_bool ConfigureNamesBool[] = NULL, NULL, NULL }, + { + {"async_walsender_delay", PGC_POSTMASTER, REPLICATION_SENDING, + gettext_noop("Delay the walsender for asynchronous replication"), + NULL, + }, + &async_walsender_delay, + false, + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL, NULL diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 157d775..d075ab8 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -271,6 +271,8 @@ # in milliseconds; 0 disables #wal_retrieve_retry_interval = 5s # time to wait before retrying to # retrieve WAL after a failed attempt +#async_walsender_delay = off # wait for sending WAL to async standby + # until sync standby get WAL #------------------------------------------------------------------------------ diff --git a/src/include/replication/walsender.h b/src/include/replication/walsender.h index fe23f66..62f28bc 100644 --- a/src/include/replication/walsender.h +++ b/src/include/replication/walsender.h @@ -26,6 +26,7 @@ extern bool wake_wal_senders; extern int max_wal_senders; extern int wal_sender_timeout; extern bool log_replication_commands; +extern bool async_walsender_delay; extern void InitWalSender(void); extern void exec_replication_command(const char *query_string);