summaryrefslogtreecommitdiff
path: root/doc/src/sgml/pgbuffercache.sgml
blob: dc402ed490efb9301405bb8f6be564656a2da071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!-- $PostgreSQL: pgsql/doc/src/sgml/pgbuffercache.sgml,v 2.7 2010/07/29 19:34:40 petere Exp $ -->

<sect1 id="pgbuffercache">
 <title>pg_buffercache</title>

 <indexterm zone="pgbuffercache">
  <primary>pg_buffercache</primary>
 </indexterm>

 <para>
  The <filename>pg_buffercache</filename> module provides a means for
  examining what's happening in the shared buffer cache in real time.
 </para>

 <para>
  The module provides a C function <function>pg_buffercache_pages</function>
  that returns a set of records, plus a view
  <structname>pg_buffercache</structname> that wraps the function for
  convenient use.
 </para>

 <para>
  By default public access is revoked from both of these, just in case there
  are security issues lurking.
 </para>

 <sect2>
  <title>The <structname>pg_buffercache</structname> view</title>

  <para>
   The definitions of the columns exposed by the view are shown in <xref linkend="pgbuffercache-columns">.
  </para>

  <table id="pgbuffercache-columns">
   <title><structname>pg_buffercache</> Columns</title>

   <tgroup cols="4">
    <thead>
     <row>
      <entry>Name</entry>
      <entry>Type</entry>
      <entry>References</entry>
      <entry>Description</entry>
     </row>
    </thead>
    <tbody>

     <row>
      <entry><structfield>bufferid</structfield></entry>
      <entry><type>integer</type></entry>
      <entry></entry>
      <entry>ID, in the range 1..<varname>shared_buffers</></entry>
     </row>

     <row>
      <entry><structfield>relfilenode</structfield></entry>
      <entry><type>oid</type></entry>
      <entry><literal>pg_class.relfilenode</literal></entry>
      <entry>Filenode number of the relation</entry>
     </row>

     <row>
      <entry><structfield>reltablespace</structfield></entry>
      <entry><type>oid</type></entry>
      <entry><literal>pg_tablespace.oid</literal></entry>
      <entry>Tablespace OID of the relation</entry>
     </row>

     <row>
      <entry><structfield>reldatabase</structfield></entry>
      <entry><type>oid</type></entry>
      <entry><literal>pg_database.oid</literal></entry>
      <entry>Database OID of the relation</entry>
     </row>

     <row>
      <entry><structfield>relblocknumber</structfield></entry>
      <entry><type>bigint</type></entry>
      <entry></entry>
      <entry>Page number within the relation</entry>
     </row>

     <row>
      <entry><structfield>relforknumber</structfield></entry>
      <entry><type>smallint</type></entry>
      <entry></entry>
      <entry>Fork number within the relation</entry>
     </row>

     <row>
      <entry><structfield>isdirty</structfield></entry>
      <entry><type>boolean</type></entry>
      <entry></entry>
      <entry>Is the page dirty?</entry>
     </row>

     <row>
      <entry><structfield>usagecount</structfield></entry>
      <entry><type>smallint</type></entry>
      <entry></entry>
      <entry>Page LRU count</entry>
     </row>

    </tbody>
   </tgroup>
  </table>

  <para>
   There is one row for each buffer in the shared cache. Unused buffers are
   shown with all fields null except <structfield>bufferid</>.  Shared system
   catalogs are shown as belonging to database zero.
  </para>

  <para>
   Because the cache is shared by all the databases, there will normally be
   pages from relations not belonging to the current database.  This means
   that there may not be matching join rows in <structname>pg_class</> for
   some rows, or that there could even be incorrect joins.  If you are
   trying to join against <structname>pg_class</>, it's a good idea to
   restrict the join to rows having <structfield>reldatabase</> equal to
   the current database's OID or zero.
  </para>

  <para>
   When the <structname>pg_buffercache</> view is accessed, internal buffer
   manager locks are taken for long enough to copy all the buffer state
   data that the view will display.
   This ensures that the view produces a consistent set of results, while not
   blocking normal buffer activity longer than necessary.  Nonetheless there
   could be some impact on database performance if this view is read often.
  </para>
 </sect2>

 <sect2>
  <title>Sample output</title>

<screen>
regression=# SELECT c.relname, count(*) AS buffers
             FROM pg_buffercache b INNER JOIN pg_class c
             ON b.relfilenode = pg_relation_filenode(c.oid) AND
                b.reldatabase IN (0, (SELECT oid FROM pg_database
                                      WHERE datname = current_database()))
             GROUP BY c.relname
             ORDER BY 2 DESC
             LIMIT 10;

             relname             | buffers
---------------------------------+---------
 tenk2                           |     345
 tenk1                           |     141
 pg_proc                         |      46
 pg_class                        |      45
 pg_attribute                    |      43
 pg_class_relname_nsp_index      |      30
 pg_proc_proname_args_nsp_index  |      28
 pg_attribute_relid_attnam_index |      26
 pg_depend                       |      22
 pg_depend_reference_index       |      20
(10 rows)
</screen>
 </sect2>

 <sect2>
  <title>Authors</title>

  <para>
   Mark Kirkwood <email>markir@paradise.net.nz</email>
  </para>

  <para>
   Design suggestions: Neil Conway <email>neilc@samurai.com</email>
  </para>

  <para>
   Debugging advice: Tom Lane <email>tgl@sss.pgh.pa.us</email>
  </para>
 </sect2>

</sect1>