From f44271176e857dd7b5adb4e70e10510e1638c9d1 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 9 Feb 2007 16:12:19 +0000
Subject: [PATCH] Call pgstat_drop_database during DROP DATABASE, so that any
 stats file entries for the victim database go away sooner rather than later. 
 We already did the equivalent thing at the per-relation level, not sure why
 it's not been done for whole databases.  With this change,
 pgstat_vacuum_tabstat should usually not find anything to do; though we still
 need it as a backstop in case DROPDB or TABPURGE messages get lost under
 load.

---
 src/backend/commands/dbcommands.c | 8 +++++++-
 src/backend/postmaster/pgstat.c   | 5 ++---
 src/include/pgstat.h              | 3 ++-
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index be16791342e..63e15e505b7 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.191 2007/02/01 19:10:26 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.192 2007/02/09 16:12:18 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,6 +37,7 @@
 #include "commands/tablespace.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "pgstat.h"
 #include "postmaster/bgwriter.h"
 #include "storage/freespace.h"
 #include "storage/procarray.h"
@@ -644,6 +645,11 @@ dropdb(const char *dbname, bool missing_ok)
 	 */
 	FreeSpaceMapForgetDatabase(db_id);
 
+	/*
+	 * Tell the stats collector to forget it immediately, too.
+	 */
+	pgstat_drop_database(db_id);
+
 	/*
 	 * Tell bgwriter to forget any pending fsync requests for files in the
 	 * database; else it'll fail at next checkpoint.
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index f64e02020a8..56ee4916d92 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -13,7 +13,7 @@
  *
  *	Copyright (c) 2001-2007, PostgreSQL Global Development Group
  *
- *	$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.145 2007/02/07 23:11:29 tgl Exp $
+ *	$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.146 2007/02/09 16:12:18 tgl Exp $
  * ----------
  */
 #include "postgres.h"
@@ -153,7 +153,6 @@ static void force_statwrite(SIGNAL_ARGS);
 static void pgstat_beshutdown_hook(int code, Datum arg);
 
 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
-static void pgstat_drop_database(Oid databaseid);
 static void pgstat_write_statsfile(void);
 static HTAB *pgstat_read_statsfile(Oid onlydb);
 static void backend_read_statsfile(void);
@@ -820,7 +819,7 @@ pgstat_collect_oids(Oid catalogid)
  *	via future invocations of pgstat_vacuum_tabstat().)
  * ----------
  */
-static void
+void
 pgstat_drop_database(Oid databaseid)
 {
 	PgStat_MsgDropdb msg;
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 896cc5ad68c..e4cc6e9aec7 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -5,7 +5,7 @@
  *
  *	Copyright (c) 2001-2007, PostgreSQL Global Development Group
  *
- *	$PostgreSQL: pgsql/src/include/pgstat.h,v 1.53 2007/02/07 23:11:30 tgl Exp $
+ *	$PostgreSQL: pgsql/src/include/pgstat.h,v 1.54 2007/02/09 16:12:19 tgl Exp $
  * ----------
  */
 #ifndef PGSTAT_H
@@ -378,6 +378,7 @@ extern void pgstat_ping(void);
 
 extern void pgstat_report_tabstat(void);
 extern void pgstat_vacuum_tabstat(void);
+extern void pgstat_drop_database(Oid databaseid);
 extern void pgstat_drop_relation(Oid relid);
 
 extern void pgstat_clear_snapshot(void);
-- 
GitLab