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 1gZCtJ-0000Y6-43 for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 10:46:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZCtF-00040y-Ub for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 10:46:17 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZCtF-00040r-M8 for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 10:46:17 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZCtC-0004rA-Hr for pgsql-general@postgresql.org; Tue, 18 Dec 2018 10:46:16 +0000 Received: from smtp2.mailbox.org (unknown [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 0BD1D497BD; Tue, 18 Dec 2018 11:46:12 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id AdA1huiTHxEP; Tue, 18 Dec 2018 11:46:11 +0100 (CET) Message-ID: <51c2cd6edffd7308e317635d022690c23096eeae.camel@cybertec.at> Subject: Re: How to insert bulk data with libpq in C? From: Laurenz Albe To: Chris Mair , a <372660931@qq.com> Cc: pgsql-general Date: Tue, 18 Dec 2018 11:46:10 +0100 In-Reply-To: References: <80ed60d54a29cb8bf3058dc0b9f5d44cf244378c.camel@cybertec.at> Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Chris Mair wrote: > > > 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. > see also > > https://www.postgresql.org/docs/10/static/protocol-flow.html#PROTOCOL-COPY > > https://www.postgresql.org/docs/10/static/sql-copy.html#id-1.9.3.52.9.4 > > Also, I suggesttransaction 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. Yours, Laurenz Albe