-- example: clamp(subject, min, max)
CREATE FUNCTION clamp(integer, integer, integer) RETURNS integer
AS 'select GREATEST($2, LEAST($3, $1));'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
-- example: clamp_above(subject, max)
CREATE FUNCTION clamp_above(integer, integer) RETURNS integer
AS 'select LEAST($1, $2);'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
-- example: clamp_below(subject, min)
CREATE FUNCTION clamp_below(integer, integer) RETURNS integer
AS 'select GREATEST($1, $2);'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;