Thread: pgstat: reduce message header

pgstat: reduce message header

From
Neil Conway
Date:
This patch reduces the size of the message header used by statistics
collector messages, per recent discussion. This actually required quite
a few changes -- for example, "databaseid != InvalidOid" was used to
check whether a slot in the backend entry table was initialized, but
that no longer works since the slot might be initialized prior to
receiving the BESTART message which contains the database id. We know
use procpid > 0 to indicate that a slot is non-empty.

Other changes:

- various comment improvements and cleanups

- there's no need to zero-out the entire activity buffer in
pgstat_add_backend(), we can just set activity[0] to '\0'.

- remove the counting of the # of connections to a database; this was
not used anywhere

One change in behavior I wasn't sure about: previously, the code would
create a hash table entry for a database as soon as any message was
received whose header referenced that database. Now, we only create hash
table entries as needed (so for example BESTART won't create a hash
table entry, since it doesn't need to access the per-db hash table). Is
it important that we retain the previous behavior? (It would be easy
enough to do by just calling pgstat_get_db_entry() when processing a
BESTART.)

Barring any objections I'll apply this to HEAD tomorrow.

-Neil
Index: src/backend/postmaster/pgstat.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/postmaster/pgstat.c,v
retrieving revision 1.93
diff -c -r1.93 pgstat.c
*** src/backend/postmaster/pgstat.c    9 May 2005 11:31:33 -0000    1.93
--- src/backend/postmaster/pgstat.c    10 May 2005 03:42:54 -0000
***************
*** 162,167 ****
--- 162,168 ----
  static void pgstat_die(SIGNAL_ARGS);
  static void pgstat_beshutdown_hook(int code, Datum arg);

+ static PgStat_StatDBEntry *pgstat_get_db_entry(int databaseid);
  static int    pgstat_add_backend(PgStat_MsgHdr *msg);
  static void pgstat_sub_backend(int procpid);
  static void pgstat_drop_database(Oid databaseid);
***************
*** 653,658 ****
--- 654,662 ----
          return;

      pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_BESTART);
+     msg.m_databaseid = MyDatabaseId;
+     msg.m_userid = GetSessionUserId();
+     memcpy(&msg.m_clientaddr, &MyProcPort->raddr, sizeof(msg.m_clientaddr));
      pgstat_send(&msg, sizeof(msg));

      /*
***************
*** 748,753 ****
--- 752,758 ----
          pgStatXactRollback = 0;

          pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
+         tsmsg->m_databaseid = MyDatabaseId;
          pgstat_send(tsmsg, len);
      }

***************
*** 825,831 ****
          }

          /*
!          * Add this tables Oid to the message
           */
          msg.m_tableid[msg.m_nentries++] = tabentry->tableid;
          nobjects++;
--- 830,836 ----
          }

          /*
!          * Add this table's Oid to the message
           */
          msg.m_tableid[msg.m_nentries++] = tabentry->tableid;
          nobjects++;
***************
*** 854,859 ****
--- 859,865 ----
              +msg.m_nentries * sizeof(Oid);

          pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
+         msg.m_databaseid = MyDatabaseId;
          pgstat_send(&msg, len);
      }

***************
*** 933,941 ****
      if (pgStatSock < 0)
          return;

-     msg.m_databaseid = databaseid;
-
      pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
      pgstat_send(&msg, sizeof(msg));
  }

--- 939,946 ----
      if (pgStatSock < 0)
          return;

      pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
+     msg.m_databaseid = databaseid;
      pgstat_send(&msg, sizeof(msg));
  }

***************
*** 960,965 ****
--- 965,971 ----
                errmsg("must be superuser to reset statistics counters")));

      pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
+     msg.m_databaseid = MyDatabaseId;
      pgstat_send(&msg, sizeof(msg));
  }

***************
*** 1176,1183 ****
  PgStat_StatDBEntry *
  pgstat_fetch_stat_dbentry(Oid dbid)
  {
-     PgStat_StatDBEntry *dbentry;
-
      /*
       * If not done for this transaction, read the statistics collector
       * stats file into some hash tables.
--- 1182,1187 ----
***************
*** 1185,1199 ****
      backend_read_statsfile();

      /*
!      * Lookup the requested database
       */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                                  (void *) &dbid,
!                                                  HASH_FIND, NULL);
!     if (dbentry == NULL)
!         return NULL;
!
!     return dbentry;
  }


