pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to insert bulk data with libpq in C?
5+ messages / 3 participants
[nested] [flat]

* How to insert bulk data with libpq in C?
@ 2018-12-18 03:03 a <372660931@qq.com>
  2018-12-18 08:24 ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 1 reply; 5+ messages in thread

From: a @ 2018-12-18 03:03 UTC (permalink / raw)
  To: pgsql-general

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.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to insert bulk data with libpq in C?
  2018-12-18 03:03 How to insert bulk data with libpq in C? a <372660931@qq.com>
@ 2018-12-18 08:24 ` Laurenz Albe <laurenz.albe@cybertec.at>
  2018-12-18 09:14   ` Re: How to insert bulk data with libpq in C? Chris Mair <chris@1006.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Laurenz Albe @ 2018-12-18 08:24 UTC (permalink / raw)
  To: a <372660931@qq.com>; pgsql-general

a 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

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to insert bulk data with libpq in C?
  2018-12-18 03:03 How to insert bulk data with libpq in C? a <372660931@qq.com>
  2018-12-18 08:24 ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
@ 2018-12-18 09:14   ` Chris Mair <chris@1006.org>
  2018-12-18 10:46     ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 1 reply; 5+ messages in thread

From: Chris Mair @ 2018-12-18 09:14 UTC (permalink / raw)
  To: a <372660931@qq.com>; +Cc: pgsql-general

>> 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

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.

Bye,
Chris.





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to insert bulk data with libpq in C?
  2018-12-18 03:03 How to insert bulk data with libpq in C? a <372660931@qq.com>
  2018-12-18 08:24 ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
  2018-12-18 09:14   ` Re: How to insert bulk data with libpq in C? Chris Mair <chris@1006.org>
@ 2018-12-18 10:46     ` Laurenz Albe <laurenz.albe@cybertec.at>
  2018-12-18 10:57       ` Re: How to insert bulk data with libpq in C? Chris Mair <chris@1006.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Laurenz Albe @ 2018-12-18 10:46 UTC (permalink / raw)
  To: Chris Mair <chris@1006.org>; a <372660931@qq.com>; +Cc: pgsql-general

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





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: How to insert bulk data with libpq in C?
  2018-12-18 03:03 How to insert bulk data with libpq in C? a <372660931@qq.com>
  2018-12-18 08:24 ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
  2018-12-18 09:14   ` Re: How to insert bulk data with libpq in C? Chris Mair <chris@1006.org>
  2018-12-18 10:46     ` Re: How to insert bulk data with libpq in C? Laurenz Albe <laurenz.albe@cybertec.at>
@ 2018-12-18 10:57       ` Chris Mair <chris@1006.org>
  0 siblings, 0 replies; 5+ messages in thread

From: Chris Mair @ 2018-12-18 10:57 UTC (permalink / raw)
  To: Laurenz Albe <laurenz.albe@cybertec.at>; a <372660931@qq.com>; +Cc: pgsql-general


>>>> 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.







^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-12-18 10:57 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-18 03:03 How to insert bulk data with libpq in C? a <372660931@qq.com>
2018-12-18 08:24 ` Laurenz Albe <laurenz.albe@cybertec.at>
2018-12-18 09:14   ` Chris Mair <chris@1006.org>
2018-12-18 10:46     ` Laurenz Albe <laurenz.albe@cybertec.at>
2018-12-18 10:57       ` Chris Mair <chris@1006.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox