Re: array_to_set functions - Mailing list pgsql-general

From Merlin Moncure
Subject Re: array_to_set functions
Date
Msg-id b42b73150708050748l4917750cv55d853b82d5ef343@mail.gmail.com
Whole thread Raw
In response to Re: array_to_set functions  (Guy Fraser <guy@incentre.net>)
Responses Re: array_to_set functions  (Decibel! <decibel@decibel.org>)
List pgsql-general
On 8/3/07, Guy Fraser <guy@incentre.net> wrote:
> On Wed, 2007-08-01 at 07:14 +0530, Merlin Moncure wrote:
> > On 8/1/07, Decibel! <decibel@decibel.org> wrote:
> > > David Fetter and I just came up with these, perhaps others will find
> > > them useful:
> > >
> > > CREATE OR REPLACE FUNCTION array_to_set(anyarray, int) RETURNS SETOF anyelement LANGUAGE SQL AS $$
> > >     SELECT $1[i] from generate_series(array_lower($1, $2), array_upper($1, $2)) i
> > > $$;
> > > CREATE OR REPLACE FUNCTION array_to_set(anyarray) RETURNS SETOF anyelement LANGUAGE SQL AS $$
> > >     SELECT array_to_set($1, 1)
> > > $$;
> >
> > very nice, although IMO there is a strong justification for these
> > functions to be in core and written in C for efficiency (along with
> > array_accum, which I have hand burn from copying and pasting out of
> > the documentation).
> >
> > merlin
> >
> Excellent timing guys. :^)
>
> I was trying to build a function to list the items of an array, but
> ran into problems and was going to post what I had been working on.
>
> Your functions work great.
>
> In case you don't have the function to generate an array from a set
> here is one I have been using :
>
>
> CREATE AGGREGATE array_accum (
>     BASETYPE = anyelement,
>     SFUNC = array_append,
>     STYPE = anyarray,
>     INITCOND = '{}'
> );

I think that's what just about everyone uses.  Unfortunately the
reverse of the function (array_to_set above) AFAIK does not map
directly to the C array API.

merlin

pgsql-general by date:

Previous
From: "Merlin Moncure"
Date:
Subject: Re: WAL Queries
Next
From: Perry Smith
Date:
Subject: pg_dump of only the structure from a client such as ruby