From: Tom Lane <tgl@sss.pgh.pa.us>
To: Paul A Jungwirth <pj@illuminatedcomputing.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: User-defined function with anyrange[]
Date: Fri, 06 Jul 2018 17:51:38 -0400
Message-ID: <69895.1530913898@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+renyVOjb4xQZGjdCnA54-1nzVSY+47-h4nkM-EP5J=1z=b9w@mail.gmail.com>
References: <CA+renyVOjb4xQZGjdCnA54-1nzVSY+47-h4nkM-EP5J=1z=b9w@mail.gmail.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, pj@illuminatedcomputing.com
Subject: Re: User-defined function with anyrange[]
In-Reply-To: <69895.1530913898@sss.pgh.pa.us>
References: <CA+renyVOjb4xQZGjdCnA54-1nzVSY+47-h4nkM-EP5J=1z=b9w@mail.gmail.com> <69895.1530913898@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox