Don't report stats in LogicalRepApplyLoop() when in xact.
authorAndres Freund
Fri, 13 May 2022 01:49:33 +0000 (18:49 -0700)
committerAndres Freund
Fri, 13 May 2022 01:54:26 +0000 (18:54 -0700)
pgstat_report_stat() is only supposed to be called outside of transactions. In
5891c7a8ed8 I added a pgstat_report_stat() call into LogicalRepApplyLoop()'s
timeout branch. While not commonly reached inside a transaction, it is
reachable (e.g. due to network bottlenecks or the sender being stalled / slow
for some reason).

To fix, add a !IsTransactionState() check.

No test added because there's no easy way to reproduce this case without
patching the code.

Reported-By: Erik Rijkers
Discussion: https://postgr.es/m/b3463b8c-2328-dcac-0136-af95715493c1@xs4all.nl

src/backend/replication/logical/worker.c

index 3b80ed92c0730e681b4e89940162172a7e0b74ff..fc210a9e7b947554fd2706add36d58f105bf515a 100644 (file)
@@ -2883,9 +2883,14 @@ LogicalRepApplyLoop(XLogRecPtr last_received)
 
            /*
             * Force reporting to ensure long idle periods don't lead to
-            * arbitrarily delayed stats.
+            * arbitrarily delayed stats. Stats can only be reported outside
+            * of (implicit or explicit) transactions. That shouldn't lead to
+            * stats being delayed for long, because transactions are either
+            * sent as a whole on commit or streamed. Streamed transactions
+            * are spilled to disk and applied on commit.
             */
-           pgstat_report_stat(true);
+           if (!IsTransactionState())
+               pgstat_report_stat(true);
        }
    }