3 <sect1 id=
"pgbuffercache">
4 <title>pg_buffercache
</title>
6 <indexterm zone=
"pgbuffercache">
7 <primary>pg_buffercache
</primary>
11 The
<filename>pg_buffercache
</filename> module provides a means for
12 examining what's happening in the shared buffer cache in real time.
16 The module provides a C function
<function>pg_buffercache_pages
</function>
17 that returns a set of records, plus a view
18 <structname>pg_buffercache
</structname> that wraps the function for
23 By default public access is revoked from both of these, just in case there
24 are security issues lurking.
28 <title>The
<structname>pg_buffercache
</structname> view
</title>
31 The definitions of the columns exposed by the view are shown in
<xref linkend=
"pgbuffercache-columns">.
34 <table id=
"pgbuffercache-columns">
35 <title><structname>pg_buffercache<
/> Columns
</title>
42 <entry>References
</entry>
43 <entry>Description
</entry>
49 <entry><structfield>bufferid
</structfield></entry>
50 <entry><type>integer
</type></entry>
52 <entry>ID, in the range
1..
<varname>shared_buffers<
/></entry>
56 <entry><structfield>relfilenode
</structfield></entry>
57 <entry><type>oid
</type></entry>
58 <entry><literal>pg_class.relfilenode
</literal></entry>
59 <entry>Relfilenode of the relation
</entry>
63 <entry><structfield>reltablespace
</structfield></entry>
64 <entry><type>oid
</type></entry>
65 <entry><literal>pg_tablespace.oid
</literal></entry>
66 <entry>Tablespace OID of the relation
</entry>
70 <entry><structfield>reldatabase
</structfield></entry>
71 <entry><type>oid
</type></entry>
72 <entry><literal>pg_database.oid
</literal></entry>
73 <entry>Database OID of the relation
</entry>
77 <entry><structfield>relblocknumber
</structfield></entry>
78 <entry><type>bigint
</type></entry>
80 <entry>Page number within the relation
</entry>
84 <entry><structfield>relforknumber
</structfield></entry>
85 <entry><type>smallint
</type></entry>
87 <entry>Fork number within the relation
</entry>
91 <entry><structfield>isdirty
</structfield></entry>
92 <entry><type>boolean
</type></entry>
94 <entry>Is the page dirty?
</entry>
98 <entry><structfield>usagecount
</structfield></entry>
99 <entry><type>smallint
</type></entry>
101 <entry>Page LRU count
</entry>
109 There is one row for each buffer in the shared cache. Unused buffers are
110 shown with all fields null except
<structfield>bufferid<
/>. Shared system
111 catalogs are shown as belonging to database zero.
115 Because the cache is shared by all the databases, there will normally be
116 pages from relations not belonging to the current database. This means
117 that there may not be matching join rows in
<structname>pg_class<
/> for
118 some rows, or that there could even be incorrect joins. If you are
119 trying to join against
<structname>pg_class<
/>, it's a good idea to
120 restrict the join to rows having
<structfield>reldatabase<
/> equal to
121 the current database's OID or zero.
125 When the
<structname>pg_buffercache<
/> view is accessed, internal buffer
126 manager locks are taken for long enough to copy all the buffer state
127 data that the view will display.
128 This ensures that the view produces a consistent set of results, while not
129 blocking normal buffer activity longer than necessary. Nonetheless there
130 could be some impact on database performance if this view is read often.
135 <title>Sample output
</title>
138 regression=# SELECT c.relname, count(*) AS buffers
139 FROM pg_buffercache b INNER JOIN pg_class c
140 ON b.relfilenode = c.relfilenode AND
141 b.reldatabase IN (
0, (SELECT oid FROM pg_database
142 WHERE datname = current_database()))
147 ---------------------------------+---------
153 pg_class_relname_nsp_index |
30
154 pg_proc_proname_args_nsp_index |
28
155 pg_attribute_relid_attnam_index |
26
157 pg_depend_reference_index |
20
163 <title>Authors
</title>
166 Mark Kirkwood
<email>markir@paradise.net.nz
</email>
170 Design suggestions: Neil Conway
<email>neilc@samurai.com
</email>
174 Debugging advice: Tom Lane
<email>tgl@sss.pgh.pa.us
</email>