From 51e40d5a0ab1e41df990d86575a1734e9954f94f Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Thu, 9 Oct 2025 16:38:37 +0800
Subject: [PATCH v8 09/20] error safe for casting bigint to other types per
 pg_cast

select castsource::regtype, casttarget::regtype, castfunc,
castcontext,castmethod, pp.prosrc, pp.proname from pg_cast pc join pg_proc pp on
pp.oid = pc.castfunc and pc.castfunc > 0
and castsource::regtype = 'bigint'::regtype
order by castsource::regtype;

 castsource |    casttarget    | castfunc | castcontext | castmethod |    prosrc    | proname
------------+------------------+----------+-------------+------------+--------------+---------
 bigint     | smallint         |      714 | a           | f          | int82        | int2
 bigint     | integer          |      480 | a           | f          | int84        | int4
 bigint     | real             |      652 | i           | f          | i8tof        | float4
 bigint     | double precision |      482 | i           | f          | i8tod        | float8
 bigint     | numeric          |     1781 | i           | f          | int8_numeric | numeric
 bigint     | money            |     3812 | a           | f          | int8_cash    | money
 bigint     | oid              |     1287 | i           | f          | i8tooid      | oid
 bigint     | regproc          |     1287 | i           | f          | i8tooid      | oid
 bigint     | regprocedure     |     1287 | i           | f          | i8tooid      | oid
 bigint     | regoper          |     1287 | i           | f          | i8tooid      | oid
 bigint     | regoperator      |     1287 | i           | f          | i8tooid      | oid
 bigint     | regclass         |     1287 | i           | f          | i8tooid      | oid
 bigint     | regcollation     |     1287 | i           | f          | i8tooid      | oid
 bigint     | regtype          |     1287 | i           | f          | i8tooid      | oid
 bigint     | regconfig        |     1287 | i           | f          | i8tooid      | oid
 bigint     | regdictionary    |     1287 | i           | f          | i8tooid      | oid
 bigint     | regrole          |     1287 | i           | f          | i8tooid      | oid
 bigint     | regnamespace     |     1287 | i           | f          | i8tooid      | oid
 bigint     | regdatabase      |     1287 | i           | f          | i8tooid      | oid
 bigint     | bytea            |     6369 | e           | f          | int8_bytea   | bytea
 bigint     | bit              |     2075 | e           | f          | bitfromint8  | bit
(21 rows)

already error safe: i8tof, i8tod, int8_numeric, int8_bytea, bitfromint8
discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
---
 src/backend/utils/adt/int8.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c
index bdea490202a..3b2d100be92 100644
--- a/src/backend/utils/adt/int8.c
+++ b/src/backend/utils/adt/int8.c
@@ -1204,10 +1204,14 @@ int84(PG_FUNCTION_ARGS)
 	int64		arg = PG_GETARG_INT64(0);
 
 	if (unlikely(arg < PG_INT32_MIN) || unlikely(arg > PG_INT32_MAX))
-		ereport(ERROR,
+	{
+		errsave(fcinfo->context,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
 				 errmsg("integer out of range")));
 
+		PG_RETURN_NULL();
+	}
+
 	PG_RETURN_INT32((int32) arg);
 }
 
@@ -1225,10 +1229,14 @@ int82(PG_FUNCTION_ARGS)
 	int64		arg = PG_GETARG_INT64(0);
 
 	if (unlikely(arg < PG_INT16_MIN) || unlikely(arg > PG_INT16_MAX))
-		ereport(ERROR,
+	{
+		errsave(fcinfo->context,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
 				 errmsg("smallint out of range")));
 
+		PG_RETURN_NULL();
+	}
+
 	PG_RETURN_INT16((int16) arg);
 }
 
@@ -1308,10 +1316,14 @@ i8tooid(PG_FUNCTION_ARGS)
 	int64		arg = PG_GETARG_INT64(0);
 
 	if (unlikely(arg < 0) || unlikely(arg > PG_UINT32_MAX))
-		ereport(ERROR,
+	{
+		errsave(fcinfo->context,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
 				 errmsg("OID out of range")));
 
+		PG_RETURN_NULL();
+	}
+
 	PG_RETURN_OID((Oid) arg);
 }
 
-- 
2.34.1

