Fix race condition in stats.sql added in 5264add7847
authorAndres Freund <[email protected]>
Fri, 16 Sep 2022 17:07:12 +0000 (10:07 -0700)
committerAndres Freund <[email protected]>
Fri, 16 Sep 2022 18:28:20 +0000 (11:28 -0700)
Very occasionally the stats test failed due to the number of sessions not
being updated yet. Likely this requires that there is contention on the
database's stats entry. Solve this by forcing pending stats to be flushed
before fetching the stats.

I verified that there are no other test failures after making
pgstat_report_stat() only flush stats when force = true.

Per message from Tom Lane and buildfarm member crake.

Discussion: https://postgr.es/m/3428246.1663271992@sss.pgh.pa.us
Backpatch: 15-, where 5264add7847 added the test

src/test/regress/expected/stats.out
src/test/regress/sql/stats.sql

index 6b233ff4c05f458c09d63305f34aac53165cce7b..628df302df0edf9ec78f33947cfa2b2294f34761 100644 (file)
@@ -560,6 +560,12 @@ DROP TABLE prevstats;
 -- Test that sessions is incremented when a new session is started in pg_stat_database
 SELECT sessions AS db_stat_sessions FROM pg_stat_database WHERE datname = (SELECT current_database()) \gset
 \c
+SELECT pg_stat_force_next_flush();
+ pg_stat_force_next_flush 
+--------------------------
+(1 row)
+
 SELECT sessions > :db_stat_sessions FROM pg_stat_database WHERE datname = (SELECT current_database());
  ?column? 
 ----------
index 096f00ce8bef2cc52e6055da98d21ddc9f44fc2d..2f39778b15500fba78ec8a3d79a1c1f53e345deb 100644 (file)
@@ -294,6 +294,7 @@ DROP TABLE prevstats;
 -- Test that sessions is incremented when a new session is started in pg_stat_database
 SELECT sessions AS db_stat_sessions FROM pg_stat_database WHERE datname = (SELECT current_database()) \gset
 \c
+SELECT pg_stat_force_next_flush();
 SELECT sessions > :db_stat_sessions FROM pg_stat_database WHERE datname = (SELECT current_database());
 
 -- Test pg_stat_bgwriter checkpointer-related stats, together with pg_stat_wal