diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index 41f4374..38656e5 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -36,6 +36,7 @@
 #include "access/transam.h"
 #include "access/twophase_rmgr.h"
 #include "access/xact.h"
+#include "catalog/catalog.h"
 #include "catalog/pg_database.h"
 #include "catalog/pg_proc.h"
 #include "lib/ilist.h"
@@ -263,7 +264,7 @@ static void pgstat_write_statsfiles(bool permanent, bool allDbs);
 static void pgstat_write_db_statsfile(PgStat_StatDBEntry *dbentry, bool permanent);
 static HTAB *pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep);
 static void pgstat_read_db_statsfile(Oid databaseid, HTAB *tabhash, HTAB *funchash, bool permanent);
-static void backend_read_statsfile(void);
+static void backend_read_statsfile(Oid databaseOid);
 static void pgstat_read_current_status(void);
 
 static bool pgstat_write_statsfile_needed(void);
@@ -957,7 +958,7 @@ pgstat_vacuum_stat(void)
 	 * If not done for this transaction, read the statistics collector stats
 	 * file into some hash tables.
 	 */
-	backend_read_statsfile();
+	backend_read_statsfile(MyDatabaseId);
 
 	/*
 	 * Read pg_database and make a list of OIDs of all existing databases
@@ -2274,7 +2275,7 @@ pgstat_fetch_stat_dbentry(Oid dbid)
 	 * If not done for this transaction, read the statistics collector stats
 	 * file into some hash tables.
 	 */
-	backend_read_statsfile();
+	backend_read_statsfile(dbid);
 
 	/*
 	 * Lookup the requested database; return NULL if not found
@@ -2302,31 +2303,19 @@ pgstat_fetch_stat_tabentry(Oid relid)
 	PgStat_StatTabEntry *tabentry;
 
 	/*
-	 * If not done for this transaction, read the statistics collector stats
-	 * file into some hash tables.
+	 * Except for shared relations, look for things in our database.
 	 */
-	backend_read_statsfile();
+	dbid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
 
 	/*
-	 * Lookup our database, then look in its table hash table.
+	 * If not done for this transaction, read the statistics collector stats
+	 * file into some hash tables.
 	 */
-	dbid = MyDatabaseId;
-	dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
-												 (void *) &dbid,
-												 HASH_FIND, NULL);
-	if (dbentry != NULL && dbentry->tables != NULL)
-	{
-		tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
-													   (void *) &relid,
-													   HASH_FIND, NULL);
-		if (tabentry)
-			return tabentry;
-	}
+	backend_read_statsfile(dbid);
 
 	/*
-	 * If we didn't find it, maybe it's a shared table.
+	 * Lookup a database, then look in its table hash table.
 	 */
-	dbid = InvalidOid;
 	dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
 												 (void *) &dbid,
 												 HASH_FIND, NULL);
@@ -2356,8 +2345,7 @@ pgstat_fetch_stat_funcentry(Oid func_id)
 	PgStat_StatDBEntry *dbentry;
 	PgStat_StatFuncEntry *funcentry = NULL;
 
-	/* load the stats file if needed */
-	backend_read_statsfile();
+	backend_read_statsfile(MyDatabaseId);
 
 	/* Lookup our database, then find the requested function.  */
 	dbentry = pgstat_fetch_stat_dbentry(MyDatabaseId);
@@ -2442,7 +2430,7 @@ pgstat_fetch_stat_numbackends(void)
 PgStat_ArchiverStats *
 pgstat_fetch_stat_archiver(void)
 {
-	backend_read_statsfile();
+	backend_read_statsfile(MyDatabaseId);
 
 	return &archiverStats;
 }
@@ -2459,7 +2447,7 @@ pgstat_fetch_stat_archiver(void)
 PgStat_GlobalStats *
 pgstat_fetch_global(void)
 {
-	backend_read_statsfile();
+	backend_read_statsfile(MyDatabaseId);
 
 	return &globalStats;
 }
@@ -4481,6 +4469,9 @@ pgstat_read_db_statsfile(Oid databaseid, HTAB *tabhash, HTAB *funchash,
 					goto done;
 				}
 
+				elog(LOG, "Log entry read for function %u",
+					 funcbuf.functionid);
+
 				/*
 				 * Skip if function belongs to a not requested database.
 				 */
@@ -4665,7 +4656,7 @@ done:
  * is called (typically, at end of transaction).
  */
 static void
-backend_read_statsfile(void)
+backend_read_statsfile(Oid databaseOid)
 {
 	TimestampTz min_ts = 0;
 	TimestampTz ref_ts = 0;
@@ -4689,7 +4680,7 @@ backend_read_statsfile(void)
 
 		CHECK_FOR_INTERRUPTS();
 
-		ok = pgstat_read_db_statsfile_timestamp(MyDatabaseId, false, &file_ts);
+		ok = pgstat_read_db_statsfile_timestamp(databaseOid, false, &file_ts);
 
 		cur_ts = GetCurrentTimestamp();
 		/* Calculate min acceptable timestamp, if we didn't already */
@@ -4748,7 +4739,7 @@ backend_read_statsfile(void)
 				pfree(mytime);
 			}
 
-			pgstat_send_inquiry(cur_ts, min_ts, MyDatabaseId);
+			pgstat_send_inquiry(cur_ts, min_ts, databaseOid);
 			break;
 		}
 
@@ -4758,7 +4749,7 @@ backend_read_statsfile(void)
 
 		/* Not there or too old, so kick the collector and wait a bit */
 		if ((count % PGSTAT_INQ_LOOP_COUNT) == 0)
-			pgstat_send_inquiry(cur_ts, min_ts, MyDatabaseId);
+			pgstat_send_inquiry(cur_ts, min_ts, databaseOid);
 
 		pg_usleep(PGSTAT_RETRY_DELAY * 1000L);
 	}
@@ -4770,12 +4761,12 @@ backend_read_statsfile(void)
 
 	/*
 	 * Autovacuum launcher wants stats about all databases, but a shallow read
-	 * is sufficient.
+	 * is sufficient on the database wanted by caller.
 	 */
 	if (IsAutoVacuumLauncherProcess())
 		pgStatDBHash = pgstat_read_statsfiles(InvalidOid, false, false);
 	else
-		pgStatDBHash = pgstat_read_statsfiles(MyDatabaseId, false, true);
+		pgStatDBHash = pgstat_read_statsfiles(databaseOid, false, true);
 }
 
 