--- 1189,1199 ----
      backend_read_statsfile();

      /*
!      * Lookup the requested database; return NULL if not found
       */
!     return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                               (void *) &dbid,
!                                               HASH_FIND, NULL);
  }


***************
*** 1298,1306 ****
      hdr->m_type = mtype;
      hdr->m_backendid = MyBackendId;
      hdr->m_procpid = MyProcPid;
-     hdr->m_databaseid = MyDatabaseId;
-     hdr->m_userid = GetSessionUserId();
-     memcpy(&hdr->m_clientaddr, &MyProcPort->raddr, sizeof(hdr->m_clientaddr));
  }


--- 1298,1303 ----
***************
*** 1976,1985 ****
  static int
  pgstat_add_backend(PgStat_MsgHdr *msg)
  {
-     PgStat_StatDBEntry *dbentry;
      PgStat_StatBeEntry *beentry;
      PgStat_StatBeDead *deadbe;
-     bool        found;

      /*
       * Check that the backend ID is valid
--- 1973,1980 ----
***************
*** 1995,2013 ****
       * Get the slot for this backendid.
       */
      beentry = &pgStatBeTable[msg->m_backendid - 1];
!     if (beentry->databaseid != InvalidOid)
!     {
!         /*
!          * If the slot contains the PID of this backend, everything is
!          * fine and we got nothing to do.
!          */
!         if (beentry->procpid == msg->m_procpid)
!             return 0;
!     }

      /*
       * Lookup if this backend is known to be dead. This can be caused due
!      * to messages arriving in the wrong order - i.e. Postmaster's BETERM
       * message might have arrived before we received all the backends
       * stats messages, or even a new backend with the same backendid was
       * faster in sending his BESTART.
--- 1990,2008 ----
       * Get the slot for this backendid.
       */
      beentry = &pgStatBeTable[msg->m_backendid - 1];
!
!     /*
!      * If the slot contains the PID of this backend, everything is
!      * fine and we have nothing to do. Note that all the slots are
!      * zero'd out when the collector is started. We assume that a slot
!      * is "empty" iff procpid == 0.
!      */
!     if (beentry->procpid > 0 && beentry->procpid == msg->m_procpid)
!         return 0;

      /*
       * Lookup if this backend is known to be dead. This can be caused due
!      * to messages arriving in the wrong order - e.g. postmaster's BETERM
       * message might have arrived before we received all the backends
       * stats messages, or even a new backend with the same backendid was
       * faster in sending his BESTART.
***************
*** 2024,2088 ****
       * Backend isn't known to be dead. If it's slot is currently used, we
       * have to kick out the old backend.
       */
!     if (beentry->databaseid != InvalidOid)
          pgstat_sub_backend(beentry->procpid);

      /*
       * Put this new backend into the slot.
       */
-     beentry->databaseid = msg->m_databaseid;
      beentry->procpid = msg->m_procpid;
-     beentry->userid = msg->m_userid;
      beentry->start_sec =
          GetCurrentAbsoluteTimeUsec(&beentry->start_usec);
      beentry->activity_start_sec = 0;
      beentry->activity_start_usec = 0;
!     memcpy(&beentry->clientaddr, &msg->m_clientaddr, sizeof(beentry->clientaddr));
!     MemSet(beentry->activity, 0, PGSTAT_ACTIVITY_SIZE);

      /*
!      * Lookup or create the database entry for this backend's 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_connects = 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 = oid_hash;
!         dbentry->tables = hash_create("Per-database table",
                                        PGSTAT_TAB_HASH_SIZE,
                                        &hash_ctl,
                                        HASH_ELEM | HASH_FUNCTION);
      }

!     /* Count the number of connects to the database */
!     dbentry->n_connects++;
!
!     return 0;
  }

-
  /* ----------
   * pgstat_sub_backend() -
   *
--- 2019,2096 ----
       * Backend isn't known to be dead. If it's slot is currently used, we
       * have to kick out the old backend.
       */
!     if (beentry->procpid > 0)
          pgstat_sub_backend(beentry->procpid);

