Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fXXHk-0004oi-CV for pgsql-general@arkaria.postgresql.org; Mon, 25 Jun 2018 19:36:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fXXHi-0002W4-Cu for pgsql-general@arkaria.postgresql.org; Mon, 25 Jun 2018 19:36:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fXXHg-0002Vx-F4 for pgsql-general@lists.postgresql.org; Mon, 25 Jun 2018 19:36:22 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fXXHa-0003Eg-7o for pgsql-general@lists.postgresql.org; Mon, 25 Jun 2018 19:36:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C9F09217DC; Mon, 25 Jun 2018 15:36:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 25 Jun 2018 15:36:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=qCkWg9ymSTUTo/L9ongy3jeqUN+RP Yd76nj7/Ty6GuI=; b=y6QXAD6aD/zG3nfyQOzeLlxI+9HfHuVjSxriddpy2SCJm Lxsw7ZgpYt0PGXGq5KUl4kVn2SdC5741IQXnMXOFq2vMMotXu1w75b6p1s/JOkae RcywjcP6VkfSe/FTbQDfUBAs5y1Mw/mMxJQlU3wdRNZxMtgvAgR2bzzylLlRrvZx t8AEES0gfxY5ww1znnOiQqlij53lFZkr+ICYjVcfb76Ib/eWFbjPaBTLCbfofadS 7MBjssse7P5l8c+4uQ8Q/TPVgzMAkM1Jd6YsjpCWK9y8xLuiTlsZvdOVmo2CNIL6 DVtYmsL+tMvsgd9dV8YgbxTMytqdqwvUyilQZkxQg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=qCkWg9 ymSTUTo/L9ongy3jeqUN+RPYd76nj7/Ty6GuI=; b=V+Np/PyhZ0Oya+niHK16Yf hYQvk6YT6O2FoH10ZpVTTkigMglamH4746366fbcixyMiPcBrvb76KPM3/R6DBXG 9dRiG8UJGRvQEYz4oOVMsxah3v/iwkmo4R9KEEbJZyxGid1nShETWlEv5iAX5mVQ hvJio7DUzzHL4Nw8cxGXdde3CRxoMUH3oHiObY7N9zg5JV6Y4rUJqB2205NCGJ7Y 3hzpxAGNAgI5Ucrw51J06w6WVJRo7Cz2Jutc6ha4UTobA9Fpuqp7qQhrGhjgSbim LECoYnj6IT0qdDJCojKfHNcfBzrlWcsXV/MV9iMGF/YZ4FjEsmMRAaHGg509l4uA == X-ME-Proxy: X-ME-Sender: Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id AEA2CE4449; Mon, 25 Jun 2018 15:36:10 -0400 (EDT) Subject: Re: Using COPY to import large xml file To: Anto Aravinth , Tim Cross Cc: Adrien Nayrat , "pgsql-generallists.postgresql.org" References: <95bb81dd-a80a-a493-2c3b-c03848acefc8@anayrat.info> <874lhrsugr.fsf@gmail.com> From: Adrian Klaver Message-ID: Date: Mon, 25 Jun 2018 12:36:09 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/25/2018 07:25 AM, Anto Aravinth wrote: > Thanks a lot. But I do got lot of challenges! Looks like SO data > contains lot of tabs within itself.. So tabs delimiter didn't work for > me. I thought I can give a special demiliter but looks like Postrgesql > copy allow only one character as delimiter :( I use | as it is rarely found in data itself. > > Sad, I guess only way is to insert or do a through serialization of my > data into something that COPY can understand. > > On Mon, Jun 25, 2018 at 8:09 AM, Tim Cross > wrote: > > > > On Mon, 25 Jun 2018 at 11:38, Anto Aravinth > > > wrote: > > > > On Mon, Jun 25, 2018 at 3:44 AM, Tim Cross > > wrote: > > > Anto Aravinth > writes: > > > Thanks for the response. I'm not sure, how long does this tool takes for > > the 70GB data. > > > > I used node to stream the xml files into inserts.. which was very slow.. > > Actually the xml contains 40 million records, out of which 10Million took > > around 2 hrs using nodejs. Hence, I thought will use COPY command, as > > suggested on the internet. > > > > Definitely, will try the code and let you know.. But looks like it uses the > > same INSERT, not copy.. interesting if it runs quick on my machine. > > > > On Sun, Jun 24, 2018 at 9:23 PM, Adrien Nayrat > > > wrote: > > > >> On 06/24/2018 05:25 PM, Anto Aravinth wrote: > >> > Hello Everyone, > >> > > >> > I have downloaded the Stackoverflow posts xml (contains all SO questions > >> till > >> > date).. the file is around 70GB.. I wanna import the data in those xml > >> to my > >> > table.. is there a way to do so in postgres? > >> > > >> > > >> > Thanks, > >> > Anto. > >> > >> Hello Anto, > >> > >> I used this tool : > >> https://github.com/Networks-Learning/stackexchange-dump-to-postgres > > >> > > If you are using nodejs, then you can easily use the > pg-copy-streams > module to insert the records into your database. I've been > using this > for inserting large numbers of records from NetCDF files. > Takes between > 40 to 50 minutes to insert 60 Million+ records and we are doing > additional calculations on the values, not just inserting them, > plus we are inserting into a database over the network and > into a database which is > also performing other processing. > > We found a significant speed improvement with COPY over > blocks of insert > transactions, which was faster than just individual inserts. > The only > downside with using COPY is that it either completely works or > completely fails and when it fails, it can be tricky to work > out which > record is causing the failure. A benefit of using blocks of > transactions > is that you have more fine grained control, allowing you to > recover from > some errors or providing more specific detail regarding the > cause of the > error. > > > Sure, let me try that.. I have a question here, COPY usually > works when you move data from files to your postgres instance, > right? Now in node.js, processing the whole file, can I use COPY > programmatically like COPY Stackoverflow run time>? Because from doc: > > https://www.postgresql.org/docs/9.2/static/sql-copy.html > > > I don't see its possible. May be I need to convert the files to > copy understandable first? > > Anto. > > > > > Yes. Essentially what you do is create a stream and feed whatever > information you want to copy into that stream. PG sees the. data as > if it was seeing each line in a file, so you push data onto the > stream wherre each item is seperated by a tab (or whatever). Here is > the basic low level function I use (Don't know how the formatting > will go!) > > async function copyInsert(sql, stringifyFN, records) { >   const logName = `${moduleName}.copyInsert`; >   var client; > >   assert.ok(Array.isArray(records), "The records arg must be an > array"); >   assert.ok(typeof(stringifyFN) === "function", "The stringifyFN > arg must be a function"); >   return getClient() >     .then(c => { >       client = c; >       return new Promise(function(resolve, reject) { >         var stream, rs; >         var idx = 0; >         function done() { >           releaseClient(client); >           client = undefined; >           resolve(idx + 1); >         } > >         function onError(err) { >           if (client !== undefined) { >             releaseClient(client); >           } >           reject(new VError(err, `${logName}: COPY failed at record > ${idx}`)); >         } > >         function arrayRead() { >           if (idx === records.length) { >             rs.push(null); >           } else { >             let rec = records[idx]; >             rs.push(stringifyFN(rec)); >             idx += 1; >           } >         } > >         rs = new Readable; >         rs._read = arrayRead; >         rs.on("error", onError); >         stream = client.query(copyFrom(sql)); >         stream.on("error", onError); >         stream.on("end", done); >         rs.pipe(stream); >       }); >     }) >     .catch(err => { >       throw new VError(err, `${logName} Failed COPY insert`); >     }); > } > > and I will call it like > > copyInsert(sql, stringifyClimateRecord, records) > > where sql and stringifycomateRecord arguments are > >   const sql = `COPY access_s.climate_data_ensemble_${ensemble} ` >         + "(forecast_dt,awap_id,rain,temp_min,temp_max,rsds," >         + "vprp_09,vprp_15,wind_speed) FROM STDIN"; > >   function stringifyClimateRecord(rec) { >     return `'${rec[0].format("YYYY-MM-DD")}'\t${rec[2]}\t${rec[3]}\t` >       + > `${rec[4]}\t${rec[5]}\t${rec[6]}\t${rec[7]}\t${rec[8]}\t${rec[9]}\n`; >   } > > The stringifyClimateRecord returns a record to be inserted as a > 'line' into the stream with values separated by tabs. Records is an > array of data records where each record is an array. > > > -- > regards, > > Tim > > -- > Tim Cross > > -- Adrian Klaver adrian.klaver@aklaver.com