Hi psql hackers,

While reviewing contrib/pg_prewarm/pg_prewarm.c, I noticed that
pg_prewarm() does not validate that first_block <= last_block after
both values are resolved. When a caller passes a reversed range such
as pg_prewarm('mytable', 'buffer', 'main', 100, 50), the function
silently returns 0 without any indication that the range was empty.
Both block numbers pass their individual bounds checks, so the caller
gets no feedback that anything went wrong.

The fix adds an explicit check after both endpoints are fully resolved
(including NULL defaults) and emits an ERROR message.

Reproduction:

  CREATE TABLE t (id int);
  INSERT INTO t SELECT generate_series(1, 10000);

  -- Before patch: silently returns 0
  SELECT pg_prewarm('t', 'buffer', 'main', 100, 50);
   pg_prewarm
  ------------
            0
  (1 row)

  -- After patch: raises a clear error

 SELECT pg_prewarm('t', 'buffer', 'main', 100, 50);
ERROR:  ending block number cannot be less than starting block number

The patch is attached. It touches only contrib/pg_prewarm/pg_prewarm.c

-- 

Thanks & Regards,

*Jhon k*

Postgres Specialist

Project & IT Department

Cybrosys Technologies

Mail

Mobile

WhatsApp


[email protected]

+91 8606827707

+91 8606827707
From 20f857a8a296b1200e477bfd9c15d6218ad363ad Mon Sep 17 00:00:00 2001
From: demo <[email protected]>
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: <[email protected]>
Author: <[email protected]>
---
 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

Reply via email to