+     /* Must be able to distinguish between empty and non-empty slots */
+     Assert(msg->m_procpid > 0);
+
      /*
       * Put this new backend into the slot.
       */
      beentry->procpid = msg->m_procpid;
      beentry->start_sec =
          GetCurrentAbsoluteTimeUsec(&beentry->start_usec);
      beentry->activity_start_sec = 0;
      beentry->activity_start_usec = 0;
!     beentry->activity[0] = '\0';

      /*
!      * There is some data in the slot that can't be set until we see
!      * the BESTART message. To indicate this, set database and user to
!      * sentinel values. There is no easy way to set clientaddr to a
!      * sentinel, so make sure to first check userid or databaseid.
       */
!     beentry->userid = InvalidOid;
!     beentry->databaseid = InvalidOid;
!
!     return 0;
! }
!
! /*
!  * Lookup the hash table entry for the specified database. If no hash
!  * table entry exists, initialize it.
!  */
! static PgStat_StatDBEntry *
! pgstat_get_db_entry(int databaseid)
! {
!     PgStat_StatDBEntry *result;
!     bool found;
!
!     /* Lookup or create the hash table entry for this database */
!     result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                                 &databaseid,
!                                                 HASH_ENTER, &found);
!     if (result == 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;

!         result->tables = NULL;
!         result->n_xact_commit = 0;
!         result->n_xact_rollback = 0;
!         result->n_blocks_fetched = 0;
!         result->n_blocks_hit = 0;
!         result->destroy = 0;

          memset(&hash_ctl, 0, sizeof(hash_ctl));
          hash_ctl.keysize = sizeof(Oid);
          hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
          hash_ctl.hash = oid_hash;
!         result->tables = hash_create("Per-database table",
                                        PGSTAT_TAB_HASH_SIZE,
                                        &hash_ctl,
                                        HASH_ELEM | HASH_FUNCTION);
      }

!     return result;
  }

  /* ----------
   * pgstat_sub_backend() -
   *
***************
*** 2102,2109 ****
       */
      for (i = 0; i < MaxBackends; i++)
      {
!         if (pgStatBeTable[i].databaseid != InvalidOid &&
!             pgStatBeTable[i].procpid == procpid)
          {
              /*
               * That's him. Add an entry to the known to be dead backends.
--- 2110,2116 ----
       */
      for (i = 0; i < MaxBackends; i++)
      {
!         if (pgStatBeTable[i].procpid == procpid)
          {
              /*
               * That's him. Add an entry to the known to be dead backends.
***************
*** 2133,2139 ****
              /*
               * Declare the backend slot empty.
               */
!             pgStatBeTable[i].databaseid = InvalidOid;
              return;
          }
      }
--- 2140,2146 ----
              /*
               * Declare the backend slot empty.
               */
!             pgStatBeTable[i].procpid = 0;
              return;
          }
      }
***************
*** 2263,2269 ****

      for (i = 0; i < MaxBackends; i++)
      {
!         if (pgStatBeTable[i].databaseid != InvalidOid)
          {
              fputc('B', fpout);
              fwrite(&pgStatBeTable[i], sizeof(PgStat_StatBeEntry), 1, fpout);
--- 2270,2276 ----

      for (i = 0; i < MaxBackends; i++)
      {
!         if (pgStatBeTable[i].procpid > 0)
          {
              fputc('B', fpout);
              fwrite(&pgStatBeTable[i], sizeof(PgStat_StatBeEntry), 1, fpout);
***************
*** 2624,2630 ****
  static void
  pgstat_recv_bestart(PgStat_MsgBestart *msg, int len)
  {
!     pgstat_add_backend(&msg->m_hdr);
  }


--- 2631,2650 ----
  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
!      * want to update the backend entry's state since this BESTART
!      * message refers to an old, dead backend
!      */
!     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;
  }


***************
*** 2690,2703 ****
      if (pgstat_add_backend(&msg->m_hdr) < 0)
          return;

!     /*
!      * Lookup the database in the hashtable.
!      */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                      (void *) &(msg->m_hdr.m_databaseid),
!                                                  HASH_FIND, NULL);
!     if (!dbentry)
!         return;

      /*
       * If the database is marked for destroy, this is a delayed UDP packet
--- 2710,2716 ----
      if (pgstat_add_backend(&msg->m_hdr) < 0)
          return;

!     dbentry = pgstat_get_db_entry(msg->m_databaseid);

      /*
       * If the database is marked for destroy, this is a delayed UDP packet
***************
*** 2782,2795 ****
      if (pgstat_add_backend(&msg->m_hdr) < 0)
          return;

!     /*
!      * Lookup the database in the hashtable.
!      */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                      (void *) &(msg->m_hdr.m_databaseid),
!                                                  HASH_FIND, NULL);
!     if (!dbentry)
!         return;

      /*
       * If the database is marked for destroy, this is a delayed UDP packet
--- 2795,2801 ----
      if (pgstat_add_backend(&msg->m_hdr) < 0)
          return;

!     dbentry = pgstat_get_db_entry(msg->m_databaseid);

      /*
       * If the database is marked for destroy, this is a delayed UDP packet
***************
*** 2832,2842 ****
      /*
       * Lookup the database in the hashtable.
       */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                            (void *) &(msg->m_databaseid),
!                                                  HASH_FIND, NULL);
!     if (!dbentry)
!         return;

      /*
       * Mark the database for destruction.
--- 2838,2844 ----
      /*
       * Lookup the database in the hashtable.
       */
