1 <!-- doc/src/sgml/pgbuffercache.sgml -->
3 <sect1 id=
"pgbuffercache" xreflabel=
"pg_buffercache">
4 <title>pg_buffercache
— inspect
<productname>PostgreSQL
</productname>
5 buffer cache state
</title>
7 <indexterm zone=
"pgbuffercache">
8 <primary>pg_buffercache
</primary>
12 The
<filename>pg_buffercache
</filename> module provides a means for
13 examining what's happening in the shared buffer cache in real time.
14 It also offers a low-level way to evict data from it, for testing
19 <primary>pg_buffercache_pages
</primary>
23 <primary>pg_buffercache_summary
</primary>
27 <primary>pg_buffercache_evict
</primary>
31 This module provides the
<function>pg_buffercache_pages()
</function>
32 function (wrapped in the
<structname>pg_buffercache
</structname> view),
33 the
<function>pg_buffercache_summary()
</function> function, the
34 <function>pg_buffercache_usage_counts()
</function> function and
35 the
<function>pg_buffercache_evict()
</function> function.
39 The
<function>pg_buffercache_pages()
</function> function returns a set of
40 records, each row describing the state of one shared buffer entry. The
41 <structname>pg_buffercache
</structname> view wraps the function for
46 The
<function>pg_buffercache_summary()
</function> function returns a single
47 row summarizing the state of the shared buffer cache.
51 The
<function>pg_buffercache_usage_counts()
</function> function returns a set
52 of records, each row describing the number of buffers with a given usage
57 By default, use of the above functions is restricted to superusers and roles
58 with privileges of the
<literal>pg_monitor
</literal> role. Access may be
59 granted to others using
<command>GRANT
</command>.
63 The
<function>pg_buffercache_evict()
</function> function allows a block to
64 be evicted from the buffer pool given a buffer identifier. Use of this
65 function is restricted to superusers only.
68 <sect2 id=
"pgbuffercache-pg-buffercache">
69 <title>The
<structname>pg_buffercache
</structname> View
</title>
72 The definitions of the columns exposed by the view are shown in
<xref linkend=
"pgbuffercache-columns"/>.
75 <table id=
"pgbuffercache-columns">
76 <title><structname>pg_buffercache
</structname> Columns
</title>
80 <entry role=
"catalog_table_entry"><para role=
"column_definition">
91 <entry role=
"catalog_table_entry"><para role=
"column_definition">
92 <structfield>bufferid
</structfield> <type>integer
</type>
95 ID, in the range
1..
<varname>shared_buffers
</varname>
100 <entry role=
"catalog_table_entry"><para role=
"column_definition">
101 <structfield>relfilenode
</structfield> <type>oid
</type>
102 (references
<link linkend=
"catalog-pg-class"><structname>pg_class
</structname></link>.
<structfield>relfilenode
</structfield>)
105 Filenode number of the relation
110 <entry role=
"catalog_table_entry"><para role=
"column_definition">
111 <structfield>reltablespace
</structfield> <type>oid
</type>
112 (references
<link linkend=
"catalog-pg-tablespace"><structname>pg_tablespace
</structname></link>.
<structfield>oid
</structfield>)
115 Tablespace OID of the relation
120 <entry role=
"catalog_table_entry"><para role=
"column_definition">
121 <structfield>reldatabase
</structfield> <type>oid
</type>
122 (references
<link linkend=
"catalog-pg-database"><structname>pg_database
</structname></link>.
<structfield>oid
</structfield>)
125 Database OID of the relation
130 <entry role=
"catalog_table_entry"><para role=
"column_definition">
131 <structfield>relforknumber
</structfield> <type>smallint
</type>
134 Fork number within the relation; see
135 <filename>common/relpath.h
</filename>
140 <entry role=
"catalog_table_entry"><para role=
"column_definition">
141 <structfield>relblocknumber
</structfield> <type>bigint
</type>
144 Page number within the relation
149 <entry role=
"catalog_table_entry"><para role=
"column_definition">
150 <structfield>isdirty
</structfield> <type>boolean
</type>
158 <entry role=
"catalog_table_entry"><para role=
"column_definition">
159 <structfield>usagecount
</structfield> <type>smallint
</type>
162 Clock-sweep access count
167 <entry role=
"catalog_table_entry"><para role=
"column_definition">
168 <structfield>pinning_backends
</structfield> <type>integer
</type>
171 Number of backends pinning this buffer
179 There is one row for each buffer in the shared cache. Unused buffers are
180 shown with all fields null except
<structfield>bufferid
</structfield>. Shared system
181 catalogs are shown as belonging to database zero.
185 Because the cache is shared by all the databases, there will normally be
186 pages from relations not belonging to the current database. This means
187 that there may not be matching join rows in
<structname>pg_class
</structname> for
188 some rows, or that there could even be incorrect joins. If you are
189 trying to join against
<structname>pg_class
</structname>, it's a good idea to
190 restrict the join to rows having
<structfield>reldatabase
</structfield> equal to
191 the current database's OID or zero.
195 Since buffer manager locks are not taken to copy the buffer state data that
196 the view will display, accessing
<structname>pg_buffercache
</structname> view
197 has less impact on normal buffer activity but it doesn't provide a consistent
198 set of results across all buffers. However, we ensure that the information of
199 each buffer is self-consistent.
203 <sect2 id=
"pgbuffercache-summary">
204 <title>The
<function>pg_buffercache_summary()
</function> Function
</title>
207 The definitions of the columns exposed by the function are shown in
<xref linkend=
"pgbuffercache-summary-columns"/>.
210 <table id=
"pgbuffercache-summary-columns">
211 <title><function>pg_buffercache_summary()
</function> Output Columns
</title>
215 <entry role=
"catalog_table_entry"><para role=
"column_definition">
226 <entry role=
"catalog_table_entry"><para role=
"column_definition">
227 <structfield>buffers_used
</structfield> <type>int4
</type>
230 Number of used shared buffers
235 <entry role=
"catalog_table_entry"><para role=
"column_definition">
236 <structfield>buffers_unused
</structfield> <type>int4
</type>
239 Number of unused shared buffers
244 <entry role=
"catalog_table_entry"><para role=
"column_definition">
245 <structfield>buffers_dirty
</structfield> <type>int4
</type>
248 Number of dirty shared buffers
253 <entry role=
"catalog_table_entry"><para role=
"column_definition">
254 <structfield>buffers_pinned
</structfield> <type>int4
</type>
257 Number of pinned shared buffers
262 <entry role=
"catalog_table_entry"><para role=
"column_definition">
263 <structfield>usagecount_avg
</structfield> <type>float8
</type>
266 Average usage count of used shared buffers
274 The
<function>pg_buffercache_summary()
</function> function returns a
275 single row summarizing the state of all shared buffers. Similar and more
276 detailed information is provided by the
277 <structname>pg_buffercache
</structname> view, but
278 <function>pg_buffercache_summary()
</function> is significantly cheaper.
282 Like the
<structname>pg_buffercache
</structname> view,
283 <function>pg_buffercache_summary()
</function> does not acquire buffer
284 manager locks. Therefore concurrent activity can lead to minor inaccuracies
289 <sect2 id=
"pgbuffercache-usage-counts">
290 <title>The
<function>pg_buffercache_usage_counts()
</function> Function
</title>
293 The definitions of the columns exposed by the function are shown in
294 <xref linkend=
"pgbuffercache_usage_counts-columns"/>.
297 <table id=
"pgbuffercache_usage_counts-columns">
298 <title><function>pg_buffercache_usage_counts()
</function> Output Columns
</title>
302 <entry role=
"catalog_table_entry"><para role=
"column_definition">
313 <entry role=
"catalog_table_entry"><para role=
"column_definition">
314 <structfield>usage_count
</structfield> <type>int4
</type>
317 A possible buffer usage count
322 <entry role=
"catalog_table_entry"><para role=
"column_definition">
323 <structfield>buffers
</structfield> <type>int4
</type>
326 Number of buffers with the usage count
331 <entry role=
"catalog_table_entry"><para role=
"column_definition">
332 <structfield>dirty
</structfield> <type>int4
</type>
335 Number of dirty buffers with the usage count
340 <entry role=
"catalog_table_entry"><para role=
"column_definition">
341 <structfield>pinned
</structfield> <type>int4
</type>
344 Number of pinned buffers with the usage count
352 The
<function>pg_buffercache_usage_counts()
</function> function returns a
353 set of rows summarizing the states of all shared buffers, aggregated over
354 the possible usage count values. Similar and more detailed information is
355 provided by the
<structname>pg_buffercache
</structname> view, but
356 <function>pg_buffercache_usage_counts()
</function> is significantly cheaper.
360 Like the
<structname>pg_buffercache
</structname> view,
361 <function>pg_buffercache_usage_counts()
</function> does not acquire buffer
362 manager locks. Therefore concurrent activity can lead to minor inaccuracies
367 <sect2 id=
"pgbuffercache-pg-buffercache-evict">
368 <title>The
<structname>pg_buffercache_evict
</structname> Function
</title>
370 The
<function>pg_buffercache_evict()
</function> function takes a buffer
371 identifier, as shown in the
<structfield>bufferid
</structfield> column of
372 the
<structname>pg_buffercache
</structname> view. It returns true on success,
373 and false if the buffer wasn't valid, if it couldn't be evicted because it
374 was pinned, or if it became dirty again after an attempt to write it out.
375 The result is immediately out of date upon return, as the buffer might
376 become valid again at any time due to concurrent activity. The function is
377 intended for developer testing only.
381 <sect2 id=
"pgbuffercache-sample-output">
382 <title>Sample Output
</title>
385 regression=# SELECT n.nspname, c.relname, count(*) AS buffers
386 FROM pg_buffercache b JOIN pg_class c
387 ON b.relfilenode = pg_relation_filenode(c.oid) AND
388 b.reldatabase IN (
0, (SELECT oid FROM pg_database
389 WHERE datname = current_database()))
390 JOIN pg_namespace n ON n.oid = c.relnamespace
391 GROUP BY n.nspname, c.relname
395 nspname | relname | buffers
396 ------------+------------------------+---------
397 public | delete_test_table |
593
398 public | delete_test_table_pkey |
494
399 pg_catalog | pg_attribute |
472
400 public | quad_poly_tbl |
353
403 public | gin_test_idx |
306
404 pg_catalog | pg_largeobject |
206
405 public | gin_test_tbl |
188
406 public | spgist_text_tbl |
182
410 regression=# SELECT * FROM pg_buffercache_summary();
411 buffers_used | buffers_unused | buffers_dirty | buffers_pinned | usagecount_avg
412 --------------+----------------+---------------+----------------+----------------
413 248 |
2096904 |
39 |
0 |
3.141129
417 regression=# SELECT * FROM pg_buffercache_usage_counts();
418 usage_count | buffers | dirty | pinned
419 -------------+---------+-------+--------
430 <sect2 id=
"pgbuffercache-authors">
431 <title>Authors
</title>
434 Mark Kirkwood
<email>markir@paradise.net.nz
</email>
438 Design suggestions: Neil Conway
<email>neilc@samurai.com
</email>
442 Debugging advice: Tom Lane
<email>tgl@sss.pgh.pa.us
</email>