'Where is query pipelining in libpq?
I'm developing a high load server application using C++ and libpq (libpqxx is very unstable). I use async queries in threads to improve performance. But I've discovered that, for example, PQsendQuery("SELECT 1;SELECT 1;")
works faster then executing PQsendQuery("SELECT 1")
two times.
Is there any way to call bunch prepared of statements or another way to pipeline or use batch mode?
EDITED: Summary: How to get best performance in libpq?
Solution 1:[1]
libpq
supports support query pipelining in PostgreSQL 14. You can use it on old PostgreSQL versions so long as you're using a new libpq. See commit acb7e4eb6b.
I wrote a patch for it in 2016 as a bit of a fire-and-forget weekend hack Thanks to the efforts of many others, it has been turned into a mature feature and merged into PostgreSQL 14.
The documentation for PostgreSQL 14 discusses pipeline mode.
Note that PgJDBC has supported batch mode for many years using the standard JDBC batch interfaces. For many applications you can achieve similar performance improvements using PgJDBC batches.
psycopg2
is currently adding or has added libpq-based adding batch and pipeline support.
Solution 2:[2]
Every time you execute a query with PQsendQuery()
and get the results with PQgetResult()
, there is a round trip to the database server. This costs time. If you execute multiple commands in one query, you save a round trip.
I have never heard of batch mode and it appears to never have landed in the official libpq release.
You have some options though. Which suits you best depends on your use case.
If you need to insert multiple rows into the same table, doing this in one query will be a lot faster. You can use prepared queries with different number of rows or build the query dynamically. If you use the latter, don't forget to do proper SQL escaping!
If you need to insert multiple rows, doing this within a single transaction might be faster. The PostgreSQL manual has some additional tricks.
You could use caching of query results using pgpool or a custom solution, for example using memcached.
Use stored procedures to combine multiple queries.
Use more complex
SELECT
queries or views to combine multiple queries.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | rveerd |