pipelining in v0.1.0 #25
-
We noticed that v0.1.0 supports pipelining of "independent queries" -- thanks for that! I have some questions about the intended use case and guarantees. We'd really like to be able to issue a whole transaction, like this: query 1: My understanding is that this should work, provided that:
The docs talk about pipelining being used for "independent queries". I assume what it means is that "even if query B is issued after query A, the SQL text of query B must not depend on the data returned by query A". Query B's behavior might still depend on the results of query A. Suppose query A is |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Using pipelining to optimize transactions is one of the use cases for which this feature is designed. "independent queries" does refer here mostly to not allow the following constructs: let id = users::table.filter(users::name.eq("John")).select(users::id).get_result(conn).await?;
diesel::update(posts::table.filter(users::id.eq(id))).set(posts::title.eq("John's posts").execute(conn).await?; This is obviously not possible as the second query depends on inputs returned by the first query. Any query combination that does not need to use the results from previous queries can theoretically use pipelining.
That's up to the user. Pipelining works if you use the same connection. If you use different connections it cannot use pipelining.
Again that's up to the user. Queries are issued in the order their futures are polled. Before that the connection does not even know that a specific query exists. If queries are truly independent (== one query do not modify data that are read by an other query) the order does not really matter, otherwise you need to be a bit careful how you create the combined future. Use something like
The transaction function does not handle unwinding or canceling the future. If that happens it results in both cases in having a connection with an open transaction. If you use a pool and the connection is returned to the pool, the pooling implementation will take of this, mark the connection as broken and close it afterwards. If you don't use a pool and reuse the same connection later, that's something you need to handle later.
Technically that would be fine, but can lead to different results depending on which query future is polled first. For more details see postgresql's documentation on this topic |
Beta Was this translation helpful? Give feedback.
Using pipelining to optimize transactions is one of the use cases for which this feature is designed. "independent queries" does refer here mostly to not allow the following constructs:
This is obviously not possible as the second query depends on inputs returned by the first query. Any query combination that does not need to use the results from previous queries can theoretically use pipelining.