At Mon, 22 Mar 2021 09:55:59 +0900 (JST), Kyotaro Horiguchi 
<horikyota....@gmail.com> wrote in 
> At Thu, 18 Mar 2021 01:47:20 -0700, Andres Freund <and...@anarazel.de> wrote 
> in 
> > Hi,
> > 
> > On 2021-03-18 16:56:02 +0900, Kyotaro Horiguchi wrote:
> > > At Tue, 16 Mar 2021 10:27:55 +0900 (JST), Kyotaro Horiguchi 
> > > <horikyota....@gmail.com> wrote in 
> > > Rebased and fixed two bugs.  Not addressed received comments in this
> > > version.
> > 
> > Since I am heavily editing the code, could you submit "functional"
> > changes (as opposed to fixing rebase issues) as incremental patches?
> 
> Oh.. please wait for.. a moment, maybe.

This is that.

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center
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 *)

Reply via email to