]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Honor passed-in database OIDs in pgstat_database.c
authorMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:03:08 +0000 (17:03 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:03:08 +0000 (17:03 +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 813aa359fa034ffd6a8b3d6f69faeb53dff1964c..91cf0fd346a97eee6c50eb081d54a50831dd3ad9 100644 (file)
@@ -197,7 +197,7 @@ pgstat_report_connect(Oid dboid)
 
        pgLastSessionReportTime = MyStartTimestamp;
 
-       dbentry = pgstat_prep_database_pending(MyDatabaseId);
+       dbentry = pgstat_prep_database_pending(dboid);
        dbentry->sessions++;
 }
 
@@ -212,7 +212,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)
        {
@@ -356,7 +356,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 94187e59cfb1e4d3514f9c93bc14dae0fbbbb9b0..00efcedc99aab800b43c898aac0190e9f362d68a 100644 (file)
@@ -791,6 +791,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 
 -------------------------------------
@@ -804,6 +806,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 1e21e55c6d9cbce2fff316ce85820072f51e4120..44952a7d64b4aed47161f6124adf791f0bc1c1f8 100644 (file)
@@ -396,9 +396,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}