Re: unnest array of row type - Mailing list pgsql-general

From Pavel Stehule
Subject Re: unnest array of row type
Date
Msg-id CAFj8pRBmQbEAG6h_SRWkK=fjf+mbfdoSb4i7n99FH4UN0g=7gA@mail.gmail.com
Whole thread Raw
In response to unnest array of row type  (seiliki@so-net.net.tw)
Responses Re: unnest array of row type  (Merlin Moncure <mmoncure@gmail.com>)
List pgsql-general
2012/1/12  <seiliki@so-net.net.tw>:
> Hi!
>
> CREATE TYPE my_row_type AS (c1 SMALLINT,c2 TEXT);
>
> CREATE FUNCTION tmp_get_c1(my_row_type) RETURNS SMALLINT AS 'SELECT $1.c1' LANGUAGE SQL;
>
> CREATE FUNCTION tmp_get_c2(my_row_type) RETURNS TEXT AS 'SELECT $1.c2' LANGUAGE SQL;
>
> CREATE TABLE test (c1 SMALLINT,c2 TEXT,c3 TEXT);
>
> INSERT INTO test
> SELECT tmp_get_c1(r),tmp_get_c2(r),'x'
> FROM (
>        SELECT UNNEST(ARRAY[ROW(1::SMALLINT,'a'::TEXT),ROW(2::SMALLINT,'b'::TEXT)]) AS r
> ) s;
>
> I get error "record type has not been registered" from the previous INSERT.
>
> I have tested version 9.1 and have confirmed that PL/PGSQL "FOREACH ... IN ARRAY ... LOOP ... END LOOP;" does the
job.Because I wonder "INSERT INTO ... SELECT ... FROM" being faster than "LOOP ... END LOOP;", I raise this question. 
>

insert into test select tmp_get_c1(r), tmp_get_c2(r), 'x' from (SELECT
(x,y)::my_row_type as r from
UNNEST(ARRAY[ROW(1::SMALLINT,'a'::TEXT),ROW(2::SMALLINT,'b'::TEXT)])
AS (x smallint, y text)) x;

regards

Pavel Stehule



> Thank you in advance!
> CN
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: seiliki@so-net.net.tw
Date:
Subject: unnest array of row type
Next
From: Merlin Moncure
Date:
Subject: Re: unnest array of row type