Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZD4w-00019a-NU for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 10:58:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZD3w-00025v-QP for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 10:57:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZD3w-00025n-Ep for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 10:57:20 +0000 Received: from smtp1.hushmail.com ([65.39.178.135]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZD3s-0001Tq-QU for pgsql-general@postgresql.org; Tue, 18 Dec 2018 10:57:18 +0000 Received: from smtp1.hushmail.com (localhost [127.0.0.1]) by smtp1.hushmail.com (Postfix) with SMTP id E01704080E for ; Tue, 18 Dec 2018 10:57:14 +0000 (UTC) Received: from smtp.hushmail.com (w2.hushmail.com [65.39.178.46]) by smtp1.hushmail.com (Postfix) with ESMTP; Tue, 18 Dec 2018 10:57:13 +0000 (UTC) Subject: Re: How to insert bulk data with libpq in C? To: Laurenz Albe , a <372660931@qq.com> Cc: pgsql-general References: <80ed60d54a29cb8bf3058dc0b9f5d44cf244378c.camel@cybertec.at> <51c2cd6edffd7308e317635d022690c23096eeae.camel@cybertec.at> From: Chris Mair Message-ID: <59eda4cff9a454701320e2a69569c156@smtp.hushmail.com> Date: Tue, 18 Dec 2018 11:57:09 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:60.0) Gecko/20100101 Thunderbird/60.3.3 MIME-Version: 1.0 In-Reply-To: <51c2cd6edffd7308e317635d022690c23096eeae.camel@cybertec.at> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>> May I ask the proper (fast) way of inserting bulk data and if possible, an example would be very appreciated. >>>> >>>> Please note that the data I would like to insert contains arrays (float8[]). >>>> >>>> By the way, my environment is Windows 10, PostgreSQL 11 and compiling with Visual Studio 2017. >>> >>> That would be COPY: >>> https://www.postgresql.org/docs/current/sql-copy.html >> >> In particular, >> >> copy table (col1, col2, ...) from stdin with binary > > Hmm, binary requires that you know the binary PostgreSQL representation. > PostgreSQL might be faster converting text to its internal representation > than your application program... > > The big exception here is of course bytea, where the binary representation > is definitely smaller and easy to create. Hi, yes. I understood the data is already binary and OP wants to store it binary, but of course this might not be the case. >> Also, I suggest transaction bundling (one commit every 1000 records or so), >> depending on context. > > Why that? > It might be useful if you expect the load to fail in the middle and want > to be able to restart from there. But normally, doing it all in one > transaction is simpler. Yes. That would be the special case with one commit every N records, where N is the total count :) I just wanted to make shure OP will not commit after each COPY. Bye. Chris.