aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/numeric.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2007-06-09 15:52:30 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2007-06-09 15:52:30 +0000
commite17e40f783da252934b19f43981084b3b1687d84 (patch)
tree33ca7390d43b3f93cc3579934d2b37672ff81069 /src/backend/utils/adt/numeric.c
parenta4d58727191419b89126f06f23237986cae966fc (diff)
downloadpostgresql-e17e40f783da252934b19f43981084b3b1687d84.tar.gz
postgresql-e17e40f783da252934b19f43981084b3b1687d84.zip
Allow numeric_fac() to be interrupted, since it can take quite a while for
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.
Diffstat (limited to 'src/backend/utils/adt/numeric.c')
-rw-r--r--src/backend/utils/adt/numeric.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index aa585f4267b..95309cbff47 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -14,7 +14,7 @@
* Copyright (c) 1998-2007, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.103 2007/06/05 21:31:06 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.104 2007/06/09 15:52:30 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -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);