title: Can I write a custom function to handle different numeric types without multiple function definitions?
body: <p>As an example, suppose I'd like to write a custom function in postgres that safely divides two numbers -- i.e. it should check whether either of the arguments are null and whether the divisor is zero.  It should handle these error conditions gracefully, and otherwise return the expected quotient.</p>
  
  <p>The current code works well as long as the two arguments are of the same numeric type (e.g. both integers, both numeric, etc.)  </p>
  
  <pre><code>CREATE OR REPLACE FUNCTION safe_divide(anyelement, anyelement) RETURNS numeric AS $$
    SELECT CASE 
       WHEN $1 IS NULL OR $2 IS NULL OR $2 = 0 THEN NULL 
       ELSE $1::numeric / $2::numeric
       END;
  $$ LANGUAGE SQL;
  </code></pre>
  
  <p>My question: is there a way to write this function so that I can supply different numeric types?  </p>
  
  <p>I would like to avoid:</p>
  
  <ul>
  <li><p>Needing to explicitly cast the arguments when calling the function (e.g. <code>safe_divide(x::numeric, y::numeric)</code>)</p></li>
  <li><p>Needing to define the function for every possible datatype</p></li>
  </ul>
  
  <p>Thanks!</p>