1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-21 10:42:50 +03:00

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.
This commit is contained in:
Tom Lane
2007-06-09 15:52:54 +00:00
parent 94dc2d8e3b
commit 6222d866a2

View File

@@ -14,7 +14,7 @@
* Copyright (c) 1998-2005, PostgreSQL Global Development Group * Copyright (c) 1998-2005, PostgreSQL Global Development Group
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.81 2005/01/01 05:43:07 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.81.4.1 2007/06/09 15:52:54 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@@ -29,6 +29,7 @@
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/array.h" #include "utils/array.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/int8.h" #include "utils/int8.h"
@@ -1419,6 +1420,11 @@ numeric_fac(PG_FUNCTION_ARGS)
res = make_result(&const_one); res = make_result(&const_one);
PG_RETURN_NUMERIC(res); 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(&fact);
init_var(&result); init_var(&result);
@@ -1427,6 +1433,9 @@ numeric_fac(PG_FUNCTION_ARGS)
for (num = num - 1; num > 1; num--) 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); int8_to_numericvar(num, &fact);
mul_var(&result, &fact, &result, 0); mul_var(&result, &fact, &result, 0);