From 728184910b7274d64c4ea25ed3fd2bbfb875b8b7 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Wed, 7 Jan 2026 16:36:36 +0300
Subject: [PATCH v2 2/2] Remove DatumGetUInt8 and UInt8GetDatum

These functions were rarely used and created some confusion. Replace the few
existing usages with more appropriate alternatives:

- use Int16GetDatum in heapfuncs.c
- use CharGetDatum/DatumGetChar in nbtcompare.c

Also update the char increment/decrement functions to use proper SCHAR_MIN and
SCHAR_MAX boundaries instead of 0/UCHAR_MAX.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Suggested-by: Tom Lane <tgl@sss.pgh.pa.us>
Suggested-by: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/CALdSSPhFyb9qLSHee73XtZm1CBWJNo9%2BJzFNf-zUEWCRW5yEiQ%40mail.gmail.com
---
 contrib/pageinspect/heapfuncs.c        |  2 +-
 src/backend/access/nbtree/nbtcompare.c | 16 ++++++++--------
 src/include/postgres.h                 | 19 -------------------
 3 files changed, 9 insertions(+), 28 deletions(-)

diff --git a/contrib/pageinspect/heapfuncs.c b/contrib/pageinspect/heapfuncs.c
index 1cf0b44e731..8b35b3c337a 100644
--- a/contrib/pageinspect/heapfuncs.c
+++ b/contrib/pageinspect/heapfuncs.c
@@ -223,7 +223,7 @@ heap_page_items(PG_FUNCTION_ARGS)
 			values[7] = PointerGetDatum(&tuphdr->t_ctid);
 			values[8] = UInt32GetDatum(tuphdr->t_infomask2);
 			values[9] = UInt32GetDatum(tuphdr->t_infomask);
-			values[10] = UInt8GetDatum(tuphdr->t_hoff);
+			values[10] = Int16GetDatum(tuphdr->t_hoff);
 
 			/*
 			 * We already checked that the item is completely within the raw
diff --git a/src/backend/access/nbtree/nbtcompare.c b/src/backend/access/nbtree/nbtcompare.c
index 8425805a292..2a123082d86 100644
--- a/src/backend/access/nbtree/nbtcompare.c
+++ b/src/backend/access/nbtree/nbtcompare.c
@@ -617,9 +617,9 @@ btcharcmp(PG_FUNCTION_ARGS)
 static Datum
 char_decrement(Relation rel, Datum existing, bool *underflow)
 {
-	uint8		cexisting = DatumGetUInt8(existing);
+	char		cexisting = DatumGetChar(existing);
 
-	if (cexisting == 0)
+	if (cexisting == SCHAR_MIN)
 	{
 		/* return value is undefined */
 		*underflow = true;
@@ -627,15 +627,15 @@ char_decrement(Relation rel, Datum existing, bool *underflow)
 	}
 
 	*underflow = false;
-	return CharGetDatum((uint8) cexisting - 1);
+	return CharGetDatum(cexisting - 1);
 }
 
 static Datum
 char_increment(Relation rel, Datum existing, bool *overflow)
 {
-	uint8		cexisting = DatumGetUInt8(existing);
+	char		cexisting = DatumGetChar(existing);
 
-	if (cexisting == UCHAR_MAX)
+	if (cexisting == SCHAR_MAX)
 	{
 		/* return value is undefined */
 		*overflow = true;
@@ -643,7 +643,7 @@ char_increment(Relation rel, Datum existing, bool *overflow)
 	}
 
 	*overflow = false;
-	return CharGetDatum((uint8) cexisting + 1);
+	return CharGetDatum(cexisting + 1);
 }
 
 Datum
@@ -655,8 +655,8 @@ btcharskipsupport(PG_FUNCTION_ARGS)
 	sksup->increment = char_increment;
 
 	/* btcharcmp compares chars as unsigned */
-	sksup->low_elem = UInt8GetDatum(0);
-	sksup->high_elem = UInt8GetDatum(UCHAR_MAX);
+	sksup->low_elem = CharGetDatum(SCHAR_MIN);
+	sksup->high_elem = CharGetDatum(SCHAR_MAX);
 
 	PG_RETURN_VOID();
 }
diff --git a/src/include/postgres.h b/src/include/postgres.h
index 1affc0565bc..b59b6b41e54 100644
--- a/src/include/postgres.h
+++ b/src/include/postgres.h
@@ -134,25 +134,6 @@ CharGetDatum(char X)
 	return (Datum) X;
 }
 
-/*
- * DatumGetUInt8
- *		Returns 8-bit unsigned integer value of a datum.
- */
-static inline uint8
-DatumGetUInt8(Datum X)
-{
-	return (uint8) X;
-}
-
-/*
- * UInt8GetDatum
- *		Returns datum representation for an 8-bit unsigned integer.
- */
-static inline Datum
-UInt8GetDatum(uint8 X)
-{
-	return (Datum) X;
-}
 
 /*
  * DatumGetInt16
-- 
2.43.0

