From a2da77cdb4661826482ebf2ddba1f953bc74afe4 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 6 Apr 2021 07:17:13 +0200 Subject: Change return type of EXTRACT to numeric The previous implementation of EXTRACT mapped internally to date_part(), which returned type double precision (since it was implemented long before the numeric type existed). This can lead to imprecise output in some cases, so returning numeric would be preferrable. Changing the return type of an existing function is a bit risky, so instead we do the following: We implement a new set of functions, which are now called "extract", in parallel to the existing date_part functions. They work the same way internally but use numeric instead of float8. The EXTRACT construct is now mapped by the parser to these new extract functions. That way, dumps of views etc. from old versions (which would use date_part) continue to work unchanged, but new uses will map to the new extract functions. Additionally, the reverse compilation of EXTRACT now reproduces the original syntax, using the new mechanism introduced in 40c24bfef92530bd846e111c1742c2a54441c62c. The following minor changes of behavior result from the new implementation: - The column name from an isolated EXTRACT call is now "extract" instead of "date_part". - Extract from date now rejects inappropriate field names such as HOUR. It was previously mapped internally to extract from timestamp, so it would silently accept everything appropriate for timestamp. - Return values when extracting fields with possibly fractional values, such as second and epoch, now have the full scale that the value has internally (so, for example, '1.000000' instead of just '1'). Reported-by: Petr Fedorov Reviewed-by: Tom Lane Discussion: https://www.postgresql.org/message-id/flat/42b73d2d-da12-ba9f-570a-420e0cce19d9@phystech.edu --- doc/src/sgml/func.sgml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 9fcee749105..c6a45d9e55c 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -8872,7 +8872,7 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); extract extract ( field from timestamp ) - double precision + numeric Get timestamp subfield; see @@ -8886,7 +8886,7 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); extract ( field from interval ) - double precision + numeric Get interval subfield; see @@ -9401,7 +9401,7 @@ EXTRACT(field FROM source) well.) field is an identifier or string that selects what field to extract from the source value. The extract function returns values of type - double precision. + numeric. The following are valid field names: @@ -9825,6 +9825,10 @@ date_part('field', source) be a string value, not a name. The valid field names for date_part are the same as for extract. + For historical reasons, the date_part function + returns values of type double precision. This can result in + a loss of precision in certain uses. Using extract + is recommended instead. -- cgit v1.2.3