You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ReplicationConnection.SendStatusUpdate, should I call it manually #6144
I have a quiet database with little activity. Using NPG sql to monitor its activity though logical replication.
I saw my wal piles up. I did follow the demo to call : conn.SetReplicationStatus(message.WalEnd); after each message. Then I found this function: public async Task SendStatusUpdate(CancellationToken cancellationToken = default)
It seems it's got called automatically.
So I am wondering what happened. The fact is:
WAL works fine with other slots except this quiet database. Once I added a busy table to the publication, wal looks fine.
I am using 9.0.2, maybe I should migrate to 9.0.3?
Thanks
I had a similar problem in one of my apps. I solved it by using a kind of heartbeat* in order to make the little active DB a bit more active and the troubles went away.
* a separate heartbeat-table (that's included in the replication) where a timer writes something, and which gets later on cleaned w/ a pg_cron job
In the code it's like this
awaitforeach(PgOutputReplicationMessagemessageinconnection.StartReplication(slot,outputReplicationOptions,cancellationToken)){// Process message omitted hereconnection.SetReplicationStatus(message.WalEnd);// Force that status to be updated in the DB. See description of SetReplicationStatus// for further info.// For outbox patterns this avoids that older messages are sent more than once.// Note: it's still at-least-once and not exactly-once.awaitconnection.SendStatusUpdate(cancellationToken);}
You can call SendStatusUpdate() manually to force sending a status update, but you usually shouldn't have to. Note that this adds a network roundtrip every time you call it (which might not be a big problem in a rather inactive database).
Apart from this, there are two configuration settings that trigger the automated status updates:
The ReplicationConnection's WalReceiverStatusInterval which you can use to set the time interval at which Npgsql sends automated status updates to the server (current default is 10s).
wal_sender_timeout (current default is 60s) is configured at the server (PostgreSQL) side. Before this timeout expires, the server sends a message to the client forcefully asking it to send a status update, which triggers a forced status update in Npgsql. If the server doesn't hear back in time, it disconnects (also see our test for this).
Yes, I checked the slots info, restart lsn, and monitor the WAL piles up by minutes. Based on on your description, the default settings should work fine. But it's not working for me. The restart lsn does not change.
Maybe SendStatusUpdate() does not work after all, or there's a bug in NPGSQL as how it's implemented?
Currently I have to fall back to a heartbeat activity like gfoidl described.
Or maybe StatusUpdate can not solve the problem? After all, it sends the last flushed lsn, which is old anyway. It's really up to the server to interpret the info.
Have you get it working on a sleeping data in a busy postgresql cluster? If it worked for you, I will dig deeper into it.
Or maybe StatusUpdate can not solve the problem? After all, it sends the last flushed lsn, which is old anyway.
That's true, yes, and the status updates triggered by WalReceiverStatusInterval or by wal_sender_timeout won't help here, since they'd only send an updated LastReceivedLsn if your code didn't update LastFlushedLsn or LastAppliedLsn in the meantime (either by setting them explicitly or by calling SetReplicationStatus()). This also means that a forced SendStatusUpdate() cannot help here since it is doing exactly the same, but in a forced way.
OK, finally I solved it by calling "pg_logical_emit_message" on a connection periodically in my replication code
This sounds like a good, low-overhead solution for server versions that support this (>13.0).
Just for documentation purposes, in case others try to do this as well:
Youn need to handle the LogicalDecodingMessage in your client code and call SendStatusUpdate() using the value from the WalEnd property of LogicalDecodingMessage so that the server can update its restart lsn.
I use the standard demo code:
conn.SetReplicationStatus(message.WalEnd);
didn't call SendStatusUpdate(). I believe it's eventually called by the framework. It works fine now.
Yeah, the WalEnd of the abstract ReplicationMessage superclass is the same and yes, calling SetReplicationStatus() is enough because a timer based status update is sent every 10 seconds by default, so there's no need to enforce one.
Activity
gfoidl commentedon Jun 23, 2025
I had a similar problem in one of my apps. I solved it by using a kind of heartbeat* in order to make the little active DB a bit more active and the troubles went away.
* a separate heartbeat-table (that's included in the replication) where a timer writes something, and which gets later on cleaned w/ a pg_cron job
In the code it's like this
Same background info that I found back then:
Brar commentedon Jun 23, 2025
You can call
SendStatusUpdate()manually to force sending a status update, but you usually shouldn't have to. Note that this adds a network roundtrip every time you call it (which might not be a big problem in a rather inactive database).Apart from this, there are two configuration settings that trigger the automated status updates:
ReplicationConnection'sWalReceiverStatusIntervalwhich you can use to set the time interval at which Npgsql sends automated status updates to the server (current default is 10s).wal_sender_timeout(current default is 60s) is configured at the server (PostgreSQL) side. Before this timeout expires, the server sends a message to the client forcefully asking it to send a status update, which triggers a forced status update in Npgsql. If the server doesn't hear back in time, it disconnects (also see our test for this).Do you have any indication that this mechnism is not working for you?
If yes, you should probably have a look at
pg_replication_slotsandpg_stat_replicationand and compare the LSN info in there with the LSN status on your client.Please also have a look at the PostgreSQL docs on monitoring streaming replication.
lazyclaw-labs commentedon Jun 23, 2025
Yes, I checked the slots info, restart lsn, and monitor the WAL piles up by minutes. Based on on your description, the default settings should work fine. But it's not working for me. The restart lsn does not change.
Maybe SendStatusUpdate() does not work after all, or there's a bug in NPGSQL as how it's implemented?
Currently I have to fall back to a heartbeat activity like gfoidl described.
Or maybe StatusUpdate can not solve the problem? After all, it sends the last flushed lsn, which is old anyway. It's really up to the server to interpret the info.
Have you get it working on a sleeping data in a busy postgresql cluster? If it worked for you, I will dig deeper into it.
lazyclaw-labs commentedon Jun 23, 2025
OK, finally I solved it by calling "pg_logical_emit_message" on a connection periodically in my replication code
Brar commentedon Jun 23, 2025
That's helpful.
That's true, yes, and the status updates triggered by
WalReceiverStatusIntervalor bywal_sender_timeoutwon't help here, since they'd only send an updatedLastReceivedLsnif your code didn't updateLastFlushedLsnorLastAppliedLsnin the meantime (either by setting them explicitly or by callingSetReplicationStatus()). This also means that a forcedSendStatusUpdate()cannot help here since it is doing exactly the same, but in a forced way.This sounds like a good, low-overhead solution for server versions that support this (>13.0).
Just for documentation purposes, in case others try to do this as well:
Youn need to handle the
LogicalDecodingMessagein your client code and callSendStatusUpdate()using the value from theWalEndproperty ofLogicalDecodingMessageso that the server can update its restart lsn.lazyclaw-labs commentedon Jun 23, 2025
I use the standard demo code:
conn.SetReplicationStatus(message.WalEnd);
didn't call SendStatusUpdate(). I believe it's eventually called by the framework. It works fine now.
Brar commentedon Jun 23, 2025
Yeah, the
WalEndof the abstractReplicationMessagesuperclass is the same and yes, callingSetReplicationStatus()is enough because a timer based status update is sent every 10 seconds by default, so there's no need to enforce one.