pg_buffercache

PostgreSQL 9.1.23 Documentation
Prev | Up | Appendix F. Additional Supplied Modules | Next

# F.27. pg_buffercache

The `pg_buffercache` module provides a means for examining what's happening in the shared buffer cache in real time.

The module provides a C function `pg_buffercache_pages` that returns a set of records, plus a view `pg_buffercache` that wraps the function for convenient use.

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

## F.27.1. The `pg_buffercache` View

The definitions of the columns exposed by the view are shown in Table F-14.

Table F-14. `pg_buffercache` Columns

Name | Type | References | Description
--- | --- | --- | ---
`bufferid` | `integer` |  | ID, in the range 1..`shared_buffers`
`relfilenode` | `oid` | `pg_class.relfilenode` | Filenode number of the relation
`reltablespace` | `oid` | `pg_tablespace.oid` | Tablespace OID of the relation
`reldatabase` | `oid` | `pg_database.oid` | Database OID of the relation
`relblocknumber` | `bigint` |  | Page number within the relation
`relforknumber` | `smallint` |  | Fork number within the relation
`isdirty` | `boolean` |  | Is the page dirty?
`usagecount` | `smallint` |  | Page LRU count

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

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 `pg_class` for some rows, or that there could even be incorrect joins. If you are trying to join against `pg_class`, it's a good idea to restrict the join to rows having `reldatabase` equal to the current database's OID or zero.

When the `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.

## F.27.2. Sample Output

    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)

## F.27.3. Authors

Mark Kirkwood `<markir@paradise.net.nz>`

Design suggestions: Neil Conway `<neilc@samurai.com>`

Debugging advice: Tom Lane `<tgl@sss.pgh.pa.us>`

Prev | Home | Next
pgbench | Up | pgcrypto