Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jSTCY-0003x8-WA for pgsql-sql@arkaria.postgresql.org; Sat, 25 Apr 2020 22:23:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jSTCV-0003yR-Jf for pgsql-sql@arkaria.postgresql.org; Sat, 25 Apr 2020 22:23:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jSTCV-0003yJ-DZ for pgsql-sql@lists.postgresql.org; Sat, 25 Apr 2020 22:23:07 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jSTCP-0006fD-1T for pgsql-sql@lists.postgresql.org; Sat, 25 Apr 2020 22:23:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 03PMMxPM016635; Sat, 25 Apr 2020 18:22:59 -0400 From: Tom Lane To: Maxime FRYSOU cc: pgsql-sql@lists.postgresql.org Subject: Re: insert in an array of composite type In-reply-to: References: Comments: In-reply-to Maxime FRYSOU message dated "Sat, 25 Apr 2020 23:45:02 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16633.1587853379.1@sss.pgh.pa.us> Date: Sat, 25 Apr 2020 18:22:59 -0400 Message-ID: <16634.1587853379@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Maxime FRYSOU writes: > Code speaks louder than words, so ...in order to abstract most of the > complexity, and to focus on the syntax, the products table is obviously not > representative of the real one. My goal here is to make a "simple" insert. > CREATE TYPE RGB AS (R VARCHAR(5), G VARCHAR(5), B VARCHAR(5)); > CREATE TYPE color AS (rgb RGB, label VARCHAR(50)); > CREATE TABLE products (index SERIAL PRIMARY KEY, colors color []); > And this is where it's not working ... > INSERT INTO products (colors) > VALUES > ( > '{ (("18", "15", "55"), "BLACK" )', > '("137", "231", "129"), "GREEN" )}' :: color [] > ) Yeah, you'd need to apply the quoting rules for arrays over those for (two levels of) records, and you didn't. TBH, the easiest way to deal with that is not to. You can build the structures at the SQL level instead: INSERT INTO products (colors) VALUES( array[ row(row('18','15','55'), 'BLACK')::color, row(row('137','231','129'), 'GREEN')::color ] ); If you really want to do it the hard way, one valid representation is INSERT INTO products (colors) VALUES ( '{"(\"(18,15,55)\",BLACK)","(\"(137,231,129)\",GREEN)"}'::color[] ); regards, tom lane