Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5Rsw-0000iV-Tp for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 08:10:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f5Rsv-0004bR-TQ for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 08:10:41 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5Niw-0008Qt-Ef for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 03:44:06 +0000 Received: from mail.digi-texx.vn ([116.193.72.250]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5Nil-0003ZR-K4 for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 03:44:03 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.digi-texx.vn (Postfix) with ESMTP id 1F36182C4997; Mon, 9 Apr 2018 10:46:36 +0700 (+07) Received: from mail.digi-texx.vn ([127.0.0.1]) by localhost (mx01.digi-texx.vn [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id DreIL83u9t1T; Mon, 9 Apr 2018 10:46:32 +0700 (+07) Received: from localhost (localhost [127.0.0.1]) by mail.digi-texx.vn (Postfix) with ESMTP id 60B1082C4998; Mon, 9 Apr 2018 10:46:32 +0700 (+07) X-Virus-Scanned: amavisd-new at mx01.digi-texx.vn Received: from mail.digi-texx.vn ([127.0.0.1]) by localhost (mx01.digi-texx.vn [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id msu__Was9n7d; Mon, 9 Apr 2018 10:46:32 +0700 (+07) Received: from zimbra-mail1.digi-texx.vn (zimbra-mail1.digi-texx.vn [10.1.1.150]) by mail.digi-texx.vn (Postfix) with ESMTP id 3E20D82C4997; Mon, 9 Apr 2018 10:46:32 +0700 (+07) Date: Mon, 9 Apr 2018 10:44:07 +0700 (ICT) From: =?utf-8?B?xJDhu5cgTmfhu41jIFRyw60gQ8aw4budbmc=?= Reply-To: =?utf-8?B?xJDhu5cgTmfhu41jIFRyw60gQ8aw4budbmc=?= To: Adrian Klaver Cc: pgsql-general Message-ID: <688939859.13457181.1523245447673.JavaMail.zimbra@digi-texx.vn> In-Reply-To: References: <152647427.13167958.1523097853639.JavaMail.zimbra@digi-texx.vn> Subject: Re: Conflict between JSON_AGG and COPY MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_13457180_831485975.1523245447672" X-Mailer: Zimbra 8.6.0_GA_1153 (ZimbraWebClient - FF47 (Win)/8.6.0_GA_1153) Thread-Topic: Conflict between JSON_AGG and COPY Thread-Index: KrrhM1pyue+ArdQUa9sOlb7emgAGlA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_13457180_831485975.1523245447672 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Dear Arian Klaver,=20 I think there is a misunderstood here.=20 I think that I quite understand how is the second query run.=20 The question I asked here is why exporting data, which is generated by a qu= ery "JSON_AGG" , with command "COPY". The data contain "\n" as 2 characters= instead of "new line" character.=20 The second query is for the old version of PostgreSQL (9.3 and previous) ca= use of they don't have JSON_AGG aggregate function. Since 9.4, we have "JSO= N_AGG" already. So I want to rewrite and reduce the length of the query. Bu= t it is don't work as I expected with command COPY.=20 Thank you and best regards,=20 =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng (Mr.) | Software D= evelopment Department | +84 28 3715 5325=20 From: "Adrian Klaver" =20 To: "=C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng" , "pgsql-general" =20 Sent: Monday, April 9, 2018 12:59:44 AM=20 Subject: Re: Conflict between JSON_AGG and COPY=20 On 04/07/2018 03:44 AM, =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB= =9Dng wrote:=20 Dear all,=20 I've found one case. I don't know this is a bug or I config/query some=20 things wrong.=20 Let I describe it. I have a table with structure and data is:=20 id | username | fullname=20 ----+-------------+---------------=20 1 | john | John=20 2 | anna | Anna=20 3 | sussi | Sussi=20 4 | david | David Beckham=20 5 | wayne | Wayne Rooney=20 I want to export it to a file in JSON format so I run the query as below:= =20 COPY (SELECT row_to_json(t) FROM (SELECT json_agg(t1) AS "RECORDS"=20 FROM test_table t1) t) TO '/home/postgres/test1.json';=20 But the result I got will include "\n" in the result:=20 {"RECORDS":[{"id":1,"username":"john","fullname":"John"}, \n=20 {"id":2,"username":"anna","fullname":"Anna"}, \n=20 {"id":3,"username":"sussi","fullname":"Sussi"}, \n=20 {"id":4,"username":"david","fullname":"David Beckham"}, \n=20 {"id":5,"username":"wayne","fullname":"Wayne Rooney"}]}=20 Then, I try to get the same data in the other way:=20 COPY (WITH t2 AS (select row_to_json(t) as js from test_table t),=20 t1 AS (SELECT ARRAY_TO_JSON(array_agg(js)) as "RECORDS"=20 FROM t2)=20 SELECT row_to_json(t1) FROM t1)=20 TO '/home/postgres/test2.json';=20 CREATE TABLE test_table (id integer, username varchar, fullname varchar);= =20 INSERT INTO=20 test_table=20 VALUES=20 (1, 'john', 'John'),=20 (2, 'anna', 'Anna'),=20 (3, 'sussi', 'Sussi'),=20 (4, 'david', 'David Beckham'),=20 (5, 'wayne', 'Wayne Rooney');=20 This can be shortened to:=20 COPY=20 (select array_to_json(array_agg(row_to_json(t))) FROM test_table AS t)=20 TO '/home/postgres/test2.json';=20 BQ_BEGIN And the result I got is quite match what I expect.=20 {"RECORDS":[{"id":1,"username":"john","fullname":"John"},{"id":2,"username"= :"anna","fullname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{= "id":4,"username":"david","fullname":"David=20 Beckham"},{"id":5,"username":"wayne","fullname":"Wayne Rooney"}]}=20 I think the COPY command does not the `\n` character for pretty in=20 `json_agg` command.=20 BQ_END Well in the first case you are starting by concatenating the 5 rows in=20 the table into a single row with the table rows separated by new lines:=20 SELECT json_agg(t1) AS "RECORDS" FROM test_table t1;=20 RECORDS=20 -----------------------------------------------------------=20 [{"id":1,"username":"john","fullname":"John"}, +=20 {"id":2,"username":"anna","fullname":"Anna"}, +=20 {"id":3,"username":"sussi","fullname":"Sussi"}, +=20 {"id":4,"username":"david","fullname":"David Beckham"}, +=20 {"id":5,"username":"wayne","fullname":"Wayne Rooney"}]=20 (1 row)=20 In the second case you start by maintaining the separate table rows:=20 select row_to_json(t) as js from test_table t;=20 js=20 --------------------------------------------------------=20 {"id":1,"username":"john","fullname":"John"}=20 {"id":2,"username":"anna","fullname":"Anna"}=20 {"id":3,"username":"sussi","fullname":"Sussi"}=20 {"id":4,"username":"david","fullname":"David Beckham"}=20 {"id":5,"username":"wayne","fullname":"Wayne Rooney"}=20 (5 rows)=20 and then keeping that as an array of arrays:=20 select array_agg(row_to_json(t)) from test_table t;=20 {"{\"id\":1,\"username\":\"john\",\"fullname\":\"John\"}","{\"id\":2,\"user= name\":\"anna\",\"fullname\":\"Anna\"}","{\"id\":3,\"username\":\"sussi\",\= "fullname\":\"Sussi\"}","{\"id\":4,\"username\":\"david\",\"fullname\":\"Da= vid=20 Beckham\"}","{\"id\":5,\"username\":\"wayne\",\"fullname\":\"Wayne=20 Rooney\"}"}=20 (1 row)=20 which then gets turned back into JSON:=20 select ARRAY_TO_JSON(array_agg(row_to_json(t))) from test_table t;=20 [{"id":1,"username":"john","fullname":"John"},{"id":2,"username":"anna","fu= llname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{"id":4,"use= rname":"david","fullname":"David=20 Beckham"},{"id":5,"username":"wayne","fullname":"Wayne Rooney"}]=20 BQ_BEGIN Please help me give me your idea. Am I wrong or this is really a bug?=20 Thank you and best regards,=20 =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD*C=C6=B0=E1=BB=9Dng*(Mr.)=20 *Software Development Dept.*=20 Mobile: +84 9 3800 3394 =20 Phone: +84 28 3715 6322 =20 Email: dntcuong@digi-texx.vn=20 DIGI-TEXX | a global BPO provider=20 Address: Anna Building, Quang Trung Software City,=20 District. 12, Ho Chi Minh City, Vietnam=20 Website: www.digi-texx.vn =20 //=20 /IMPORTANT NOTICE:/=20 /*This e-mail and any attachments may contain confidential and/or=20 privileged information. If you are not the intended recipient, please=20 delete it and notify the sender immediately. Any unauthorized copying,=20 disclosure or distribution of the material in this e-mail is strictly=20 forbidden./=20 /*Please consider the environment before printing./=20 BQ_END --=20 Adrian Klaver=20 adrian.klaver@aklaver.com=20 ------=_Part_13457180_831485975.1523245447672 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Dear Arian Klaver,

I think there is a misunderstood here.

I think that I qui= te understand how is the second query run.
<= div>
The question I asked here is why ex= porting data, which is generated by a query "JSON_AGG" , with command "COPY= ". The data contain "\n" as 2 characters instead of "new line" character.&n= bsp;

The second query is for the old version of PostgreSQL (9.3 and previous) = cause of they don't have JSON_AGG aggregate function. Since 9.4, we have "J= SON_AGG" already. So I want to rewrite and reduce the length of the query. = But it is don't work as I expected with command COPY.

Thank you and best regards,


=C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng (Mr.) | Software Development Departmen= t | +84 28 371= 5 5325



From: "Adrian Klaver" <a= drian.klaver@aklaver.com>
To: "=C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr= =C3=AD C=C6=B0=E1=BB=9Dng" <dntcuong@digi-texx.vn>, "pgsql-general" &= lt;pgsql-general@lists.postgresql.org>
Sent: Monday, April 9, = 2018 12:59:44 AM
Subject: Re: Conflict between JSON_AGG and COPY<= br>

On 04/07/2018 03:44 AM, =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr= =C3=AD C=C6=B0=E1=BB=9Dng wrote:
Dear all, 
 I've found one case. I don't know this is a bug or I conf= ig/query some
 things wrong.
 
 Let I describe it.= I have a table with structure and data is:
 
   = id | username |   fullname
 ----+-------------+----------= -----
    1 | john        | John=
    2 | anna        |= Anna
    3 | sussi      &n= bsp; | Sussi
    4 | david      = | David Beckham
    5 | wayne     | W= ayne Rooney
 
 I want to export it to a file in JSON format= so I run the query as below:
 
     COPY (SELECT= row_to_json(t) FROM (SELECT json_agg(t1) AS "RECORDS"
   &nbs= p; FROM test_table t1) t) TO '/home/postgres/test1.json';
 
&nbs= p;But the result I got will include "\n" in the result:
 
 =     {"RECORDS":[{"id":1,"username":"john","fullname":"John"}, \n<= br>     {"id":2,"username":"anna","fullname":"Anna"}, \n
=      {"id":3,"username":"sussi","fullname":"Sussi"}, \n
&= nbsp;    {"id":4,"username":"david","fullname":"David Beckham"}, = \n
     {"id":5,"username":"wayne","fullname":"Wayne Roon= ey"}]}
 
 Then, I try to get the same data in the other way= :
 
     COPY (WITH t2 AS (select row_to_json(t) = as js from test_table t),
          &= nbsp;   t1 AS (SELECT ARRAY_TO_JSON(array_agg(js)) as "RECORDS"     FROM t2)
         &n= bsp;SELECT row_to_json(t1) FROM t1)
       &nbs= p;  TO '/home/postgres/test2.json';

CRE= ATE TABLE test_table (id integer, username varchar, fullname varchar);
<= br>INSERT INTO
     test_table
VALUES
   =   (1, 'john', 'John'),
     (2, 'anna',  'Anna'= ),
     (3, 'sussi',  'Sussi'),
   &nbs= p; (4, 'david', 'David Beckham'),
     (5, 'wayne', 'Wayn= e Rooney');


This can be shortened to:

COPY
(select arr= ay_to_json(array_agg(row_to_json(t))) FROM test_table AS t)
TO '/home/p= ostgres/test2.json';


 And the r= esult I got is quite match what I expect.
 
     = {"RECORDS":[{"id":1,"username":"john","fullname":"John"},{"id":2,"username"= :"anna","fullname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{= "id":4,"username":"david","fullname":"David
     Beckham"= },{"id":5,"username":"wayne","fullname":"Wayne Rooney"}]}
 
&nbs= p;
 I think the COPY command does not the `\n` character for pretty= in
 `json_agg` command.

Well in th= e first case you are starting by concatenating the 5 rows in
the table = into a single row with the table rows separated by new lines:

SELECT= json_agg(t1) AS "RECORDS" FROM test_table t1;
      = ;                     REC= ORDS
-----------------------------------------------------------
&nbs= p; [{"id":1,"username":"john","fullname":"John"},      =     +
   {"id":2,"username":"anna","fullname":"Anna= "},           +
   {"id":3,"username"= :"sussi","fullname":"Sussi"},         +
  = {"id":4,"username":"david","fullname":"David Beckham"}, +
   = {"id":5,"username":"wayne","fullname":"Wayne Rooney"}]
(1 row)

In the second case you start by maintaining the separate table rows:
<= br>select row_to_json(t) as js from test_table t;
     &n= bsp;                     =  js
--------------------------------------------------------
&nb= sp; {"id":1,"username":"john","fullname":"John"}
  {"id":= 2,"username":"anna","fullname":"Anna"}
  {"id":3,"username":"s= ussi","fullname":"Sussi"}
  {"id":4,"username":"david","fullna= me":"David Beckham"}
  {"id":5,"username":"wayne","fullname":"= Wayne Rooney"}
(5 rows)

and then keeping that as an array of arra= ys:

select array_agg(row_to_json(t)) from test_table t;

 = ;
{"{\"id\":1,\"username\":\"john\",\"fullname\":\"John\"}","{\"id\":2,\= "username\":\"anna\",\"fullname\":\"Anna\"}","{\"id\":3,\"username\":\"suss= i\",\"fullname\":\"Sussi\"}","{\"id\":4,\"username\":\"david\",\"fullname\"= :\"David
Beckham\"}","{\"id\":5,\"username\":\"wayne\",\"fullname\":\"W= ayne
Rooney\"}"}
(1 row)

which then gets turned back into JSO= N:

select ARRAY_TO_JSON(array_agg(row_to_json(t))) from test_table t= ;

[{"id":1,"username":"john","fullname":"John"},{"id":2,"username":"= anna","fullname":"Anna"},{"id":3,"username":"sussi","fullname":"Sussi"},{"i= d":4,"username":"david","fullname":"David
Beckham"},{"id":5,"username":= "wayne","fullname":"Wayne Rooney"}]


=
 Please help me give me your idea. Am I wrong or this is really a = bug?
 
 Thank you and best regards,
 
 =C4= =90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD*C=C6=B0=E1=BB=9Dng*(Mr.)
 
&n= bsp;*Software Development Dept.*
 
     = ;    
 
 Mobile: +84 9 3800 3394 <te= l:+84917220706>
 
 Phone: +84 28 3715 6322 <callto:+8= 4%208%203715%205325>
 
 Email: dntcuong@digi-texx.vn
=  
 DIGI-TEXX | a global BPO provider
 
 Addres= s: Anna Building, Quang Trung Software City,
 
 District. 1= 2, Ho Chi Minh City, Vietnam
 
 Website: www.digi-texx.vn &= lt;http://www.digi-texx.vn/>
 
 //
 
 /I= MPORTANT NOTICE:/
 
 /*This e-mail and any attachments may = contain confidential and/or
 privileged information. If you are no= t the intended recipient, please
 delete it and notify the sender = immediately. Any unauthorized copying,
 disclosure or distribution= of the material in this e-mail is strictly
 forbidden./
 =
 /*Please consider the environment before printing./
 
=  


--
Adrian Klaver
adrian.kl= aver@aklaver.com
------=_Part_13457180_831485975.1523245447672--