]> 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:10 +0000 (17:03 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:03:10 +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 e7b5633a40b3769e79716fa63af0be883efd2f52..f4f5f1e85aed46cb12518d4b1063eb8fa8cda5b5 100644 (file)
@@ -194,7 +194,7 @@ pgstat_report_connect(Oid dboid)
 
        pgLastSessionReportTime = MyStartTimestamp;
 
-       dbentry = pgstat_prep_database_pending(MyDatabaseId);
+       dbentry = pgstat_prep_database_pending(dboid);
        dbentry->n_sessions++;
 }
 
@@ -209,7 +209,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)
        {
@@ -353,7 +353,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 ffcba3060679315754b0b2664fcba1abb050bdcd..8195deb995e25618300def959c878816fd48c017 100644 (file)
@@ -583,6 +583,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 
 -------------------------------------
@@ -596,6 +598,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 73c8d38b15fa4d7b0752bdae928eac1dd754974e..1d020298fdc5240e4df8bae36d51c35f6dc24d4d 100644 (file)
@@ -309,9 +309,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}