bgwriter stats - Mailing list pgsql-patches

From Magnus Hagander
Subject bgwriter stats
Date
Msg-id 45FEE82C.8060205@hagander.net
Whole thread Raw
Responses Re: bgwriter stats  (Neil Conway <neilc@samurai.com>)
Re: bgwriter stats  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
I want to be able to pull some stats out of the bgwriter to be able to
track things. One thing is the total number of buffers written out.
Other things are the "number of checkpoints" and such.

Anyway. Attached patch adds this to the bgwriter shared memory. Is it
safe to do this, and then just have a regular function running in a
normal backend pulling out the value and returning it to the user,
without locking? Given that only the bgwriter can write to it?

Patch of course entirely incomplete, just illustrating the main approach
I've been thinking of taking. Just want to get this question asked and
answered before I go ahead and code more...

//Magnus

Index: src/backend/postmaster/bgwriter.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/postmaster/bgwriter.c,v
retrieving revision 1.36
diff -c -r1.36 bgwriter.c
*** src/backend/postmaster/bgwriter.c    17 Jan 2007 16:25:01 -0000    1.36
--- src/backend/postmaster/bgwriter.c    19 Mar 2007 19:38:27 -0000
***************
*** 119,124 ****
--- 119,125 ----

      int            num_requests;    /* current # of requests */
      int            max_requests;    /* allocated array size */
+     int64        buffers_written; /* number of buffers written */
      BgWriterRequest requests[1];    /* VARIABLE LENGTH ARRAY */
  } BgWriterShmemStruct;

***************
*** 427,433 ****
              last_checkpoint_time = now;
          }
          else
!             BgBufferSync();

          /*
           * Check for archive_timeout, if so, switch xlog files.  First we do a
--- 428,434 ----
              last_checkpoint_time = now;
          }
          else
!             BgWriterShmem->buffers_written += BgBufferSync();

          /*
           * Check for archive_timeout, if so, switch xlog files.  First we do a
Index: src/backend/storage/buffer/bufmgr.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v
retrieving revision 1.215
diff -c -r1.215 bufmgr.c
*** src/backend/storage/buffer/bufmgr.c    1 Feb 2007 19:10:27 -0000    1.215
--- src/backend/storage/buffer/bufmgr.c    19 Mar 2007 19:38:27 -0000
***************
*** 986,998 ****
   *
   * This is called periodically by the background writer process.
   */
! void
  BgBufferSync(void)
  {
      static int    buf_id1 = 0;
      int            buf_id2;
      int            num_to_scan;
      int            num_written;

      /* Make sure we can handle the pin inside SyncOneBuffer */
      ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
--- 986,999 ----
   *
   * This is called periodically by the background writer process.
   */
! int
  BgBufferSync(void)
  {
      static int    buf_id1 = 0;
      int            buf_id2;
      int            num_to_scan;
      int            num_written;
+     int            total_written = 0;

      /* Make sure we can handle the pin inside SyncOneBuffer */
      ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
***************
*** 1030,1035 ****
--- 1031,1037 ----
                      break;
              }
          }
+         total_written += num_written;
      }

      /*
***************
*** 1053,1059 ****
--- 1055,1064 ----
              if (++buf_id2 >= NBuffers)
                  buf_id2 = 0;
          }
+         total_written += num_written;
      }
+
+     return total_written;
  }

  /*
Index: src/include/storage/bufmgr.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/storage/bufmgr.h,v
retrieving revision 1.102
diff -c -r1.102 bufmgr.h
*** src/include/storage/bufmgr.h    5 Jan 2007 22:19:57 -0000    1.102
--- src/include/storage/bufmgr.h    19 Mar 2007 19:38:28 -0000
***************
*** 151,157 ****

  extern void BufmgrCommit(void);
  extern void BufferSync(void);
! extern void BgBufferSync(void);

  extern void AtProcExit_LocalBuffers(void);

--- 151,157 ----

  extern void BufmgrCommit(void);
  extern void BufferSync(void);
! extern int BgBufferSync(void);

  extern void AtProcExit_LocalBuffers(void);


pgsql-patches by date:

Previous
From: Magnus Hagander
Date:
Subject: Win32 shmem
Next
From: Neil Conway
Date:
Subject: Re: bgwriter stats