From 7b279721ae04e823f20e94331dd3b0a634ff3e7f Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Sun, 6 Apr 2025 14:19:41 +0200
Subject: [PATCH v25 6/6] fixes for review by Andres

---
 contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql | 2 +-
 contrib/pg_buffercache/pg_buffercache_pages.c       | 8 --------
 doc/src/sgml/system-views.sgml                      | 8 +++++++-
 meson.build                                         | 2 +-
 src/include/port/pg_numa.h                          | 2 +-
 src/test/regress/expected/numa.out                  | 1 +
 src/test/regress/expected/numa_1.out                | 2 ++
 src/test/regress/sql/numa.sql                       | 1 +
 8 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql b/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
index 1230e244a5f..e3b145a1687 100644
--- a/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
+++ b/contrib/pg_buffercache/pg_buffercache--1.5--1.6.sql
@@ -10,7 +10,7 @@ AS 'MODULE_PATHNAME', 'pg_buffercache_numa_pages'
 LANGUAGE C PARALLEL SAFE;
 
 -- Create a view for convenient access.
-CREATE OR REPLACE VIEW pg_buffercache_numa AS
+CREATE VIEW pg_buffercache_numa AS
 	SELECT P.* FROM pg_buffercache_numa_pages() AS P
 	(bufferid integer, page_num int4, node_id int4);
 
diff --git a/contrib/pg_buffercache/pg_buffercache_pages.c b/contrib/pg_buffercache/pg_buffercache_pages.c
index a3c4a2578d9..df94cc6ef7d 100644
--- a/contrib/pg_buffercache/pg_buffercache_pages.c
+++ b/contrib/pg_buffercache/pg_buffercache_pages.c
@@ -375,14 +375,6 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS)
 		/* Create a user function context for cross-call persistence */
 		fctx = (BufferCacheNumaContext *) palloc(sizeof(BufferCacheNumaContext));
 
-		/*
-		 * To smoothly support upgrades from version 1.0 of this extension
-		 * transparently handle the (non-)existence of the pinning_backends
-		 * column. We unfortunately have to get the result type for that... -
-		 * we can't use the result type determined by the function definition
-		 * without potentially crashing when somebody uses the old (or even
-		 * wrong) function definition though.
-		 */
 		if (get_call_result_type(fcinfo, NULL, &expected_tupledesc) != TYPEFUNC_COMPOSITE)
 			elog(ERROR, "return type must be a row type");
 
diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml
index a83365ae24a..4e853885de6 100644
--- a/doc/src/sgml/system-views.sgml
+++ b/doc/src/sgml/system-views.sgml
@@ -4069,7 +4069,13 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
    across NUMA nodes. This includes both memory allocated by
    <productname>PostgreSQL</productname> itself and memory allocated
    by extensions using the mechanisms detailed in
-   <xref linkend="xfunc-shared-addin" />.
+   <xref linkend="xfunc-shared-addin" />. This view will output multiple rows
+   for each of the shared memory segments provided that they are spread accross
+   multiple NUMA nodes. This view should not be queried by monitoring systems
+   as it is very slow and may end up allocating shared memory in case it was not
+   used earlier.
+   Current limitation for this view is that won't show anonymous shared memory
+   allocations.
   </para>
 
   <para>
diff --git a/meson.build b/meson.build
index b562a00c588..a1516e54529 100644
--- a/meson.build
+++ b/meson.build
@@ -950,7 +950,7 @@ endif
 libnumaopt = get_option('libnuma')
 if not libnumaopt.disabled()
   # via pkg-config
-  libnuma = dependency('numa', required: libnumaopt)
+  libnuma = dependency('numa', required: false)
   if not libnuma.found()
     libnuma = cc.find_library('numa', required: libnumaopt)
   endif
diff --git a/src/include/port/pg_numa.h b/src/include/port/pg_numa.h
index 3c1b50c1428..7e990d9f776 100644
--- a/src/include/port/pg_numa.h
+++ b/src/include/port/pg_numa.h
@@ -28,7 +28,7 @@ extern PGDLLIMPORT Size pg_numa_get_pagesize(void);
  * need to page-fault before move_pages(2) syscall returns valid results.
  */
 #define pg_numa_touch_mem_if_required(ro_volatile_var, ptr) \
-	ro_volatile_var = *(uint64 *) ptr
+	ro_volatile_var = *(volatile uint64 *) ptr
 
 #else
 
diff --git a/src/test/regress/expected/numa.out b/src/test/regress/expected/numa.out
index 668172f7d79..8af5dfeb9a5 100644
--- a/src/test/regress/expected/numa.out
+++ b/src/test/regress/expected/numa.out
@@ -1,5 +1,6 @@
 SELECT NOT(pg_numa_available()) AS skip_test \gset
 \if :skip_test
+SELECT COUNT(*) = 0 AS ok FROM pg_shmem_allocations_numa;
 \quit
 \endif
 -- switch to superuser
diff --git a/src/test/regress/expected/numa_1.out b/src/test/regress/expected/numa_1.out
index 6dd6824b4e4..c90042fa7cc 100644
--- a/src/test/regress/expected/numa_1.out
+++ b/src/test/regress/expected/numa_1.out
@@ -1,3 +1,5 @@
 SELECT NOT(pg_numa_available()) AS skip_test \gset
 \if :skip_test
+SELECT COUNT(*) = 0 AS ok FROM pg_shmem_allocations_numa;
+ERROR:  libnuma initialization failed or NUMA is not supported on this platform
 \quit
diff --git a/src/test/regress/sql/numa.sql b/src/test/regress/sql/numa.sql
index 034098783fb..324481c33b7 100644
--- a/src/test/regress/sql/numa.sql
+++ b/src/test/regress/sql/numa.sql
@@ -1,5 +1,6 @@
 SELECT NOT(pg_numa_available()) AS skip_test \gset
 \if :skip_test
+SELECT COUNT(*) = 0 AS ok FROM pg_shmem_allocations_numa;
 \quit
 \endif
 
-- 
2.39.5