!     dbentry = pgstat_get_db_entry(msg->m_databaseid);

      /*
       * Mark the database for destruction.
***************
*** 2846,2854 ****


  /* ----------
!  * pgstat_recv_dropdb() -
   *
!  *    Arrange for dead database removal
   * ----------
   */
  static void
--- 2848,2856 ----


  /* ----------
!  * pgstat_recv_resetcounter() -
   *
!  *    Reset the statistics for the specified database.
   * ----------
   */
  static void
***************
*** 2866,2880 ****
      /*
       * Lookup the database in the hashtable.
       */
!     dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
!                                      (void *) &(msg->m_hdr.m_databaseid),
!                                                  HASH_FIND, NULL);
!     if (!dbentry)
!         return;

      /*
!      * We simply throw away all the databases table entries by recreating
!      * a new hash table for them.
       */
      if (dbentry->tables != NULL)
          hash_destroy(dbentry->tables);
--- 2868,2878 ----
      /*
       * Lookup the database in the hashtable.
       */
!     dbentry = pgstat_get_db_entry(msg->m_databaseid);

      /*
!      * We simply throw away all the database's table entries by
!      * recreating a new hash table for them.
       */
      if (dbentry->tables != NULL)
          hash_destroy(dbentry->tables);
***************
*** 2884,2890 ****
      dbentry->n_xact_rollback = 0;
      dbentry->n_blocks_fetched = 0;
      dbentry->n_blocks_hit = 0;
-     dbentry->n_connects = 0;
      dbentry->destroy = 0;

      memset(&hash_ctl, 0, sizeof(hash_ctl));
--- 2882,2887 ----
Index: src/backend/utils/adt/pgstatfuncs.c
===================================================================
RCS file: /var/lib/cvs/pgsql/src/backend/utils/adt/pgstatfuncs.c,v
retrieving revision 1.21
diff -c -r1.21 pgstatfuncs.c
*** src/backend/utils/adt/pgstatfuncs.c    9 May 2005 11:31:33 -0000    1.21
--- src/backend/utils/adt/pgstatfuncs.c    10 May 2005 03:01:31 -0000
***************
*** 283,288 ****
--- 283,292 ----
      if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
          PG_RETURN_NULL();

+     /* Not initialized yet? */
+     if (!OidIsValid(beentry->databaseid))
+         PG_RETURN_NULL();
+
      PG_RETURN_OID(beentry->databaseid);
  }

***************
*** 298,303 ****
--- 302,311 ----
      if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
          PG_RETURN_NULL();

+     /* Not initialized yet? */
+     if (!OidIsValid(beentry->userid))
+         PG_RETURN_NULL();
+
      PG_RETURN_INT32(beentry->userid);
  }

***************
*** 405,410 ****
--- 413,422 ----
      if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
          PG_RETURN_NULL();

+     /* Not initialized yet? */
+     if (!OidIsValid(beentry->userid))
+         PG_RETURN_NULL();
+
      if (!superuser() && beentry->userid != GetUserId())
          PG_RETURN_NULL();

***************
*** 420,426 ****
      }

      remote_host[0] = '\0';
