I'm using C#.NET 4.0 (Visual Studio 2010), PostgreSQL 9.2, and Npgsql 2.0.12. I can't upgrade to Npgsql 3.
I'm needing to do a fast insert into a parent table and then, using the primary keys from that insert, do a fast insert into a child table.
The parent table has a column defined as "serial" that is the primary key.
The child table has an integer column that is a foreign key back to the parent table.
Not every parent record will have children. A parent can have 0, 1, or many children.
Currently I'm buffering the parent objects to a List. When 5000 parents have been buffered, spawn a new thread from the thread pool to write the records to the database. (A new List is created for the main thread to buffer the next set of parent objects.) The new thread calls NpgsqlConnection.BeginTransaction(), then inside a loop calls NpgsqlCommand.ExecuteScalar() with parameters to insert the parent record and get the primary key back. Then build the the parent's child object if any and save to another List. At the end of the loop commit the transaction of parents. But this methodology is BRUTALLY slow. Anywhere from 3 to 10 seconds to insert the 5000 records. Surely there's a better way.
After the parents are committed, I use BulkCopy described at http://codebetter.com/karlseguin/2009/10/25/postgresql-day-2/ (which uses NpgsqlCopyIn) to insert the child records. This works FANTASTIC. It inserts several thousand child records in less than half a second.
I'd love to use that BulkCopy for the parent records also. But I can't figure out how to get the primary key values back from the bulk insert.
So what's the trick to do a fast insert of parent and child records using C# and Npgsql? The answer is probably out there somewhere, but clearly I'm not using the right search engine parameters.
Thanks very much in advance.