postgresql/src/backend
Dean Rasheed 39b0bda670 Fix corner-case errors and loss of precision in numeric_power().
This fixes a couple of related problems that arise when raising
numbers to very large powers.

Firstly, when raising a negative number to a very large integer power,
the result should be well-defined, but the previous code would only
cope if the exponent was small enough to go through power_var_int().
Otherwise it would throw an internal error, attempting to take the
logarithm of a negative number. Fix this by adding suitable handling
to the general case in power_var() to cope with negative bases,
checking for integer powers there.

Next, when raising a (positive or negative) number whose absolute
value is slightly less than 1 to a very large power, the result should
approach zero as the power is increased. However, in some cases, for
sufficiently large powers, this would lose all precision and return 1
instead of 0. This was due to the way that the local_rscale was being
calculated for the final full-precision calculation:

  local_rscale = rscale + (int) val - ln_dweight + 8

The first two terms on the right hand side are meant to give the
number of significant digits required in the result ("val" being the
estimated result weight). However, this failed to account for the fact
that rscale is clipped to a maximum of NUMERIC_MAX_DISPLAY_SCALE
(1000), and the result weight might be less then -1000, causing their
sum to be negative, leading to a loss of precision. Fix this by
forcing the number of significant digits calculated to be nonnegative.
It's OK for it to be zero (when the result weight is less than -1000),
since the local_rscale value then includes a few extra digits to
ensure an accurate result.

Finally, add additional underflow checks to exp_var() and power_var(),
so that they consistently return zero for cases like this where the
result is indistinguishable from zero. Some paths through this code
already returned zero in such cases, but others were throwing overflow
errors.

Dean Rasheed, reviewed by Yugo Nagata.

Discussion: http://postgr.es/m/CAEZATCW6Dvq7+3wN3tt5jLj-FyOcUgT5xNoOqce5=6Su0bCR0w@mail.gmail.com
2021-07-31 11:29:50 +01:00
..
access Update minimum recovery point on truncation during WAL replay of abort record. 2021-07-29 01:35:10 +09:00
bootstrap Revert "Skip WAL for new relfilenodes, under wal_level=minimal." 2020-03-22 09:24:14 -07:00
catalog Make index_set_state_flags() transactional 2021-06-28 10:43:08 +09:00
commands Don't allow to set replication slot_name as ''. 2021-07-19 11:32:08 +05:30
executor Fix mishandling of resjunk columns in ON CONFLICT ... UPDATE tlists. 2021-05-10 11:02:30 -04:00
foreign Abstract logic to allow for multiple kinds of child rels. 2017-04-03 22:41:31 -04:00
lib Rename rbtree.c functions to use "rbt" prefix not "rb" prefix. 2018-11-06 13:25:24 -05:00
libpq Don't assume GSSAPI result strings are null-terminated. 2021-06-23 14:01:32 -04:00
main Install Windows crash dump handler before all else. 2017-11-12 14:31:04 -08:00
nodes Ensure that expandTableLikeClause() re-examines the same table. 2020-12-01 14:02:28 -05:00
optimizer Silence another gcc 11 warning. 2021-01-28 17:18:42 -05:00
parser Reject SELECT ... GROUP BY GROUPING SETS (()) FOR UPDATE. 2021-06-01 11:12:56 -04:00
po Translation updates 2021-05-10 14:27:15 +02:00
port Fix race condition in our Windows signal emulation. 2019-12-09 15:03:52 -05:00
postmaster Be more careful about barriers when releasing BackgroundWorkerSlots. 2021-05-15 12:21:06 -04:00
regex Fix another ancient bug in parsing of BRE-mode regular expressions. 2021-02-18 22:38:55 -05:00
replication Don't try to print data type names in slot_store_error_callback(). 2021-07-02 16:04:54 -04:00
rewrite Reject cases where a query in WITH rewrites to just NOTIFY. 2021-07-09 11:02:26 -04:00
snowball Initial pgindent run with pg_bsd_indent version 2.0. 2017-06-21 14:39:04 -04:00
statistics Use correct spelling of statistics kind 2021-03-23 04:54:34 +01:00
storage Fix check for conflicting session- vs transaction-level locks. 2021-07-24 18:35:52 -04:00
tcop Refactor CHECK_FOR_INTERRUPTS() to add flexibility. 2021-05-14 12:54:26 -04:00
tsearch Don't leak compiled regex(es) when an ispell cache entry is dropped. 2021-03-18 21:44:43 -04:00
utils Fix corner-case errors and loss of precision in numeric_power(). 2021-07-31 11:29:50 +01:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Add ICU_CFLAGS to global CPPFLAGS 2017-06-12 15:57:22 -04:00
Makefile Fix make rules that generate multiple output files. 2018-03-23 13:45:38 -04:00
nls.mk Translation updates 2018-05-07 11:59:42 -04:00