Add missing function abs (interval)

From: Isaac Morland <isaac(dot)morland(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Add missing function abs (interval)
Date: 2021-03-29 19:32:56
Message-ID: CAMsGm5cTHQLjfrRj00y6P05uegdn7qBSaXic7u_GMTViPS1A8Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2021-03-29 20:06:45 Re: pg_amcheck contrib application
Previous Message Tomas Vondra 2021-03-29 19:24:19 Re: Merging statistics from children instead of re-sampling everything