pg_buffercache is the contrib module that exposes the shared buffer cache - every buffer slot,
which relation and block it holds, and how dirty it is. It exists for inspection and for testing:
the pg_buffercache view shows contents, pg_buffercache_summary() aggregates it,
pg_buffercache_usage_counts() buckets buffers by usage count, and the pg_buffercache_evict()
family can push pages out to simulate a cold cache.
PostgreSQL 19 makes two additions: a reworked way to see how buffers map to operating-system memory, and a way to mark buffers dirty without writing to them.
Mapping buffers to OS pages
19 refactors how the module reports shared memory mapping. A new function,
pg_buffercache_os_pages(boolean), backs two views:
| View | Wraps | Columns |
|---|---|---|
pg_buffercache_os_pages |
pg_buffercache_os_pages(false) |
bufferid, os_page_num |
pg_buffercache_numa |
pg_buffercache_os_pages(true) |
bufferid, os_page_num, numa_node |
os_page_num is the OS memory page backing that buffer slot. The first use is auditing:
shared_buffers asks for a large shared segment, and huge_pages controls how the OS supplies it -
this view lets you confirm the mapping actually happened rather than trusting that it did. The
second is NUMA diagnosis: pg_buffercache_numa shows which memory node each buffer lives on, which
matters on multi-socket machines where remote memory access costs measurably more.
The earlier interface, function pg_buffercache_numa_pages(), remains for backward compatibility -
scripts written against it still run.
CREATE EXTENSION pg_buffercache;
-- which OS pages back the buffer pool
SELECT * FROM pg_buffercache_os_pages LIMIT 5;
-- how the pool is spread across NUMA nodes
SELECT numa_node, count(*)
FROM pg_buffercache_numa
GROUP BY numa_node
ORDER BY numa_node;
The NUMA warning, because it is unusual
The documentation attaches a warning to the NUMA view that is worth quoting in substance: determining the NUMA node touches every memory page of the shared memory segment. Three consequences:
- It forces the allocation - if the shared memory was not already paged in, querying the view pages it in.
- It can mislead the thing you are measuring - depending on system configuration, the memory may get allocated on a single NUMA node by the act of asking, so a snapshot taken too early reports the query’s footprint rather than steady-state locality.
- It is expensive on every call - the first execution can take noticeable time, and the docs advise against querying the view at high frequency. It is a diagnostic you run deliberately, not a metric you poll.
Marking buffers dirty
The evict functions let you empty the cache; mark_dirty fills the other half of the test rig -
dirtying buffers so that checkpoint and background-writer behaviour can be exercised without
generating real writes:
| Function | Scope | Returns |
|---|---|---|
pg_buffercache_mark_dirty(bufferid) |
One buffer | buffer_dirtied - false if it was already dirty, invalid, or pinned |
pg_buffercache_mark_dirty_relation(relation) |
All forks of one relation | Counts: dirtied, already dirty, skipped (pinned or invalid) |
pg_buffercache_mark_dirty_all() |
Every unpinned buffer | Counts, same shape |
The relation and all variants report the buffer pool as it was when they ran - the documentation
notes the counts are immediately out of date, since other backends keep changing the pool. The
point is not precision but making a checkpoint have something to do: dirty ten thousand buffers,
watch checkpoints_timed versus checkpoints_req, or test bgwriter tuning against a known
dirty pool.
Access control
Reading the cache is available to superusers and roles with pg_monitor privileges, grantable
with GRANT. Anything that changes the pool - pg_buffercache_evict* and
pg_buffercache_mark_dirty* - is superuser-only. On a production system the write functions
belong behind that restriction; on a test rig they are the reason to load the module.
What changed, and in which release
| Need | PG 18 and earlier | PG 19 | |
|---|---|---|---|
| Buffer contents and summary | pg_buffercache view, summary(), usage_counts() |
Unchanged | |
| NUMA mapping | pg_buffercache_numa_pages() function |
pg_buffercache_os_pages / pg_buffercache_numa views; old function kept for compatibility |
|
| Dirty buffers for testing | Not possible from SQL | mark_dirty, mark_dirty_relation, mark_dirty_all |
|
| Eviction | pg_buffercache_evict family |
Unchanged |
Where it breaks
- Reading too much meaning into
os_page_num- it is a mapping, not a residency guarantee; a page mapped is not a page that stays resident under memory pressure. - Polling
pg_buffercache_numa- the view touches the whole segment on every call; treat it as a one-shot diagnostic. mark_dirty_all()on anything shared - it dirties the whole pool, which means the next checkpoint does real work; that is the point on a benchmark and a hazard anywhere else.- Expecting clean counts -
mark_dirtyresults report a moving target; on a busy system another backend may re-dirty or evict between your call and your check. - Assuming the compatibility function is permanent -
pg_buffercache_numa_pages()is retained for backward compatibility rather than as the preferred interface; write new tooling againstpg_buffercache_os_pages().
What this is not for
- Fixing cache hit rates - it shows the cache, it does not change hit rates;
shared_bufferssizing and workload analysis come first. - Real-time alerting - the NUMA mapping especially is a deliberate diagnostic, not a per-second
metric. For the cumulative counters, the monitoring
page covers what 19 adds there, including the
pg_dsm_registry_allocationsview for the rest of shared memory.
PostgreSQL release support
5 supported| Release | Latest | Released |
|---|---|---|
19 | 19.0 | Nov 2026 |
18 | 18.7 | Sept 2025 |
17 | 17.12 | Sept 2024 |
16 | 16.16 | Sept 2023 |
15 | 15.20 | Oct 2022 |
Data as of 2026-11-21 from endoflife.date. Refresh with node scripts/fetch-versions.mjs.
What to watch
Specific things that would change what this page concludes.
- pg_buffercache_numa_pages() shipped for backward compatibility; watch for a deprecation notice in a later release
- Whether the NUMA view's cost is reduced - today it touches every page of the shared segment
Where PostgreSQL sits
How this component relates to the rest of the stack. Items without a link are not covered here yet.