Skip to content

Commit ba2a3c2

Browse files
committed
Add pg_buffercache_numa view with NUMA node info
Introduces a new view pg_buffercache_numa, showing NUMA memory nodes for individual buffers. For each buffer the view returns an entry for each memory page, with the associated NUMA node. The database blocks and OS memory pages may have different size - the default block size is 8KB, while the memory page is 4K (on x86). But other combinations are possible, depending on configure parameters, platform, etc. This means buffers may overlap with multiple memory pages, each associated with a different NUMA node. To determine the NUMA node for a buffer, we first need to touch the memory pages using pg_numa_touch_mem_if_required, otherwise we might get status -2 (ENOENT = The page is not present), indicating the page is either unmapped or unallocated. The view may be relatively expensive, especially when accessed for the first time in a backend, as it touches all memory pages to get reliable information about the NUMA node. This may also force allocation of the shared memory. Author: Jakub Wartak <[email protected]> Reviewed-by: Andres Freund <[email protected]> Reviewed-by: Bertrand Drouvot <[email protected]> Reviewed-by: Tomas Vondra <[email protected]> Discussion: https://postgr.es/m/CAKZiRmxh6KWo0aqRqvmcoaX2jUxZYb4kGp3N%3Dq1w%2BDiH-696Xw%40mail.gmail.com
1 parent 8cc139b commit ba2a3c2

10 files changed

+452
-4
lines changed

contrib/pg_buffercache/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ OBJS = \
88
EXTENSION = pg_buffercache
99
DATA = pg_buffercache--1.2.sql pg_buffercache--1.2--1.3.sql \
1010
pg_buffercache--1.1--1.2.sql pg_buffercache--1.0--1.1.sql \
11-
pg_buffercache--1.3--1.4.sql pg_buffercache--1.4--1.5.sql
11+
pg_buffercache--1.3--1.4.sql pg_buffercache--1.4--1.5.sql \
12+
pg_buffercache--1.5--1.6.sql
1213
PGFILEDESC = "pg_buffercache - monitoring of shared buffer cache in real-time"
1314

14-
REGRESS = pg_buffercache
15+
REGRESS = pg_buffercache pg_buffercache_numa
1516

1617
ifdef USE_PGXS
1718
PG_CONFIG = pg_config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
SELECT NOT(pg_numa_available()) AS skip_test \gset
2+
\if :skip_test
3+
\quit
4+
\endif
5+
-- We expect at least one entry for each buffer
6+
select count(*) >= (select setting::bigint
7+
from pg_settings
8+
where name = 'shared_buffers')
9+
from pg_buffercache_numa;
10+
?column?
11+
----------
12+
t
13+
(1 row)
14+
15+
-- Check that the functions / views can't be accessed by default. To avoid
16+
-- having to create a dedicated user, use the pg_database_owner pseudo-role.
17+
SET ROLE pg_database_owner;
18+
SELECT count(*) > 0 FROM pg_buffercache_numa;
19+
ERROR: permission denied for view pg_buffercache_numa
20+
RESET role;
21+
-- Check that pg_monitor is allowed to query view / function
22+
SET ROLE pg_monitor;
23+
SELECT count(*) > 0 FROM pg_buffercache_numa;
24+
?column?
25+
----------
26+
t
27+
(1 row)
28+
29+
RESET role;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT NOT(pg_numa_available()) AS skip_test \gset
2+
\if :skip_test
3+
\quit

contrib/pg_buffercache/meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ install_data(
2323
'pg_buffercache--1.2.sql',
2424
'pg_buffercache--1.3--1.4.sql',
2525
'pg_buffercache--1.4--1.5.sql',
26+
'pg_buffercache--1.5--1.6.sql',
2627
'pg_buffercache.control',
2728
kwargs: contrib_data_args,
2829
)
@@ -34,6 +35,7 @@ tests += {
3435
'regress': {
3536
'sql': [
3637
'pg_buffercache',
38+
'pg_buffercache_numa',
3739
],
3840
},
3941
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "ALTER EXTENSION pg_buffercache UPDATE TO '1.6'" to load this file. \quit
5+
6+
-- Register the new functions.
7+
CREATE OR REPLACE FUNCTION pg_buffercache_numa_pages()
8+
RETURNS SETOF RECORD
9+
AS 'MODULE_PATHNAME', 'pg_buffercache_numa_pages'
10+
LANGUAGE C PARALLEL SAFE;
11+
12+
-- Create a view for convenient access.
13+
CREATE VIEW pg_buffercache_numa AS
14+
SELECT P.* FROM pg_buffercache_numa_pages() AS P
15+
(bufferid integer, os_page_num int4, numa_node int4);
16+
17+
-- Don't want these to be available to public.
18+
REVOKE ALL ON FUNCTION pg_buffercache_numa_pages() FROM PUBLIC;
19+
REVOKE ALL ON pg_buffercache_numa FROM PUBLIC;
20+
21+
GRANT EXECUTE ON FUNCTION pg_buffercache_numa_pages() TO pg_monitor;
22+
GRANT SELECT ON pg_buffercache_numa TO pg_monitor;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_buffercache extension
22
comment = 'examine the shared buffer cache'
3-
default_version = '1.5'
3+
default_version = '1.6'
44
module_pathname = '$libdir/pg_buffercache'
55
relocatable = true

0 commit comments

Comments
 (0)