From 6c3e93abb484a25c49ac7ea7ee2fd2e597aedbe4 Mon Sep 17 00:00:00 2001 From: Ewan Young Date: Mon, 31 Aug 2026 23:55:19 +0800 Subject: [PATCH 2/2] Drop redundant unlikely() around overflow checks that lead to ereport(ERROR) When the failure branch of a pg_{add,sub,mul}_s{16,32,64}_overflow() call ends in ereport(ERROR), the branch is already known cold: a constant elevel >= ERROR routes the call through errstart_cold(), which carries pg_attribute_cold. The extra unlikely() adds nothing, so drop it from such call sites in the files touched by the previous commit. Discussion: https://postgr.es/m/CAON2xHO4tTFiow2KKLbpiOEJNL4Th5p1QVTm4mSdEU+KW1rrfQ@mail.gmail.com --- src/backend/utils/adt/cash.c | 6 +++--- src/backend/utils/adt/int.c | 28 ++++++++++++++-------------- src/backend/utils/adt/int8.c | 36 ++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index 0820187b8e6..badc95c2fa7 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -93,7 +93,7 @@ cash_pl_cash(Cash c1, Cash c2) { Cash res; - if (unlikely(pg_add_s64_overflow(c1, c2, &res))) + if (pg_add_s64_overflow(c1, c2, &res)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("money out of range"))); @@ -106,7 +106,7 @@ cash_mi_cash(Cash c1, Cash c2) { Cash res; - if (unlikely(pg_sub_s64_overflow(c1, c2, &res))) + if (pg_sub_s64_overflow(c1, c2, &res)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("money out of range"))); @@ -145,7 +145,7 @@ cash_mul_int64(Cash c, int64 i) { Cash res; - if (unlikely(pg_mul_s64_overflow(c, i, &res))) + if (pg_mul_s64_overflow(c, i, &res)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("money out of range"))); diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 8e2fdbe2be0..fc4f77a7476 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -824,7 +824,7 @@ int4pl(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_add_s32_overflow(arg1, arg2, &result))) + if (pg_add_s32_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -838,7 +838,7 @@ int4mi(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_sub_s32_overflow(arg1, arg2, &result))) + if (pg_sub_s32_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -852,7 +852,7 @@ int4mul(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result))) + if (pg_mul_s32_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -903,7 +903,7 @@ int4inc(PG_FUNCTION_ARGS) int32 arg = PG_GETARG_INT32(0); int32 result; - if (unlikely(pg_add_s32_overflow(arg, 1, &result))) + if (pg_add_s32_overflow(arg, 1, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -939,7 +939,7 @@ int2pl(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int16 result; - if (unlikely(pg_add_s16_overflow(arg1, arg2, &result))) + if (pg_add_s16_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -953,7 +953,7 @@ int2mi(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int16 result; - if (unlikely(pg_sub_s16_overflow(arg1, arg2, &result))) + if (pg_sub_s16_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -967,7 +967,7 @@ int2mul(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int16 result; - if (unlikely(pg_mul_s16_overflow(arg1, arg2, &result))) + if (pg_mul_s16_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("smallint out of range"))); @@ -1020,7 +1020,7 @@ int24pl(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_add_s32_overflow((int32) arg1, arg2, &result))) + if (pg_add_s32_overflow((int32) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1034,7 +1034,7 @@ int24mi(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_sub_s32_overflow((int32) arg1, arg2, &result))) + if (pg_sub_s32_overflow((int32) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1048,7 +1048,7 @@ int24mul(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int32 result; - if (unlikely(pg_mul_s32_overflow((int32) arg1, arg2, &result))) + if (pg_mul_s32_overflow((int32) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1081,7 +1081,7 @@ int42pl(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int32 result; - if (unlikely(pg_add_s32_overflow(arg1, (int32) arg2, &result))) + if (pg_add_s32_overflow(arg1, (int32) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1095,7 +1095,7 @@ int42mi(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int32 result; - if (unlikely(pg_sub_s32_overflow(arg1, (int32) arg2, &result))) + if (pg_sub_s32_overflow(arg1, (int32) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1109,7 +1109,7 @@ int42mul(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int32 result; - if (unlikely(pg_mul_s32_overflow(arg1, (int32) arg2, &result))) + if (pg_mul_s32_overflow(arg1, (int32) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); @@ -1362,7 +1362,7 @@ int4lcm(PG_FUNCTION_ARGS) gcd = int4gcd_internal(arg1, arg2); arg1 = arg1 / gcd; - if (unlikely(pg_mul_s32_overflow(arg1, arg2, &result))) + if (pg_mul_s32_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("integer out of range"))); diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 5a59de0f5c4..64b46804622 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -471,7 +471,7 @@ int8pl(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_add_s64_overflow(arg1, arg2, &result))) + if (pg_add_s64_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -485,7 +485,7 @@ int8mi(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_sub_s64_overflow(arg1, arg2, &result))) + if (pg_sub_s64_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -499,7 +499,7 @@ int8mul(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result))) + if (pg_mul_s64_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -709,7 +709,7 @@ int8lcm(PG_FUNCTION_ARGS) gcd = int8gcd_internal(arg1, arg2); arg1 = arg1 / gcd; - if (unlikely(pg_mul_s64_overflow(arg1, arg2, &result))) + if (pg_mul_s64_overflow(arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -729,7 +729,7 @@ int8inc(PG_FUNCTION_ARGS) int64 arg = PG_GETARG_INT64(0); int64 result; - if (unlikely(pg_add_s64_overflow(arg, 1, &result))) + if (pg_add_s64_overflow(arg, 1, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -743,7 +743,7 @@ int8dec(PG_FUNCTION_ARGS) int64 arg = PG_GETARG_INT64(0); int64 result; - if (unlikely(pg_sub_s64_overflow(arg, 1, &result))) + if (pg_sub_s64_overflow(arg, 1, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -931,7 +931,7 @@ int84pl(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int64 result; - if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_add_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -945,7 +945,7 @@ int84mi(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int64 result; - if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_sub_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -959,7 +959,7 @@ int84mul(PG_FUNCTION_ARGS) int32 arg2 = PG_GETARG_INT32(1); int64 result; - if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_mul_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1011,7 +1011,7 @@ int48pl(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result))) + if (pg_add_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1025,7 +1025,7 @@ int48mi(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result))) + if (pg_sub_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1039,7 +1039,7 @@ int48mul(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result))) + if (pg_mul_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1072,7 +1072,7 @@ int82pl(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int64 result; - if (unlikely(pg_add_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_add_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1086,7 +1086,7 @@ int82mi(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int64 result; - if (unlikely(pg_sub_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_sub_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1100,7 +1100,7 @@ int82mul(PG_FUNCTION_ARGS) int16 arg2 = PG_GETARG_INT16(1); int64 result; - if (unlikely(pg_mul_s64_overflow(arg1, (int64) arg2, &result))) + if (pg_mul_s64_overflow(arg1, (int64) arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1152,7 +1152,7 @@ int28pl(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_add_s64_overflow((int64) arg1, arg2, &result))) + if (pg_add_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1166,7 +1166,7 @@ int28mi(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_sub_s64_overflow((int64) arg1, arg2, &result))) + if (pg_sub_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); @@ -1180,7 +1180,7 @@ int28mul(PG_FUNCTION_ARGS) int64 arg2 = PG_GETARG_INT64(1); int64 result; - if (unlikely(pg_mul_s64_overflow((int64) arg1, arg2, &result))) + if (pg_mul_s64_overflow((int64) arg1, arg2, &result)) ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("bigint out of range"))); -- 2.47.3