Hi,

I encountered an assertion failure when a partitioned table is specified
as an argument to pg_prewarm. Below are the steps to reproduce the issue:

$ pgbench -i -s 1 --partitions=3
$ psql <<EOF
CREATE EXTENSION pg_prewarm;
SELECT pg_prewarm('pgbench_accounts');
EOF

The following assertion failure occurs:

TRAP: failed Assert("RelFileNumberIsValid(rlocator.relNumber)"), File: "smgr.c", Line: 246, PID: 1246282 postgres: ikeda postgres [local] SELECT(ExceptionalCondition+0xbb)[0x55edd16725c1]
postgres: ikeda postgres [local] SELECT(smgropen+0x5e)[0x55edd145c1ff]


It looks like this may have been overlooked in commit 049ef33.
What do you think?

Regards,
--
Masahiro Ikeda
NTT DATA CORPORATION
From 3c27152719f3bbc84d61dc136584f530a75e6da1 Mon Sep 17 00:00:00 2001
From: Masahiro Ikeda <ikeda...@oss.nttdata.com>
Date: Thu, 15 May 2025 17:26:27 +0900
Subject: [PATCH v1] Fix assertion failure when pg_prewarm is used on a
 partitioned table

This issue was introduced by commit 049ef33.

Specifying a partitioned table 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/pg_prewarm.c   |  3 ++-
 contrib/pg_prewarm/t/001_basic.pl | 11 ++++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index 50808569bd7..94a36795130 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -113,7 +113,8 @@ pg_prewarm(PG_FUNCTION_ARGS)
 		aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind), get_rel_name(relOid));
 
 	/* Check that the fork exists. */
-	if (!smgrexists(RelationGetSmgr(rel), forkNumber))
+	if (!RelFileNumberIsValid(rel->rd_locator.relNumber) ||
+			!smgrexists(RelationGetSmgr(rel), forkNumber))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 errmsg("fork \"%s\" does not exist for this relation",
diff --git a/contrib/pg_prewarm/t/001_basic.pl b/contrib/pg_prewarm/t/001_basic.pl
index 0a8259d3678..57a0134454b 100644
--- a/contrib/pg_prewarm/t/001_basic.pl
+++ b/contrib/pg_prewarm/t/001_basic.pl
@@ -23,7 +23,10 @@ $node->start;
 $node->safe_psql("postgres",
 		"CREATE EXTENSION pg_prewarm;\n"
 	  . "CREATE TABLE test(c1 int);\n"
-	  . "INSERT INTO test SELECT generate_series(1, 100);");
+	  . "INSERT INTO test SELECT generate_series(1, 100);\n"
+	  . "CREATE TABLE test_part(c1 int) PARTITION BY RANGE (c1);\n"
+	  . "CREATE TABLE test_part1 PARTITION OF test_part FOR VALUES FROM (1) TO (1000);\n"
+	  . "INSERT INTO test_part SELECT generate_series(1, 100);");
 
 # test read mode
 my $result =
@@ -42,6 +45,12 @@ ok( (        $stdout =~ qr/^[1-9][0-9]*$/
 		  or $stderr =~ qr/prefetch is not supported by this build/),
 	'prefetch mode succeeded');
 
+# test partition table
+($cmdret, $stdout, $stderr) =
+  $node->psql("postgres", "SELECT pg_prewarm('test_part', 'buffer');");
+ok( $stderr =~ /fork "main" does not exist for this relation/,
+	'detected the fork does not exist');
+
 # test autoprewarm_dump_now()
 $result = $node->safe_psql("postgres", "SELECT autoprewarm_dump_now();");
 like($result, qr/^[1-9][0-9]*$/, 'autoprewarm_dump_now succeeded');
-- 
2.34.1

Reply via email to