Repository navigation
Persist prepared commands - #1084
MarkusSintonen wants to merge 2 commits into
Conversation
|
Thanks @MarkusSintonen, any chance you can squash all those commits? |
2d31d57 to
eafa65f
Compare
|
@roji commits are now squashed into a single commit |
- Implemented command persisting - Added IsPersisted property to NpgsqlCommand - Added Persist Prepared connection string parameter - Added unit tests for above
eafa65f to
a785850
Compare
3b2c4af to
ad61dee
Compare
|
|
||
| internal const int DefaultTimeout = 30; | ||
|
|
||
| internal const bool DefaultIsPersistent = false; |
There was a problem hiding this comment.
Not sure of the value of having a const false here... We can just say that the default is false without having a const to define it.
There was a problem hiding this comment.
Sure thing, will simplify this
|
@MarkusSintonen am waiting for your responses on this |
|
Hi @roji, Sorry I haven't been able to respond yet. I'm currently abroad and I'll answer ASAP when I get back home after sunday. |
|
Sure thing @MarkusSintonen, no problem. |
- Disabling IsPersistent no longer deallocates stament immediately - Fixed tests
|
@roji I made a new commit to the pull request. Sorry it took a while to react. |
|
Hi @roji, I totally missed your last comment at the closed PR #1053 so I did not respond to that. I copied your last comment here from the closed PR so we can continue discussion here:
Regarding your questions: |
|
@MarkusSintonen, I'm sorry I haven't replied sooner, other things have kept me busy. I really appreciate your work and responses, here are some comments:
Do you really think performance is important here? First, it's a one-time pass (per connection) when doing Prepare. Second, whatever time the parsing takes, it's going to be completely dwarfed by the actual networking etc. So this seems like an unnecessary optimization to me. I do understand that the data structures are necessary to support the per-command caching, but it seems like if you switch to per-statement caching things become much simpler - basically you just need a dictionary mapping (parsed) statement SQL to the PostgreSQL prepared statement name. We would need two of these - one for persistent statements and one for non-persistent statements (which get deallocated on close). This would be very light and doesn't introduce any new classes. Since performance isn't a real concern here, in my opinion, I'd opt for the lighter/less invasive design.
I may be missing something but I imagined a simple counter for persistent prepared statements only, that only goes gets incremented. For a given (physical) connection it never gets reset - no need to keep track of anything. Regarding overflow, a program would need to prepare over Let me know what you think of the above. |
|
Hi @roji nice to hear from you, Regarding your comments:
I think the use cases of persisting the prepared commands is in applications where performance is paramount. We have one of these: a time series database that is time critical requiring very high performance. We had the exact performance problems with Npgsql, that we are trying to solve in PR, the missing command persisting feature. This required us to create a custom connection pooling and command persisting logic on top of the Npgsql. I think this is not a very optimal solution since the library should support the command persisting out of the box for these kind of applications. We did performance measurements and the processing of raw command SQLs inside Prepare() was also one of the culprits for the low performance (we use few fairly complex queries that also require a high query performance). With the custom command persisting (and connection pooling) logic we got huge performance benefits (I can give some numbers later if needed). I agree that the networking somewhat dwarfs the command parsing but I don't agree that this can be generalized since one of the use cases for command persisting is with highly complex queries. Which would require some parsing burden every time when the IsPersistent command is used. Unnecessary parsing also increases the CPU usage and GC load where commands are used in a tight cycle. Where as in the PR solution a simple dictionary lookup is used instead of a full sql parsing, making it very efficient.
I agree the design could be further simplified also with the command persisting based on full SQL. But I also think the full command persisting is performance wise better (noticable with complex queries) than the solution where command is everytime parsed into individual statements.
I also agree that ulong is the best solution here and not the GUID. BR |
|
@MarkusSintonen I'm definitely interested in your performance experience with Npgsql. Can you share some hard figures specifically on the SQL parsing part? I'm sure that the current parser isn't necessarily very optimized - it's a pretty old piece of code within Npgsql and although it works pretty well it may not be the fastest. However, I'd rather optimize the parser itself than cover over its inefficiencies by persisting prepared commands; this way everyone benefits from the performance, with or without persisting. Also, I'd still like to see evidence that the SQL parsing is actually significant when compared to networking. Note also #1042, which is about allowing a mode where no parsing takes place at all. I personally really dislike that Npgsql parses SQL - IMHO it should simply be forwarding user SQL strings to the database. The two reasons this is necessary is:
So if #1042 is implemented, caching by statement becomes just as fast as caching by command, since no parsing takes place. |
|
Hi @roji
I created a version of IsPersisted feature that includes the SQL parsing step at each Prepare() to measure the performance implication of parsing. I then created a test that uses a quite complex SQL query (as was the case with our original performance problems). The test uses 4 workers that run the query in a loop to collect the performance results. The workers used their own NpgsqlConnection without returning it to pool during the test run. The test measured the time taken to Prepare() and ExecuteScalar() separately to evaluate the networking overhead. Both were measured 100000 times with JIT warm up taken into account and by using StopWatch. The tests do not take the initial Prepare() into account that prepares the statements to PostgreSQL. Here are the results: 3 test runs WITHOUT parsing:
3 test runs WITH parsing:
As you can see the parsing worsens the Prepare() performance by a factor of 2,5x. Just the parsing itself seems to take about 6% of total Prepare() + ExecuteScalar() time. The whole test run takes about 1 second longer with parsing. Below are also CPU profiler results. With CPU profiler we can see the true performance hit from the SQL parsing. 16% of total CPU usage is consumed just for parsing the SQL. Total CPU time (ms) spend in Prepare() with parsing is higher by a factor of 5,0x. Profiler also shows that Prepare() with parsing takes 27% of CPU (ms) time when Prepare+ExecuteScalar is executed. Wihout parsing this is only 7%. Parsing is therefore a major contributor to the performance problem and not so much the networking. This could be now avoided altogether with IsPersisted feature. Without parsing even the initial NpgsqlConnection opening is slower than the subsequent Prepare() calls. I also examined the SQL parsing step with profiler but there is no single point that seems to take lot of CPU cycles. It is just generally intensive to parse the query and it gets worse linearly as expected compared to the pull requests O(1) dictionary lookup. About my test setup: I know that it would be much more elegant to just persist the statements but we do not have API to initialize the NpgsqlStatements separately currently. This requires us to work with NpgsqlCommand and in my mind we should avoid the parsing step in IsPersisted feuture. This feature would have very positive performance implications in Npgsql and we should bring it as soon as possible. In the future the user initialized NpgsqlStatements (#1042) would probably also have the IsPersisted flags available. This feature would be easy to implement on top of the suggested PR. BR |
|
@MarkusSintonen, thanks for the detailed benchmark, it's always good to see hard numbers. This is definitely a "low-latency" usage of Npgsql, which seems quite rare (but that doesn't mean it isn't important). It's worth nothing that if you benchmark against any non-localhost database (even in a LAN) the SQL parsing time is almost sure to be competely dominated by networking. I'm also wondering how the gains in avoiding SQL parsing compare against the overall query execution, including server-side processing - shaving off a few microseconds at the client side doesn't mean much if the entire query roundtrip takes 50 milliseconds. More concretely, here's how I see things. #1042 is already planned for 3.2, as is the persistent prepared statement feature. #1042 removes SQL parsing altogether for those who use it, so it obviates the extra optimization involved in persisting the entire command rather than the statement. In other words, in 3.2 low-latency users should definitely use raw SQL mode. I think this way everyone wins: you get to have the performance you want, without any SQL parsing going on, and we get the simpler/more elegant design of persisting prepared statements rather than commands. The only "drawback" is that you, as a low-latency user, are forced to use raw SQL mode, which will imply certain things (i.e. using PostgreSQL's positional arguments, using a PostgreSQL-specific batching API). But that seems reasonable to me. If you agree with the above, I can dig up the work I've already done on this and finish work on it. I also intend to look at raw SQL mode (#1042) soon. |
| for (int i = 0; i < stmtNames.Count; i++) | ||
| { | ||
| var query = String.Format("DEALLOCATE \"{0}\";", stmtNames[i]); | ||
| PrependInternalMessage(QueryMessage.Populate(query)); |
There was a problem hiding this comment.
Note that this line fails if there are many deallocations, i.e. if there are enough to fill the 8192 byte send buffer.
|
@MarkusSintonen do you have any comments on my implementation, will you have time to review? Or should I go ahead and merge? |
|
I'll move the conversation to your pull request at #1204 |
Prepared statements can now be made to persist across pooled connection Close/Open. This can provide a significant performance boost in applications with short-lived connections and complex queries that take long to plan. Based on significant work by @MarkusSintonen in npgsql#1084 (thanks!) Fixes npgsql#483
Prepared statements can now be made to persist across pooled connection Close/Open. This can provide a significant performance boost in applications with short-lived connections and complex queries that take long to plan. Based on significant work by @MarkusSintonen in #1084 (thanks!) Fixes #483
Prepared statements can now be made to persist across pooled connection Close/Open. This can provide a significant performance boost in applications with short-lived connections and complex queries that take long to plan. Based on significant work by @MarkusSintonen in #1084 (thanks!) Fixes #483
|
Replaced by adf1c0b |


Implementation for #483