From 20f857a8a296b1200e477bfd9c15d6218ad363ad Mon Sep 17 00:00:00 2001
From: demo <demo@gmail.com>
Date: Fri, 27 Mar 2026 10:56:14 +0530
Subject: [PATCH] pg_prewarm: validate that first_block <= last_block

pg_prewarm() accepted a block range where first_block > last_block
without complaint, silently returning 0 blocks prewarmed. Both block
numbers could individually pass their own range checks, yet the
resulting range is logically empty with no indication to the caller.

Add an explicit check after both first_block and last_block are fully
resolved (including NULL defaults) so that we can compare the two
finalized values and emit a clear error with errdetail showing the
exact values the caller supplied.

Reported-by: <postgress@cybrosys.com>
Author: <postgress@cybrosys.com>
---
 contrib/pg_prewarm/pg_prewarm.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/contrib/pg_prewarm/pg_prewarm.c b/contrib/pg_prewarm/pg_prewarm.c
index c2716086693..b4d4a53dc84 100644
--- a/contrib/pg_prewarm/pg_prewarm.c
+++ b/contrib/pg_prewarm/pg_prewarm.c
@@ -189,6 +189,12 @@ pg_prewarm(PG_FUNCTION_ARGS)
 					 errmsg("ending block number must be between 0 and %" PRId64,
 							(nblocks - 1))));
 	}
+	if (last_block < first_block)
+	{
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("ending block number cannot be less than starting block number")));
+	}
 
 	/* Now we're ready to do the real work. */
 	if (ptype == PREWARM_PREFETCH)
-- 
2.34.1

