]> 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:04 +0000 (17:03 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 11 Apr 2026 08:03:04 +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 2f83b9e333de13368edbbd666a1b1e3456ae47a5..9f1a8633b4bac15782abf862dbd7313e0e0f6232 100644 (file)
@@ -233,7 +233,7 @@ pgstat_report_connect(Oid dboid)
 
        pgLastSessionReportTime = MyStartTimestamp;
 
-       dbentry = pgstat_prep_database_pending(MyDatabaseId);
+       dbentry = pgstat_prep_database_pending(dboid);
        dbentry->sessions++;
 }
 
@@ -248,7 +248,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)
        {
@@ -409,7 +409,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 776f1ad0e53477be1bea320609639d58773aef76..37df27351d8af634d3a46a99c6492104fd2a83f0 100644 (file)
@@ -877,6 +877,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 
 -------------------------------------
@@ -890,6 +892,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 232ab8db8fa8b210628471b5a3a52e96b9ab946c..2df55a9f2e5d26ffe920358360f043ff16e49f57 100644 (file)
@@ -402,9 +402,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}