-
      ret = getnameinfo_all(&beentry->clientaddr.addr, beentry->clientaddr.salen,
                            remote_host, sizeof(remote_host),
                            NULL, 0,
--- 432,437 ----
***************
*** 445,450 ****
--- 456,465 ----
      if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
          PG_RETURN_NULL();

+     /* Not initialized yet? */
+     if (!OidIsValid(beentry->userid))
+         PG_RETURN_NULL();
+
      if (!superuser() && beentry->userid != GetUserId())
          PG_RETURN_NULL();

***************
*** 462,468 ****
      }

      remote_port[0] = '\0';
-
      ret = getnameinfo_all(&beentry->clientaddr.addr,
                            beentry->clientaddr.salen,
                            NULL, 0,
--- 477,482 ----
Index: src/include/pgstat.h
===================================================================
RCS file: /var/lib/cvs/pgsql/src/include/pgstat.h,v
retrieving revision 1.28
diff -c -r1.28 pgstat.h
*** src/include/pgstat.h    9 May 2005 11:31:33 -0000    1.28
--- src/include/pgstat.h    10 May 2005 03:38:05 -0000
***************
*** 52,60 ****
      int            m_size;
      int            m_backendid;
      int            m_procpid;
-     Oid            m_databaseid;
-     AclId        m_userid;
-     SockAddr    m_clientaddr;
  } PgStat_MsgHdr;

  /* ----------
--- 52,57 ----
***************
*** 102,108 ****
   */
  typedef struct PgStat_MsgBestart
  {
!     PgStat_MsgHdr m_hdr;
  } PgStat_MsgBestart;

  /* ----------
--- 99,108 ----
   */
  typedef struct PgStat_MsgBestart
  {
!     PgStat_MsgHdr    m_hdr;
!     Oid                m_databaseid;
!     AclId            m_userid;
!     SockAddr        m_clientaddr;
  } PgStat_MsgBestart;

  /* ----------
***************
*** 138,143 ****
--- 138,144 ----
  typedef struct PgStat_MsgTabstat
  {
      PgStat_MsgHdr m_hdr;
+     int            m_databaseid;
      int            m_nentries;
      int            m_xact_commit;
      int            m_xact_rollback;
***************
*** 155,160 ****
--- 156,162 ----
  typedef struct PgStat_MsgTabpurge
  {
      PgStat_MsgHdr m_hdr;
+     Oid            m_databaseid;
      int            m_nentries;
      Oid            m_tableid[PGSTAT_NUM_TABPURGE];
  } PgStat_MsgTabpurge;
***************
*** 180,185 ****
--- 182,188 ----
  typedef struct PgStat_MsgResetcounter
  {
      PgStat_MsgHdr m_hdr;
+     Oid            m_databaseid;
  } PgStat_MsgResetcounter;


***************
*** 214,220 ****
      Oid            databaseid;
      HTAB       *tables;
      int            n_backends;
-     PgStat_Counter n_connects;
      PgStat_Counter n_xact_commit;
      PgStat_Counter n_xact_rollback;
      PgStat_Counter n_blocks_fetched;
--- 217,222 ----
***************
*** 229,243 ****
   */
  typedef struct PgStat_StatBeEntry
  {
!     Oid            databaseid;
!     Oid            userid;
      int            procpid;
      AbsoluteTime start_sec;
      int         start_usec;
      AbsoluteTime activity_start_sec;
      int            activity_start_usec;
-     SockAddr    clientaddr;
      char        activity[PGSTAT_ACTIVITY_SIZE];
  } PgStat_StatBeEntry;


--- 231,254 ----
   */
  typedef struct PgStat_StatBeEntry
  {
!     /* * An entry is non-empty iff procpid > 0 */
      int            procpid;
      AbsoluteTime start_sec;
      int         start_usec;
      AbsoluteTime activity_start_sec;
      int            activity_start_usec;
      char        activity[PGSTAT_ACTIVITY_SIZE];
+
+     /*
+      * The following fields are initialized by the BESTART message. If
+      * we have received messages from a backend before we have
+      * received its BESTART, these fields will be uninitialized;
+      * userid and databaseid will be InvalidOid, and clientaddr will
+      * be undefined.
+      */
+     Oid            userid;
+     Oid            databaseid;
+     SockAddr    clientaddr;
  } PgStat_StatBeEntry;



Re: pgstat: reduce message header

From
Neil Conway
Date:
Neil Conway wrote:
> This patch reduces the size of the message header used by statistics
> collector messages, per recent discussion.

Applied.

-Neil