Allow numeric_fac() to be interrupted, since it can take quite a while for
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 9 Jun 2007 15:52:54 +0000 (15:52 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 9 Jun 2007 15:52:54 +0000 (15:52 +0000)
large inputs.  Also cause it to error out immediately if the result will
overflow, instead of grinding through a lot of calculation first.
Per gripe from Jim Nasby.

src/backend/utils/adt/numeric.c

index 5295e66b214b489988d60d70742697d54e685573..5ac306209a47ae048273d8869409b683e2b45d4b 100644 (file)
@@ -29,6 +29,7 @@
 #include "access/hash.h"
 #include "catalog/pg_type.h"
 #include "libpq/pqformat.h"
+#include "miscadmin.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/int8.h"
@@ -1560,6 +1561,11 @@ numeric_fac(PG_FUNCTION_ARGS)
                res = make_result(&const_one);
                PG_RETURN_NUMERIC(res);
        }
+       /* Fail immediately if the result would overflow */
+       if (num > 32177)
+               ereport(ERROR,
+                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                                errmsg("value overflows numeric format")));
 
        init_var(&fact);
        init_var(&result);
@@ -1568,6 +1574,9 @@ numeric_fac(PG_FUNCTION_ARGS)
 
        for (num = num - 1; num > 1; num--)
        {
+               /* this loop can take awhile, so allow it to be interrupted */
+               CHECK_FOR_INTERRUPTS();
+
                int8_to_numericvar(num, &fact);
 
                mul_var(&result, &fact, &result, 0);