Re: Set Returning Functions and array_agg()

From: Stephen Scheck <singularsyntax(at)gmail(dot)com>
To: depesz(at)depesz(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Set Returning Functions and array_agg()
Date: 2013-04-24 21:26:31
Message-ID: CAKjnHz2vfH+h=19mQk7dWTDmDe=rV_N3jEefhqVfaYNixcfXQw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Possibly due to my lack of thorough SQL understanding. Perhaps there's a
better way of doing what I'm ultimately trying to accomplish, but still the
question remains - why does this work:

pg_dev=# select unnest(array[1,2,3]);
unnest
--------
1
2
3
(3 rows)

But not this:

pg_dev=# select array_agg(unnest(array[1,2,3]));
ERROR: set-valued function called in context that cannot accept a set

The solution to the problem is actually of less interest right now then in
understanding what's going on in the two statements above. It seems a bit
inconsistent to me. If an aggregate function cannot handle rows generated
in the columns-part of the statement, then why is a single-column row(s)
result acceptable in the first statement?

On Wed, Apr 24, 2013 at 1:29 PM, hubert depesz lubaczewski <
depesz(at)depesz(dot)com> wrote:

> On Wed, Apr 24, 2013 at 12:48:44PM -0700, Stephen Scheck wrote:
> > I have a UDF (written in C) that returns SETOF RECORD of an anonymous
> > record type
> > (defined via OUT parameters). I'm trying to use array_agg() to transform
> > its output to
> > an array:
> > pg_dev=# SELECT array_agg((my_setof_record_returning_func()).col1);
> > ERROR: set-valued function called in context that cannot accept a set
>
> Is there any reason why you're not using normal syntax:
> select array_agg(col1) from my_setof_record_returning_func();
> ?
>
> Best regards,
>
> depesz
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Scheck 2013-04-24 21:44:24 Re: Set Returning Functions and array_agg()
Previous Message hubert depesz lubaczewski 2013-04-24 20:29:25 Re: Set Returning Functions and array_agg()