Live data from Hacker News

Fast Way to Load Data into PostgreSQL Using Python

hakibenita.com

11–20 of 31 posts

Re: Fast Way to Load Data into PostgreSQL Using Python

#11

Would also like this with asyncpg. psycopg2 is kind of obsolete today.

Bold claim, since psycopg2 is used overwhelmingly more than any other library. Though asyncpg (or something like it) is likely the future, it’s a long way to go before calling psycopg obsolete.

Re: Fast Way to Load Data into PostgreSQL Using Python

#12

Would also like this with asyncpg. psycopg2 is kind of obsolete today.

Bold claim, since psycopg2 is used overwhelmingly more than any other library. Though asyncpg (or something like it) is likely the future, it’s a long way to go before calling psycopg obsolete.

Asyncpg is not future, it is present. We use it in production and quite happy with it.

Re: Fast Way to Load Data into PostgreSQL Using Python

#13

I am looking for ways to quickly load data into a PG database for local testing. An initial seed run is ok, but I want test suites to run on identical data sets and since PG doesn't (yet) support nested transactions, I a currently use my seed database as a TEMPLATE for suite-specific databases. It works reasonably well but creating the database with a template takes several seconds, even though my test data is fairly…

Not an expert, but I'm curious - what use case is not sufficiently solved by savepoints?

Re: Fast Way to Load Data into PostgreSQL Using Python

#14
post #4

I concur - we load a fair amount of data into Postgres using Python and we've learned these lessons the hard way. We ended up feeding generators into execute_batch. We don't use execute_values, because we want to generate dictionaries (since there are usually multiple generators along the way) and we don't use copy_from, because the cognitive overhead of data prep is just way too high for the benefit (for us! your mi…

For old school CSV and Excel that requires any processing before inserting I Have found Perl with the Relevant Modules from CPAN for CSV and Excel is the best solution

Re: Fast Way to Load Data into PostgreSQL Using Python

#15
post #10
post #6

There’s also copy_expert in psycopg2. I believe csv with copy_expert should be the fastest but have not tested in as much detail as op. Great work on the article.

IIRC copy_expert just lets you enter the postgres COPY command that's used to start the transfer manually, which by itself doesn't change anything. It does allow you to use the BINARY transfer format, though, and that's quite often the fastest version, at the cost of a more complicated data transformation step.

Plus freeze for use cases like the example in the post.

Re: Fast Way to Load Data into PostgreSQL Using Python

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

Re: Fast Way to Load Data into PostgreSQL Using Python

#17
Why do you drop and recreate an unlogged table? Why not create a temporary table, which is also not WAL-logged and automatically gets cleaned up? It looks like you're not closing the connection. The only advantage is that the staging table remains in place until the next execution, but presumably so does the data file.

Alternately, if you have an unlogged table already created, why not simply truncate the table if it exists instead of recreating it on every execution? Drop and truncate should take about the same amount of time, but creating a table is going to be slightly more expensive. If you're worried about making sure the db table schema matches the file format, I don't think I would be. File formats change, you're already going to be manually working on it. You can drop the table so the updated script recreates it.

I suppose it doesn't really matter, but I'm wondering if your choice was one of intent or one of convenience.

Re: Fast Way to Load Data into PostgreSQL Using Python

#18

I am looking for ways to quickly load data into a PG database for local testing. An initial seed run is ok, but I want test suites to run on identical data sets and since PG doesn't (yet) support nested transactions, I a currently use my seed database as a TEMPLATE for suite-specific databases. It works reasonably well but creating the database with a template takes several seconds, even though my test data is fairly…

Not an expert, but I'm curious - what use case is not sufficiently solved by savepoints?

They might well be faster, I haven't tried. I just remember looking into them and thinking that it was more involved than what I had now so I decided against them.

Re: Fast Way to Load Data into PostgreSQL Using Python

#19

Earlier quoted context omitted.

Bold claim, since psycopg2 is used overwhelmingly more than any other library. Though asyncpg (or something like it) is likely the future, it’s a long way to go before calling psycopg obsolete.

Asyncpg is not future, it is present. We use it in production and quite happy with it.

That still doesn't really challenge parent's claims.
Post reply on HN