> CREATE OR REPLACE FUNCTION null_safe_cmp (ANYELEMENT, ANYELEMENT)
> RETURNS INTEGER IMMUTABLE
> LANGUAGE SQL AS $$
> SELECT CASE
> WHEN NOT ($1 IS DISTINCT FROM $2) THEN 1
> ELSE 0
> END;
> $$;
Even cooler:
CREATE OR REPLACE FUNCTION null_safe_cmp(anyelement, anyelement)
RETURNS integer AS ' SELECT (NOT ($1 IS DISTINCT FROM $2))::integer
' IMMUTABLE LANGUAGE SQL;