(unnamed)

text/plain

Filename: (unnamed)
Type: text/plain
Part: 0
Message: Re: shared-memory based stats collector
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index c09fa026b9..3f546afe6a 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -1175,13 +1175,6 @@ pgstat_report_stat(bool force)
 	if (area == NULL)
 		return 0;
 
-	/*
-	 * We need a database entry if the following stats exists.
-	 */
-	if (pgStatXactCommit > 0 || pgStatXactRollback > 0 ||
-		pgStatBlockReadTime > 0 || pgStatBlockWriteTime > 0)
-		get_local_dbstat_entry(MyDatabaseId);
-
 	/* Don't expend a clock check if nothing to do */
 	if (pgStatLocalHash == NULL && have_slrustats && !walstats_pending())
 		return 0;
@@ -1221,8 +1214,6 @@ pgstat_report_stat(bool force)
 	{
 		int			remains = 0;
 		pgstat_localhash_iterator i;
-		List	   *dbentlist = NIL;
-		ListCell   *lc;
 		PgStatLocalHashEntry *lent;
 
 		/* Step 1: flush out other than database stats */
@@ -1234,13 +1225,11 @@ pgstat_report_stat(bool force)
 			switch (lent->key.type)
 			{
 				case PGSTAT_TYPE_DB:
-
 					/*
 					 * flush_tabstat applies some of stats numbers of flushed
-					 * entries into local database stats. Just remember the
-					 * database entries for now then flush-out them later.
+					 * entries into local and shared database stats. Treat them
+					 * separately later.
 					 */
-					dbentlist = lappend(dbentlist, lent);
 					break;
 				case PGSTAT_TYPE_TABLE:
 					if (flush_tabstat(lent, nowait))
@@ -1268,9 +1257,11 @@ pgstat_report_stat(bool force)
 		}
 
 		/* Step 2: flush out database stats */
-		foreach(lc, dbentlist)
+		pgstat_localhash_start_iterate(pgStatLocalHash, &i);
+		while ((lent = pgstat_localhash_iterate(pgStatLocalHash, &i)) != NULL)
 		{
-			PgStatLocalHashEntry *lent = (PgStatLocalHashEntry *) lfirst(lc);
+			/* no other types of entry must be found here */
+			Assert(lent->key.type == PGSTAT_TYPE_DB);
 
 			if (flush_dbstat(lent, nowait))
 			{
@@ -1281,8 +1272,6 @@ pgstat_report_stat(bool force)
 				pgstat_localhash_delete(pgStatLocalHash, lent->key);
 			}
 		}
-		list_free(dbentlist);
-		dbentlist = NULL;
 
 		if (remains <= 0)
 		{
@@ -1507,7 +1496,7 @@ flush_dbstat(PgStatLocalHashEntry *ent, bool nowait)
 
 	Assert(ent->key.type == PGSTAT_TYPE_DB);
 
-	localent = (PgStat_StatDBEntry *) &ent->body;
+	localent = (PgStat_StatDBEntry *) ent->body;
 
 	/* find shared database stats entry corresponding to the local entry */
 	sharedent = (PgStat_StatDBEntry *)
@@ -3215,11 +3204,8 @@ pgstat_fetch_stat_dbentry(Oid dbid)
 	/* should be called from backends */
 	Assert(IsUnderPostmaster);
 
-	/* the simple cache doesn't work properly for InvalidOid */
-	Assert(dbid != InvalidOid);
-
 	/* Return cached result if it is valid. */
-	if (cached_dbent_key.databaseid == dbid)
+	if (dbid != 0 && cached_dbent_key.databaseid == dbid)
 		return &cached_dbent;
 
 	shent = (PgStat_StatDBEntry *)