Detect overflows in various numerical aggregate transfns

Here we add overflow checks to guard against overflowing the int8 sum of
all inputs for sum(int2), sum(int4), avg(int2) and avg(int4).  Such an
overflow is very unlikely for the int2 versions, but somewhat possible
to hit for the int4 versions when given billions of large enough inputs.
Since our code and machines are getting faster, and with the advent of
things like columnar table AMs, it's getting more likely that such an
overflow could occur in a real use case.

No guards have been added to the average count.  That would require 2^63
input rows, which is not possible to achieve in a single instance of
PostgreSQL, given other limitations such as 64-bit LSNs.

Fixing this only in master as we can probably get away with that today
due to this still being unlikely for users to ever hit.

Author: Andrei Lepikhov <lepihov@gmail.com>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/1ddc2a6f-4b26-43d9-9f3b-5b5db98a486d@gmail.com
This commit is contained in:
David Rowley committed 2026-10-28 13:25:21 +13:00
1 parent 4a92c8c29b
commit 903ce587e6
1 file changed
+33 -5
+33 -5
View File
@@ -6372,7 +6372,11 @@ int2_sum(PG_FUNCTION_ARGS)
oldsum = PG_GETARG_INT64(0);
/* OK to do the addition. */
newval = oldsum + (int64) PG_GETARG_INT16(1);
if (unlikely(pg_add_s64_overflow(oldsum, (int64) PG_GETARG_INT16(1),
&newval)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
PG_RETURN_INT64(newval);
}
@@ -6398,7 +6402,11 @@ int4_sum(PG_FUNCTION_ARGS)
oldsum = PG_GETARG_INT64(0);
/* OK to do the addition. */
newval = oldsum + (int64) PG_GETARG_INT32(1);
if (unlikely(pg_add_s64_overflow(oldsum, (int64) PG_GETARG_INT32(1),
&newval)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
PG_RETURN_INT64(newval);
}
@@ -6454,6 +6462,7 @@ int2_avg_accum(PG_FUNCTION_ARGS)
ArrayType *transarray;
int16 newval = PG_GETARG_INT16(1);
Int8TransTypeData *transdata;
int64 newsum;
/*
* If we're invoked as an aggregate, we can cheat and modify our first
@@ -6470,8 +6479,14 @@ int2_avg_accum(PG_FUNCTION_ARGS)
elog(ERROR, "expected 2-element int8 array");
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
if (unlikely(pg_add_s64_overflow(transdata->sum, (int64) newval, &newsum)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
transdata->count++;
transdata->sum += newval;
transdata->sum = newsum;
PG_RETURN_ARRAYTYPE_P(transarray);
}
@@ -6482,6 +6497,7 @@ int4_avg_accum(PG_FUNCTION_ARGS)
ArrayType *transarray;
int32 newval = PG_GETARG_INT32(1);
Int8TransTypeData *transdata;
int64 newsum;
/*
* If we're invoked as an aggregate, we can cheat and modify our first
@@ -6498,8 +6514,14 @@ int4_avg_accum(PG_FUNCTION_ARGS)
elog(ERROR, "expected 2-element int8 array");
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
if (unlikely(pg_add_s64_overflow(transdata->sum, (int64) newval, &newsum)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
transdata->count++;
transdata->sum += newval;
transdata->sum = newsum;
PG_RETURN_ARRAYTYPE_P(transarray);
}
@@ -6511,6 +6533,7 @@ int4_avg_combine(PG_FUNCTION_ARGS)
ArrayType *transarray2;
Int8TransTypeData *state1;
Int8TransTypeData *state2;
int64 newsum;
if (!AggCheckCallContext(fcinfo, NULL))
elog(ERROR, "aggregate function called in non-aggregate context");
@@ -6529,8 +6552,13 @@ int4_avg_combine(PG_FUNCTION_ARGS)
state1 = (Int8TransTypeData *) ARR_DATA_PTR(transarray1);
state2 = (Int8TransTypeData *) ARR_DATA_PTR(transarray2);
if (unlikely(pg_add_s64_overflow(state1->sum, state2->sum, &newsum)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
state1->count += state2->count;
state1->sum += state2->sum;
state1->sum = newsum;
PG_RETURN_ARRAYTYPE_P(transarray1);
}