Re: User-defined function with anyrange[] - Mailing list pgsql-general

From Tom Lane
Subject Re: User-defined function with anyrange[]
Date
Msg-id 69895.1530913898@sss.pgh.pa.us
Whole thread Raw
In response to User-defined function with anyrange[]  (Paul A Jungwirth <pj@illuminatedcomputing.com>)
Responses Re: User-defined function with anyrange[]
List pgsql-general
Paul A Jungwirth <pj@illuminatedcomputing.com> writes:
> I want to make an aggregate function range_agg(anyrange) that returns
> anyrange[]. But when I try to define it, Postgres tells me it doesn't
> know what an anyrange[] is. I get this error:

Yeah, there is no such thing as anyrange[], nor arrays over any other
pseudo-type either.

> I also tried taking an anyrange and returning an anyarray, which does
> let me define the function, but upon calling it I get an error. For
> example:

>     paul=# CREATE OR REPLACE FUNCTION range_agg4(anyrange)
>     RETURNS anyarray
>     AS $$
>     BEGIN
>       RETURN ARRAY[$1];
>     END;
>     $$
>     LANGUAGE plpgsql;

The trouble with this is that anyarray and anyrange are both implicitly
related to the "anyelement" pseudo-type, which represents their element
type.  So if anyrange is associated with daterange in a particular
function call, then anyelement is associated with date, and then anyarray
is associated with date[] not daterange[].

I don't think there's any way to get what you want using a single
polymorphic function, at least not without some sort of extension to
the polymorphism rules.  However, you can use overloading to define
several functions of the same name, and just write out one for each
range type you actually need this functionality for.  I haven't really
seen applications that need so many range types that this'd be
intolerable.

            regards, tom lane


pgsql-general by date:

Previous
From: Duarte Carreira
Date:
Subject: Re: correcting tablespaces inside data folder
Next
From: DiasCosta
Date:
Subject: Re: Cloning schemas