Re: [GENERAL] How do I insert and update into a table of arrays ofcomposite types via a select command?

Поиск
Список
Период
Сортировка
От Celia McInnis
Тема Re: [GENERAL] How do I insert and update into a table of arrays ofcomposite types via a select command?
Дата
Msg-id CAGD6t7+5rbpCd=pT8CzGNyXAVNSY7PB8kEft9znMRK0FSMpmDQ@mail.gmail.com
обсуждение исходный текст
Ответ на Re: [GENERAL] How do I insert and update into a table of arrays of composite types via a select command?  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: [GENERAL] How do I insert and update into a table of arrays ofcomposite types via a select command?
Список pgsql-general
Got it, finally...

insert into t_array select array[row((data_comp).*)::mytype[] from t_composite;

I'm not sure why I need (data_comp).* rather than some of the other things that I tried and failed with...

On Wed, Oct 25, 2017 at 3:47 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Celia McInnis <celia.mcinnis@gmail.com> writes:
> My first question is: HOW do I populate this table, at first with arrays
> containing one element from the t_composite table?
> I have tried various things with no success.

You didn't say what you tried, but I imagine it was something like

regression=# insert into t_array select array[row('a','b','c'),row('d','e','f')];
ERROR:  column "data_array" is of type mytype[] but expression is of type record[]
LINE 1: insert into t_array select array[row('a','b','c'),row('d','e...
                                   ^
HINT:  You will need to rewrite or cast the expression.

Like it says, you need a cast.  You can either cast the array elements
individually:

regression=# insert into t_array select array[row('a','b','c')::mytype,row('d','e','f')::mytype];
INSERT 0 1

or just cast the whole ARRAY[] construct:

regression=# insert into t_array select array[row('a','b','c'),row('d','e','f')]::mytype[];
INSERT 0 1

although I think the latter only works in relatively late-model
Postgres, and it might make parsing a bit slower too.

                        regards, tom lane

В списке pgsql-general по дате отправления:

Предыдущее
От: Celia McInnis
Дата:
Сообщение: Re: [GENERAL] How do I insert and update into a table of arrays ofcomposite types via a select command?
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: [GENERAL] How do I insert and update into a table of arrays ofcomposite types via a select command?