|  | 
|  | 1 | +Pg_buffercache - Real time queries on the shared buffer cache. | 
|  | 2 | +-------------- | 
|  | 3 | + | 
|  | 4 | +  This module consists of a C function 'pg_buffercache_pages()' that returns  | 
|  | 5 | +  a set of records, plus a view 'pg_buffercache' to wrapper the function. | 
|  | 6 | + | 
|  | 7 | +  The intent is to do for the buffercache what pg_locks does for locks, i.e - | 
|  | 8 | +  ability to examine what is happening at any given time without having to  | 
|  | 9 | +  restart or rebuild the server with debugging code added. | 
|  | 10 | + | 
|  | 11 | +  By default public access is REVOKED from both of these, just in case there | 
|  | 12 | +  are security issues lurking. | 
|  | 13 | + | 
|  | 14 | + | 
|  | 15 | +Installation | 
|  | 16 | +------------ | 
|  | 17 | + | 
|  | 18 | +  Build and install the main Postgresql source, then this contrib module: | 
|  | 19 | + | 
|  | 20 | +  $ cd contrib/pg_buffercache | 
|  | 21 | +  $ gmake | 
|  | 22 | +  $ gmake install | 
|  | 23 | + | 
|  | 24 | + | 
|  | 25 | +  To register the functions: | 
|  | 26 | + | 
|  | 27 | +  $ psql -d <database> -f pg_buffercache.sql | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +Notes | 
|  | 31 | +----- | 
|  | 32 | + | 
|  | 33 | +  The definition of the columns exposed in the view is: | 
|  | 34 | + | 
|  | 35 | +       Column     |  references          | Description | 
|  | 36 | +  ----------------+----------------------+------------------------------------ | 
|  | 37 | +   bufferid       |                      | Id, 1->shared_buffers. | 
|  | 38 | +   relfilenode    | pg_class.relfilenode | Refilenode of the relation. | 
|  | 39 | +   reltablespace  | pg_tablespace.oid    | Tablespace oid of the relation. | 
|  | 40 | +   reldatabase    | pg_database.oid      | Database for the relation. | 
|  | 41 | +   relblocknumber |                      | Offset of the page in the relation. | 
|  | 42 | +   isdirty        |                      | Is the page dirty? | 
|  | 43 | + | 
|  | 44 | + | 
|  | 45 | +  There is one row for each buffer in the shared cache. Unused buffers are | 
|  | 46 | +  shown with all fields null except bufferid. | 
|  | 47 | + | 
|  | 48 | +  Because the cache is shared by all the databases, there are pages from | 
|  | 49 | +  relations not belonging to the current database. | 
|  | 50 | + | 
|  | 51 | +  When the pg_buffercache view is accessed, internal buffer manager locks are  | 
|  | 52 | +  taken, and a copy of the buffer cache data is made for the view to display.  | 
|  | 53 | +  This ensures that the view produces a consistent set of results, while not  | 
|  | 54 | +  blocking normal buffer activity longer than necessary.  Nonetheless there  | 
|  | 55 | +  could be some impact on database performance if this view is read often. | 
|  | 56 | + | 
|  | 57 | + | 
|  | 58 | +Sample output | 
|  | 59 | +------------- | 
|  | 60 | + | 
|  | 61 | +  regression=# \d pg_buffercache; | 
|  | 62 | +       View "public.pg_buffercache" | 
|  | 63 | +       Column     |  Type   | Modifiers | 
|  | 64 | +  ----------------+---------+----------- | 
|  | 65 | +   bufferid       | integer | | 
|  | 66 | +   relfilenode    | oid     | | 
|  | 67 | +   reltablespace  | oid     | | 
|  | 68 | +   reldatabase    | oid     | | 
|  | 69 | +   relblocknumber | numeric | | 
|  | 70 | +   isdirty        | boolean | | 
|  | 71 | +  View definition: | 
|  | 72 | +   SELECT p.bufferid, p.relfilenode, p.reltablespace, p.reldatabase,  | 
|  | 73 | +          p.relblocknumber, p.isdirty | 
|  | 74 | +     FROM pg_buffercache_pages() p(bufferid integer, relfilenode oid,  | 
|  | 75 | +     reltablespace oid, reldatabase oid, relblocknumber numeric(10,0),  | 
|  | 76 | +     isdirty boolean); | 
|  | 77 | + | 
|  | 78 | +  regression=# SELECT c.relname, count(*) AS buffers | 
|  | 79 | +               FROM pg_class c, pg_buffercache b | 
|  | 80 | +               WHERE b.relfilenode = c.relfilenode | 
|  | 81 | +               GROUP BY c.relname | 
|  | 82 | +               ORDER BY 2 DESC LIMIT 10; | 
|  | 83 | +               relname             | buffers | 
|  | 84 | +  ---------------------------------+--------- | 
|  | 85 | +   tenk2                           |     345 | 
|  | 86 | +   tenk1                           |     141 | 
|  | 87 | +   pg_proc                         |      46 | 
|  | 88 | +   pg_class                        |      45 | 
|  | 89 | +   pg_attribute                    |      43 | 
|  | 90 | +   pg_class_relname_nsp_index      |      30 | 
|  | 91 | +   pg_proc_proname_args_nsp_index  |      28 | 
|  | 92 | +   pg_attribute_relid_attnam_index |      26 | 
|  | 93 | +   pg_depend                       |      22 | 
|  | 94 | +   pg_depend_reference_index       |      20 | 
|  | 95 | +  (10 rows) | 
|  | 96 | + | 
|  | 97 | +  regression=#  | 
|  | 98 | + | 
|  | 99 | + | 
|  | 100 | +Author | 
|  | 101 | +------ | 
|  | 102 | + | 
|  | 103 | +  * Mark Kirkwood <[email protected] > | 
|  | 104 | + | 
|  | 105 | + | 
|  | 106 | +Help | 
|  | 107 | +---- | 
|  | 108 | + | 
|  | 109 | +  * Design suggestions : Neil Conway <[email protected] > | 
|  | 110 | +  * Debugging advice : Tom Lane <[email protected] > | 
|  | 111 | + | 
|  | 112 | +  Thanks guys! | 
0 commit comments