]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Honor passed-in database OIDs in pgstat_database.c master github/master
authorMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:02:52 +0000 (17:02 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:02:52 +0000 (17:02 +0900)
Three routines in pgstat_database.c incorrectly ignore the database OID
provided by their caller, using MyDatabaseId instead:
- pgstat_report_connect()
- pgstat_report_disconnect()
- pgstat_reset_database_timestamp()

The first two functions, for connection and disconnection, each have a
single caller that already passes MyDatabaseId.  This was harmless,
still incorrect.

The timestamp reset function also has a single caller, but in this case
the issue has a real impact: it fails to reset the timestamp for the
shared-database entry (datid=0) when operating on shared objects.  This
situation can occur, for example, when resetting counters for shared
relations via pg_stat_reset_single_table_counters().

There is currently one test in the tree that checks the reset of a
shared relation, for pg_shdescription, we rely on it to check what is
stored in pg_stat_database.  As stats_reset may be NULL, two resets are
done to provide a baseline for comparison.

Author: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Dapeng Wang <wangdp20191008@gmail.com>
Discussion: https://postgr.es/m/ABBD5026-506F-4006-A569-28F72C188693@gmail.com
Backpatch-through: 15

src/backend/utils/activity/pgstat_database.c
src/test/regress/expected/stats.out
src/test/regress/sql/stats.sql

index f1846d3236ccf9fb3d0961f66d346a9f351d045d..7f3bc0165931cc11f00ebe6c3feaede65f85eda8 100644 (file)
@@ -243,7 +243,7 @@ pgstat_report_connect(Oid dboid)
 
        pgLastSessionReportTime = MyStartTimestamp;
 
-       dbentry = pgstat_prep_database_pending(MyDatabaseId);
+       dbentry = pgstat_prep_database_pending(dboid);
        dbentry->sessions++;
 }
 
@@ -258,7 +258,7 @@ pgstat_report_disconnect(Oid dboid)
        if (!pgstat_should_report_connstat())
                return;
 
-       dbentry = pgstat_prep_database_pending(MyDatabaseId);
+       dbentry = pgstat_prep_database_pending(dboid);
 
        switch (pgStatSessionEndCause)
        {
@@ -419,7 +419,7 @@ pgstat_reset_database_timestamp(Oid dboid, TimestampTz ts)
        PgStat_EntryRef *dbref;
        PgStatShared_Database *dbentry;
 
-       dbref = pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, MyDatabaseId, InvalidOid,
+       dbref = pgstat_get_entry_ref_locked(PGSTAT_KIND_DATABASE, dboid, InvalidOid,
                                                                                false);
 
        dbentry = (PgStatShared_Database *) dbref->shared_stats;
index 55009cfcc7d05e81a6e0c547e27b3e21b7c588b5..e5dcb85abd8d47edb8a55e57ad2c22d591b35fec 100644 (file)
@@ -924,6 +924,8 @@ SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
  t
 (1 row)
 
+-- stats_reset may not be set for datid=0 and shared objects in
+-- pg_stat_database, so reset once.
 SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
  pg_stat_reset_single_table_counters 
 -------------------------------------
@@ -937,6 +939,22 @@ SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
  f
 (1 row)
 
+SELECT stats_reset AS shared_db_reset_before
+  FROM pg_stat_database WHERE datid = 0 \gset
+-- Second reset for comparison.
+SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
+ pg_stat_reset_single_table_counters 
+-------------------------------------
+(1 row)
+
+SELECT stats_reset > :'shared_db_reset_before'::timestamptz AS has_updated
+  FROM pg_stat_database WHERE datid = 0;
+ has_updated 
+-------------
+ t
+(1 row)
+
 -- set back comment
 \if :{?description_before}
   COMMENT ON DATABASE :"datname" IS :'description_before';
index 158f3ca6ebe4ec268761b16370b76285365b4fb0..610fd21fae447cb8222a053c1d673dfb4f56a9ef 100644 (file)
@@ -416,9 +416,17 @@ COMMIT;
 -- check that the stats are reset.
 SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
   WHERE relid = 'pg_shdescription'::regclass;
+-- stats_reset may not be set for datid=0 and shared objects in
+-- pg_stat_database, so reset once.
 SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
 SELECT (n_tup_ins + n_tup_upd) > 0 AS has_data FROM pg_stat_all_tables
   WHERE relid = 'pg_shdescription'::regclass;
+SELECT stats_reset AS shared_db_reset_before
+  FROM pg_stat_database WHERE datid = 0 \gset
+-- Second reset for comparison.
+SELECT pg_stat_reset_single_table_counters('pg_shdescription'::regclass);
+SELECT stats_reset > :'shared_db_reset_before'::timestamptz AS has_updated
+  FROM pg_stat_database WHERE datid = 0;
 
 -- set back comment
 \if :{?description_before}