Re: User-defined function with anyrange[]

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: User-defined function with anyrange[]
Date: 2018-07-06 21:51:38
Message-ID: 69895.1530913898@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Paul A Jungwirth <pj(at)illuminatedcomputing(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message DiasCosta 2018-07-07 08:32:56 Re: Cloning schemas
Previous Message Duarte Carreira 2018-07-06 17:30:18 Re: correcting tablespaces inside data folder