1

I'm inserting millions of records, coming from C++ structures. With regular inserts, I'm getting a terrible performance, with the database taking up 98% of program time, even after config optimization. I'm reading that I should use COPY to import it from a CSV-file.

Now I'm not so sure if writing to a CSV-file first and then rewriting it to the DB will be much of an improvement, since the writing will double. I've looked at piping the STDIN, but at first glance it seems like there's also much overhead, and it's limited

If I've got a string in CSV-format, what would be the quickest way to write that data to my DB?

Thank you in advance,

CX

Breina
  • 538
  • 5
  • 20

1 Answers1

2

I assume you need to do it once on an offline database. If you need to do it on an online database there's not much you can do besides using a your-program-generating-a-copy-command | psql or PQputCopyData from your program.

  1. Disable all other access to database, terminate all client connections.
  2. Backup a database without your bulk data, as this procedure is dangerous (a crash, reboot or power failure during this will make your database corrupted beyond repair).
  3. Shutdown database.
  4. Move pg_xlog directory from data directory to a tmpfs (RAM-disk), symlink it.
  5. Run postgres -F -c full_page_writes=off -c checkpoint_segments=128 …. It will need about 2GB of free RAM more than usually — be prepared.
  6. Drop primary key contraint from your table.
  7. Drop foreign key constraints from your table.
  8. Load data using COPY from stdin and a pipe or PQputCopyData.
  9. Run analyze.
  10. Recreate primary key constraint.
  11. Recreate foreign key constraints (a file generated by pg_dump before dropping them will have suitable commands near the end).
  12. Shutdown database.
  13. Delete pg_xlog symlink and move pg_xlog directory back to data directory.
  14. Run sync command on server.
  15. Start database normally.

This is based on Populating a Database guide from documentation.

Tometzky
  • 22,573
  • 5
  • 59
  • 73