Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import *huge* chunks of data to PostgreSQL?

I have a data structure that looks like this:

Model Place
    primary key "id"

    foreign key "parent" -> Place
    foreign key "neighbor" -> Place (symmetryc)
    foreign key "belongtos" -> Place (asymmetric)

    a bunch of scalar fields ...

I have over 5 million rows in the model table, and I need to insert ~50 million rows into each of the two foreign key tables. I have SQL files that look like this:

INSERT INTO place_belongtos (from_place_id, to_place_id) VALUES (123, 456);

and they are about 7 Gb each. The problem is, when I do psql < belongtos.sql, it takes me about 12 hours to import ~4 million rows on my AMD Turion64x2 CPU. OS is Gentoo ~amd64, PostgreSQL is version 8.4, compiled locally. The data dir is a bind mount, located on my second extended partition (ext4), which I believe is not the bottleneck.

I'm suspecting it takes so long to insert the foreign key relations because psql checks for the key constraints for each row, which probably adds some unnecessary overhead, as I know for sure that the data is valid. Is there a way to speed up the import, i.e. temporarily disabling the constraint check?

like image 347
Attila O. Avatar asked Aug 09 '10 04:08

Attila O.


People also ask

Can Postgres handle 100 million rows?

Aggregations vs. If you're simply filtering the data and data fits in memory, Postgres is capable of parsing roughly 5-10 million rows per second (assuming some reasonable row size of say 100 bytes). If you're aggregating then you're at about 1-2 million rows per second.

How big is too big for a Postgres database?

There is no PostgreSQL-imposed limit on the number of indexes you can create on a table. Of course, performance may degrade if you choose to create more and more indexes on a table with more and more columns. PostgreSQL has a limit of 1GB for the size of any one field in a table.

Which commands is the most efficient way to bulk load data into a Postgres table from a CSV file?

Goto solution for bulk loading into PostgreSQL is the native copy command.


1 Answers

  1. Make sure both foreign key constraints are DEFERRABLE
  2. Use COPY to load your data
  3. If you can't use COPY, use a prepared statement for your INSERT.
  4. Propper configuration settings will also help, check the WAL settings.
like image 87
Frank Heikens Avatar answered Sep 23 '22 11:09

Frank Heikens