Honor passed-in database OIDs in pgstat_database.c

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
This commit is contained in:
Michael Paquier 2026-04-11 17:03:08 +09:00
parent e630f65d03
commit c7cdcbd3e6
3 changed files with 29 additions and 3 deletions

View 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;

View 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';

View 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}