10 ms·
Not sure what the python code translates to, so maybe this is covered, but in my experience, if you can't use COPY, you should: 1 - Generate a static SQL to in
by latch 7y ago
Not sure what the python code translates to, so maybe this is covered, but in my experience, if you can't use COPY, you should:
1 - Generate a static SQL to insert N rows (say 10 to 50):
insert into blah (id, name) values ($1, $2), ($3, $4),
2 - Loop through the data in batch size
3 - Generate a dynamic sql for M rows where M is whatever's left over
Wrap it in a transaction. You can have an SQL builder where you give it the list of columns and how many rows you want to generate and use that for both steps 1 and 3. You can prepare statement 1 (and keep reusing it), but I'mnot sure how much that'll buy you.
If you need to deal with conflicts. Inserting the above into a temp table (on commit drop) and then doing an insert + update from the team into the real table works well.
Then there's a handful of postgresql configs to tweak, but if you're willing to risk a bit of data loss (but not corruption), asynchronous_commit=off.