asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

PostgreSQL 14+ Pipelining Support

Open MicahLyle opened this issue 3 years ago • 8 comments

PostgreSQL 14 introduced the ability to Pipeline queries.

Are there any plans to support this with asyncpg in any capacity? At the time of writing, I'm looking to optimize large numbers of inserts and updates, and pipelining seems like it could be an interesting solution. I checked the existing issues and didn't seem to find anything related to this so I thought I'd open a fresh issue/feature request.

MicahLyle avatar Oct 15 '21 16:10 MicahLyle

Pipelining is already supported via executemany.

elprans avatar Oct 15 '21 17:10 elprans

@elprans executemany discards the results, so it doesn't work in all the cases that pipelining would be useful.

In my case, it would be useful to be able to pipeline SELECT statements.

chdsbd avatar Nov 18 '21 13:11 chdsbd

@chdsbd can you describe your use case in some detail?

elprans avatar Nov 18 '21 16:11 elprans

I have multiple queries that I want to run against Postgres.

Currently I can send these queries with two await conn.fetch(...), but it would be better if I didn't have to wait for the first one to return.

select * from comments where user_id = 5;
select * from edits where user_id = 5;

chdsbd avatar Nov 18 '21 20:11 chdsbd

Pipelining only makes sense if you are sending lots and lots of queries as is the case with bulk insert. For regular queries it makes very little sense in the face of substantial implementation complexity, cancellation issues etc. In your case you can trivially emulate pipelining by querying both tables at the same time like so:

    comments, edits = await conn.fetchrow('''
        select
           (select array_agg(comments.*) from comments where user_id = 5),
           (select array_agg(edits.*) from edits where user_id = 5)
    ''')

elprans avatar Nov 18 '21 21:11 elprans

@elprans Thanks for the suggestion!

chdsbd avatar Nov 18 '21 23:11 chdsbd

Pipelining only makes sense if you are sending lots and lots of queries as is the case with bulk insert. For regular queries it makes very little sense in the face of substantial implementation complexity, cancellation issues etc.

I somewhat disagree. Pipelining also makes sense with a smaller number of queries if the query execution time is dominated by network latency.

Also, if executemany already supports pipelining then doesn't that mean that asyncpg already has the implementation complexity in its codebase, or is the executemany pipelining somehow simpler than the general case?

FeldrinH avatar Mar 21 '24 19:03 FeldrinH