Add missing function abs (interval) - Mailing list pgsql-hackers

From Isaac Morland
Subject Add missing function abs (interval)
Date
Msg-id CAMsGm5cTHQLjfrRj00y6P05uegdn7qBSaXic7u_GMTViPS1A8Q@mail.gmail.com
Whole thread Raw
Responses Re: Add missing function abs (interval)  (John Naylor <john.naylor@enterprisedb.com>)
List pgsql-hackers
On a newly set up system there are 7 types with a unary minus operator defined, but only 6 of them have an abs function:

postgres=# \df abs
                         List of functions
   Schema   | Name | Result data type | Argument data types | Type 
------------+------+------------------+---------------------+------
 pg_catalog | abs  | bigint           | bigint              | func
 pg_catalog | abs  | double precision | double precision    | func
 pg_catalog | abs  | integer          | integer             | func
 pg_catalog | abs  | numeric          | numeric             | func
 pg_catalog | abs  | real             | real                | func
 pg_catalog | abs  | smallint         | smallint            | func
(6 rows)

I now have the following definition in my database:

CREATE OR REPLACE FUNCTION abs (
    p                           interval
) RETURNS interval
    LANGUAGE SQL IMMUTABLE STRICT
    SET search_path FROM CURRENT
AS $$
SELECT GREATEST (p, -p)
$$;
COMMENT ON FUNCTION abs (interval) IS 'absolute value';

Would a patch to add a function with this behaviour to the initial database be welcome?

If so, should I implement it essentially like the above, or as an internal function? I've noticed that even when it seems like it might be reasonable to implement a built-in function as an SQL function they tend to be internal.

pgsql-hackers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: Merging statistics from children instead of re-sampling everything
Next
From: Robert Haas
Date:
Subject: Re: pg_amcheck contrib application