Re: How do I insert and update into a table of arrays of composite types via a select command?

From: Celia McInnis <celia(dot)mcinnis(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How do I insert and update into a table of arrays of composite types via a select command?
Date: 2017-10-25 20:59:41
Message-ID: CAGD6t7+1NjbZxp0FuqreXDzFHk=0pirAnYr-dNZB-DVVXPqOOg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Thanks Tom - I saw how to do that for specified data, but I want to select
the elements from a table of composite and don't know how to do that. I
can't do insertions manually of each row separately! So how do I get
material from my table of composite values into my table of array values?

I am running postgresql 9.6.2.

On Wed, Oct 25, 2017 at 3:47 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Celia McInnis <celia(dot)mcinnis(at)gmail(dot)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
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Celia McInnis 2017-10-25 21:16:09 Re: How do I insert and update into a table of arrays of composite types via a select command?
Previous Message Rob Sargent 2017-10-25 20:59:27 query not scaling