Thread: [PATCH] fix for pg_stat_database (numbackends is always zero)

[PATCH] fix for pg_stat_database (numbackends is always zero)

From
Petr Jelínek
Date:
This patch fixes pg_stat_database which is broken since this commit:
http://archives.postgresql.org/pgsql-committers/2005-05/msg00126.php
Neilc forgot to add new db entry inicialization to pgstat_recv_bestart()
when he removed it from pgstat_add_backend() function, which causes
numbackends in pg_stat_database table to be allways zero.

This is my first patch for postgres so I hope I did it right.

--
Regards
Petr Jelinek (PJMODOS)
www.parba.cz

*** pgstat.c    Sat Jun 18 01:17:26 2005
--- pgstat.c.new    Thu Jun 23 21:38:06 2005
***************
*** 2613,2619 ****
  static void
  pgstat_recv_bestart(PgStat_MsgBestart *msg, int len)
  {
!     PgStat_StatBeEntry *entry;

      /*
       * If the backend is known dead, we ignore the message -- we don't
--- 2613,2621 ----
  static void
  pgstat_recv_bestart(PgStat_MsgBestart *msg, int len)
  {
!     PgStat_StatBeEntry *beentry;
!     PgStat_StatDBEntry *dbentry;
!     bool        found;

      /*
       * If the backend is known dead, we ignore the message -- we don't
***************
*** 2623,2632 ****
      if (pgstat_add_backend(&msg->m_hdr) != 0)
          return;

!     entry = &(pgStatBeTable[msg->m_hdr.m_backendid - 1]);
!     entry->userid = msg->m_userid;
!     memcpy(&entry->clientaddr, &msg->m_clientaddr, sizeof(entry->clientaddr));
!     entry->databaseid = msg->m_databaseid;
  }


--- 2625,2675 ----
      if (pgstat_add_backend(&msg->m_hdr) != 0)
          return;

!     beentry = &(pgStatBeTable[msg->m_hdr.m_backendid - 1]);
!     beentry->userid = msg->m_userid;
!     memcpy(&beentry->clientaddr, &msg->m_clientaddr, sizeof(beentry->clientaddr));
!     beentry->databaseid = msg->m_databaseid;
!
!     /*
!      * Lookup or create the database entry for this backends DB.
!      */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                            (void *) &(msg->m_databaseid),
!                                                  HASH_ENTER, &found);
!     if (dbentry == NULL)
!         ereport(ERROR,
!                 (errcode(ERRCODE_OUT_OF_MEMORY),
!              errmsg("out of memory in statistics collector --- abort")));
!
!     /*
!      * If not found, initialize the new one.
!      */
!     if (!found)
!     {
!         HASHCTL        hash_ctl;
!
!         dbentry->tables = NULL;
!         dbentry->n_xact_commit = 0;
!         dbentry->n_xact_rollback = 0;
!         dbentry->n_blocks_fetched = 0;
!         dbentry->n_blocks_hit = 0;
!         dbentry->n_backends = 0;
!         dbentry->destroy = 0;
!
!         memset(&hash_ctl, 0, sizeof(hash_ctl));
!         hash_ctl.keysize = sizeof(Oid);
!         hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
!         hash_ctl.hash = tag_hash;
!         dbentry->tables = hash_create("Per-database table",
!                                       PGSTAT_TAB_HASH_SIZE,
!                                       &hash_ctl,
!                                       HASH_ELEM | HASH_FUNCTION);
!     }
!
!     /*
!      * Count number of connects to the database
!      */
!     dbentry->n_backends++;
  }



Re: [PATCH] fix for pg_stat_database (numbackends is always

From
Petr Jelinek
Date:
Petr Jelinek wrote:
This patch fixes pg_stat_database which is broken since this commit: http://archives.postgresql.org/pgsql-committers/2005-05/msg00126.php
Neilc forgot to add new db entry inicialization to pgstat_recv_bestart() when he removed it from pgstat_add_backend() function, which causes numbackends in pg_stat_database table to be allways zero.

This is my first patch for postgres so I hope I did it right.

Please ignore this email - it was sent when I wasn't subscribed and we already had discrussion here about this prob.

-- 
Regards
Petr Jelinek (PJMODOS)