From 16027ecedc4301904965306d8a495f7a08c6d798 Mon Sep 17 00:00:00 2001
From: Masahiro Ikeda <ikedamsh@oss.nttdata.com>
Date: Mon, 19 May 2025 16:24:55 +0900
Subject: [PATCH v3 1/2] Fix assertion failure when pg_prewarm() is run on
 objects that don't have storage.

This issue was introduced by commit 049ef33.

Specifying objects that don't have storage as an argument to
pg_prewarm() could trigger an assertion failure:

  Failed Assert("RelFileNumberIsValid(rlocator.relNumber)")

This fix ensures that such cases are handled appropriately.
---
 contrib/pg_prewarm/Makefile                |  2 ++
 contrib/pg_prewarm/expected/pg_prewarm.out | 12 ++++++++++++
 contrib/pg_prewarm/meson.build             |  5 +++++
 contrib/pg_prewarm/pg_prewarm.c            |  8 ++++++++
 contrib/pg_prewarm/sql/pg_prewarm.sql      | 12 ++++++++++++
 5 files changed, 39 insertions(+)
 create mode 100644 contrib/pg_prewarm/expected/pg_prewarm.out
 create mode 100644 contrib/pg_prewarm/sql/pg_prewarm.sql

diff --git a/contrib/pg_prewarm/Makefile b/contrib/pg_prewarm/Makefile
index 9cfde8c4e4f..617ac8e09b2 100644
--- a/contrib/pg_prewarm/Makefile
+++ b/contrib/pg_prewarm/Makefile
@@ -10,6 +10,8 @@ EXTENSION = pg_prewarm
 DATA = pg_prewarm--1.1--1.2.sql pg_prewarm--1.1.sql pg_prewarm--1.0--1.1.sql
 PGFILEDESC = "pg_prewarm - preload relation data into system buffer cache"
 
+REGRESS = pg_prewarm
+
 TAP_TESTS = 1
 
 ifdef USE_PGXS
diff --git a/contrib/pg_prewarm/expected/pg_prewarm.out b/contrib/pg_prewarm/expected/pg_prewarm.out
new file mode 100644
index 00000000000..3bb644499d4
--- /dev/null
+++ b/contrib/pg_prewarm/expected/pg_prewarm.out
@@ -0,0 +1,12 @@
+-- Test pg_prewarm extension
+CREATE EXTENSION pg_prewarm;
+-- To specify the relation which does not have storage should fail.
+CREATE TABLE test (c1 int) PARTITION BY RANGE (c1);
+CREATE TABLE test_part1 PARTITION OF test FOR VALUES FROM (1) TO (1000);
+INSERT INTO test SELECT generate_series(1, 100);
+SELECT pg_prewarm('test', 'buffer');
+ERROR:  relation "test" does not have storage
+DETAIL:  This operation is not supported for partitioned tables.
+-- Cleanup
+DROP TABLE test;
+DROP EXTENSION pg_prewarm;
diff --git a/contrib/pg_prewarm/meson.build b/contrib/pg_prewarm/meson.build
index 82b9851303c..f24c47ef6a5 100644
--- a/contrib/pg_prewarm/meson.build
+++ b/contrib/pg_prewarm/meson.build
@@ -29,6 +29,11 @@ tests += {
   'name': 'pg_prewarm',
   'sd': meson.current_source_dir(),
   'bd': meson.current_build_dir(),
+  'regress': {
+    'sql': [
+      'pg_prewarm',
+    ],
+  },
   'tap': {
     'tests': [
       't/001_basic.pl',
diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index 50808569bd7..1f89c9adc86 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -112,6 +112,14 @@ pg_prewarm(PG_FUNCTION_ARGS)
 	if (aclresult != ACLCHECK_OK)
 		aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
 
+	/* Check that the storage exists. */
+	if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("relation \"%s\" does not have storage",
+						RelationGetRelationName(rel)),
+				 errdetail_relkind_not_supported(rel->rd_rel->relkind)));
+
 	/* Check that the fork exists. */
 	if (!smgrexists(RelationGetSmgr(rel), forkNumber))
 		ereport(ERROR,
diff --git a/contrib/pg_prewarm/sql/pg_prewarm.sql b/contrib/pg_prewarm/sql/pg_prewarm.sql
new file mode 100644
index 00000000000..2743fa5e778
--- /dev/null
+++ b/contrib/pg_prewarm/sql/pg_prewarm.sql
@@ -0,0 +1,12 @@
+-- Test pg_prewarm extension
+CREATE EXTENSION pg_prewarm;
+
+-- To specify the relation which does not have storage should fail.
+CREATE TABLE test (c1 int) PARTITION BY RANGE (c1);
+CREATE TABLE test_part1 PARTITION OF test FOR VALUES FROM (1) TO (1000);
+INSERT INTO test SELECT generate_series(1, 100);
+SELECT pg_prewarm('test', 'buffer');
+
+-- Cleanup
+DROP TABLE test;
+DROP EXTENSION pg_prewarm;
-- 
2.34.1

