Re: function with different return type depending on parameter?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: James Harper <james(dot)harper(at)bendigoit(dot)com(dot)au>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: function with different return type depending on parameter?
Date: 2014-02-12 15:24:23
Message-ID: 24654.1392218663@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

James Harper <james(dot)harper(at)bendigoit(dot)com(dot)au> writes:
> is it possible to have a function that can return a different type
> depending on the parameters?

The data type of any expression (including a function call) has to be
determinable at parse time, so no you can't just randomly return a
run-time-determined data type.

However, have you looked at the "polymorphic functions" feature?
You can declare a function as returning the same data type that
one of its inputs has. This seems to cover most of the cases
that are useful in practice.

http://www.postgresql.org/docs/9.3/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC
http://www.postgresql.org/docs/9.3/static/xfunc-sql.html#AEN52916

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2014-02-12 16:09:48 Re: pgsql and asciidoc output
Previous Message Tom Lane 2014-02-12 15:15:25 Re: [GENERAL]