Skip to content

ReplicationConnection.SendStatusUpdate, should I call it manually #6144

Closed as not planned

Description

@lazyclaw-labs

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

Activity

  1. gfoidl commented on Jun 23, 2025

    @gfoidl
    Contributor

    my wal piles up

    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

    await foreach (PgOutputReplicationMessage message in connection.StartReplication(slot, outputReplicationOptions, cancellationToken))
    {
        // Process message omitted here
    
        connection.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.
        await connection.SendStatusUpdate(cancellationToken);
    }

    Same background info that I found back then:

  2. Brar commented on Jun 23, 2025

    @Brar
    Member

    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:

    1. 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).
    2. 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_slots and pg_stat_replication and 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.

  3. lazyclaw-labs commented on Jun 23, 2025

    @lazyclaw-labs
    Author

    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.

  4. lazyclaw-labs commented on Jun 23, 2025

    @lazyclaw-labs
    Author

    OK, finally I solved it by calling "pg_logical_emit_message" on a connection periodically in my replication code

  5. Brar commented on Jun 23, 2025

    @Brar
    Member

    The restart lsn does not change.

    That's helpful.

    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.

  6. lazyclaw-labs commented on Jun 23, 2025

    @lazyclaw-labs
    Author

    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.

  7. Brar commented on Jun 23, 2025

    @Brar
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions