Repository navigation
Description
Summary
A PgOutput consumer can acknowledge the end of each transaction it has applied, but it gets no ordered notification when PostgreSQL processes WAL that contains no published changes. As a result, its logical slot can retain WAL until the next published transaction, even while Npgsql continues to send status updates.
Steps to reproduce
On a local PostgreSQL server with wal_level=logical, create two tables and publish only one:
CREATE TABLE published (id integer);
CREATE TABLE unpublished (id integer);
CREATE PUBLICATION demo_pub FOR TABLE published WITH (publish = 'insert');
SELECT pg_create_logical_replication_slot('demo_slot', 'pgoutput');With Npgsql 11.0.0, run this top-level code using the same database and a login with replication privileges:
using Npgsql.Replication;
using Npgsql.Replication.PgOutput;
using Npgsql.Replication.PgOutput.Messages;
await using var connection = new LogicalReplicationConnection(args[0]);
await connection.Open();
await foreach (var message in connection.StartReplication(
new PgOutputReplicationSlot("demo_slot"),
new PgOutputReplicationOptions("demo_pub", PgOutputProtocolVersion.V1),
cancellationToken: default))
{
if (message is CommitMessage commit)
connection.SetReplicationStatus(commit.TransactionEndLsn);
}Insert into published and wait for its checkpoint. Then insert rows into unpublished and compare pg_stat_replication.write_lsn with pg_replication_slots.confirmed_flush_lsn. The write position advances after a keepalive, but the confirmed flush position stays at the earlier published commit.
Proposal
Please expose primary keepalives and their positions in order with the PgOutput stream, for example as opt-in messages. Consumers could then report applied and flushed progress when they reach a keepalive with all preceding transactions applied. The current implementation consumes the frame inside the iterator. PostgreSQL's own subscriber also uses received progress when no transaction remains to be flushed.
The workaround in #6144 uses pg_logical_emit_message to create a message the consumer can observe. An ordered keepalive notification would allow progress without periodic writes.
Further technical details
Npgsql 11.0.0, PostgreSQL 18.6, .NET 11, Windows with PostgreSQL in Docker.
Related tracking in Relayer is thomhurst/Relayer#1141.
Activity