You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
(Re-)implement multi-queries in the extended protocol #459
To optimize for the common queries (single queries), those should probably just be written as
P/B/D/E/S
but other queries (multi queries) should be written as
P/B/P/B.../D/E/C/D/E/C../S with unnamed statements but named portals.
Note that close messages are actually only needed if the user starts an explicit transaction, since named portals last all the way until the end of the current transaction (otherwise Sync closes the transaction). But we can't know that, so we probably have to include them. I'm not sure however what happens if some early Execute message reports an error (then the rest of the Close messages will be ignored). But that probably doesn't matter since a failed transaction is likely to be closed very soon anyway.
What do you think?
To optimize for the common queries (single queries), those should probably just be written as P/B/D/E/S
Note that currently single queries are written as P/D/B/E/S, i.e. with a Describe being sent on the statement and not on the portal. The downside is that the resulting DescriptionRow doesn't have the correct Binary/Text indicator, so we have to fix that up. However, this makes the code more consistent with the prepared flow, which is P/D, and then B/E/S cycles for each execution. Any comment on that?
but other queries (multi queries) should be written as
P/B/P/B.../D/E/C/D/E/C../S with unnamed statements but named portals.
Yep, I think so.
Note that close messages are actually only needed if the user starts an explicit transaction, since named portals last all the way until the end of the current transaction (otherwise Sync closes the transaction). But we can't know that, so we probably have to include them. I'm not sure however what happens if some early Execute message reports an error (then the rest of the Close messages will be ignored). But that probably doesn't matter since a failed transaction is likely to be closed very soon anyway.
What do you think?
If I remember correctly, we said that multi-queries have to provide transaction semantics, i.e. if the second query bombs the first should be rolled back. This means we have to wrap the multi-query in an (implicit) transaction, but only if a transaction isn't already in progress (we can know this from the last ReadyForQuery message we got). If we go down this route, the named portals will be closed anyway at the end of the transaction so we don't have to do anything special, right?
If I remember correctly, we said that multi-queries have to provide transaction semantics, i.e. if the second query bombs the first should be rolled back. This means we have to wrap the multi-query in an (implicit) transaction, but only if a transaction isn't already in progress (we can know this from the last ReadyForQuery message we got). If we go down this route, the named portals will be closed anyway at the end of the transaction so we don't have to do anything special, right?
Note that a BEGIN or COMMIT can happen inside a multi-query. That is the problem...
Note that currently single queries are written as P/D/B/E/S, i.e. with a Describe being sent on the statement and not on the portal. The downside is that the resulting DescriptionRow doesn't have the correct Binary/Text indicator, so we have to fix that up. However, this makes the code more consistent with the prepared flow, which is P/D, and then B/E/S cycles for each execution. Any comment on that?
Aha.. A "problem" is that you get the worthless ParameterDescription message you have to throw away. Is it hard to change it to P/B/D/E/S?
If I remember correctly, we said that multi-queries have to provide transaction semantics, i.e. if the second query bombs the first should be rolled back. This means we have to wrap the multi-query in an (implicit) transaction, but only if a transaction isn't already in progress (we can know this from the last ReadyForQuery message we got). If we go down this route, the named portals will be closed anyway at the end of the transaction so we don't have to do anything special, right?
Note that a BEGIN or COMMIT can happen inside a multi-query. That is the problem...
Yeah, that's a problem - but it seems to be the only way to ensure transaction semantics for multiqueries, isn't it? It seems like the least bad solution here, if the user attempts to do BEGIN/COMMIT inside a multiquery (which seems weird?) they get an error...
Unless you have another idea...
Note that currently single queries are written as P/D/B/E/S, i.e. with a Describe being sent on the statement and not on the portal. The downside is that the resulting DescriptionRow doesn't have the correct Binary/Text indicator, so we have to fix that up. However, this makes the code more consistent with the prepared flow, which is P/D, and then B/E/S cycles for each execution. Any comment on that?
Aha.. A "problem" is that you get the worthless ParameterDescription message you have to throw away. Is it hard to change it to P/B/D/E/S?
It's not a problem at all, I was thinking about it. However, in this case, in the prepared statement flow we only send a Parse when preparing. Then, on the first Bind we need to be aware that we still don't have a description row, and add a Describe there. This isn't a bad idea, it eliminates the useless ParameterDescription and also the fixup (which sets the binary/text). Any objections or more thoughts?
Yeah, that's a problem - but it seems to be the only way to ensure transaction semantics for multiqueries, isn't it? It seems like the least bad solution here, if the user attempts to do BEGIN/COMMIT inside a multiquery (which seems weird?) they get an error...
Unless you have another idea...
I'm not sure what you're thinking about...
I think queries such as
BEGIN;
do some stuff1;
do some stuff2;
END;
can be quite common. But as you say, I think it's quite uncommon you have a multiquery with BEGIN without END... Single queries with only BEGIN is probably more common.
It's not a problem at all, I was thinking about it. However, in this case, in the prepared statement flow we only send a Parse when preparing. Then, on the first Bind we need to be aware that we still don't have a description row, and add a Describe there. This isn't a bad idea, it eliminates the useless ParameterDescription and also the fixup (which sets the binary/text). Any objections or more thoughts?
Do you mean prepared statements are run as P/S at prepare, and B/D/E/S at execution?
And for simple queries we just run P/B/D/E/S?
One question regarding transactions and the extended protocol, to make sure I understand... The docs say:
At completion of each series of extended-query messages, the frontend should issue a Sync message. This parameterless message causes the backend to close the current transaction if it's not inside a BEGIN/COMMIT transaction block
This seems to indicate that a multiquery is automatically wrapped in an implicit transaction (if it isn't already part of an explicit one), but I think you said this isn't the case? What I mean is, if you send P/B/P/B/D/E/C/D/E/C/S without being in an external explicit transaction, and the second command fails, is the first one rolled back?
If the above isn't true (PG doesn't do transaction semantics for us), then we have two options:
Do nothing implicitly. This means multiqueries don't automatically have transaction semantics (if the second query fails the first still executed).
Implicitly wrap the multiquery in a transaction in Npgsql. As discussed above this fails if the user explicitly does BEGIN.
I don't currently see a third option... Since I can't see the transactional semantics of multiqueries grounded in any specs or documentation, we can decide it's not really mandatory and go with option 1. That's where I'm leaning now, also because in general I don't like implicit stuff happening and think Npgsql should be as simple as possible... Let me know what you think.
Do you mean prepared statements are run as P/S at prepare, and B/D/E/S at execution?
And for simple queries we just run P/B/D/E/S?
That's it, but note that the second execution of the same prepared statement can be optimized to not send the describe (so only B/E/S), since the description row from the previous bind is still valid. The two advantages are the elimination of the ParameterDescription and the text/binary fixup.
One last thing about what you originally wrote:
Note that close messages are actually only needed if the user starts an explicit transaction, since named portals last all the way until the end of the current transaction (otherwise Sync closes the transaction). But we can't know that, so we probably have to include them. I'm not sure however what happens if some early Execute message reports an error (then the rest of the Close messages will be ignored). But that probably doesn't matter since a failed transaction is likely to be closed very soon anyway.
What do you think?
You're right, and we can know whether we're in an explicit transaction by looking at ReadyForQuery which tells you what the transaction state is. The connector would always have an enum field describing its transaction state, and it would be updated every time an RFQ is seen. Based on this we can send Close or not for multiquery named portals. On the other hand, I'm not really sure it's worth the bother - we can also always send them for multiquery named portals, the difference is negligible isn't it?
This seems to indicate that a multiquery is automatically wrapped in an implicit transaction (if it isn't already part of an explicit one), but I think you said this isn't the case? What I mean is, if you send P/B/P/B/D/E/C/D/E/C/S without being in an external explicit transaction, and the second command fails, is the first one rolled back?
Correct. If the second one fails, the first one is rolled back. It has exactly the same semantics as running the simple query protocol with multiple queries separated by semicolon.
That's it, but note that the second execution of the same prepared statement can be optimized to not send the describe (so only B/E/S), since the description row from the previous bind is still valid. The two advantages are the elimination of the ParameterDescription and the text/binary fixup.
OK ;)
You're right, and we can know whether we're in an explicit transaction by looking at ReadyForQuery which tells you what the transaction state is. The connector would always have an enum field describing its transaction state, and it would be updated every time an RFQ is seen. Based on this we can send Close or not for multiquery named portals. On the other hand, I'm not really sure it's worth the bother - we can also always send them for multiquery named portals, the difference is negligible isn't it?
You mean if we receive a ReadyForQuery with the transaction flag active after processing a multi query, we should the next time we send a query to the database first send some close messages first that closes the previous query?
This seems to indicate that a multiquery is automatically wrapped in an implicit transaction (if it isn't already part of an explicit one), but I think you said this isn't the case? What I mean is, if you send P/B/P/B/D/E/C/D/E/C/S without being in an external explicit transaction, and the second command fails, is the first one rolled back?
Correct. If the second one fails, the first one is rolled back. It has exactly the same semantics as running the simple query protocol with multiple queries separated by semicolon.
Great! So we don't have to do anything, and get the exact same transaction semantics as with simple query. I somehow thought this didn't happen when sending multiple queries in the extended protocol. I guess there's nothing more to say here.
You're right, and we can know whether we're in an explicit transaction by looking at ReadyForQuery which tells you what the transaction state is. The connector would always have an enum field describing its transaction state, and it would be updated every time an RFQ is seen. Based on this we can send Close or not for multiquery named portals. On the other hand, I'm not really sure it's worth the bother - we can also always send them for multiquery named portals, the difference is negligible isn't it?
You mean if we receive a ReadyForQuery with the transaction flag active after processing a multi query, we should the next time we send a query to the database first send some close messages first that closes the previous query?
I meant that if we go into a multiquery and an explicit transaction is not active (via the RFQ from the previous statement), we don't have to send Close messages since the Sync at the end will close them anyway.
But then I'm not sure it's worth the bother of actually checking for the transaction status, we can just always send Close for all named portals in multi-queries, regardless of whether they're in an explicit transaction or not. In other words, the optimization of not sending Close for multi-queries not wrapped in an explicit transaction seems negligible...
Aside from all the above, I think we've solved all the problems with this, right? And just to avoid the enum situation again, I plan to work on this soon :)
I meant that if we go into a multiquery and an explicit transaction is not active (via the RFQ from the previous statement), we don't have to send Close messages since the Sync at the end will close them anyway.
It is not the previous transaction state that is important, but the current one. So if a user runs BEGIN inside a multi-query, and we noticed that in the previous query, there was no transaction, so we don't close when we should, because now we have a transaction.
But then I'm not sure it's worth the bother of actually checking for the transaction status, we can just always send Close for all named portals in multi-queries, regardless of whether they're in an explicit transaction or not. In other words, the optimization of not sending Close for multi-queries not wrapped in an explicit transaction seems negligible...
Yes, seems reasonable.
Aside from all the above, I think we've solved all the problems with this, right? And just to avoid the enum situation again, I plan to work on this soon :)
Great ;)
Are you planning for the moment to just set up a limit of number of subqueries, or doing the full chunking thing where we send a batch of some thousands until a matching NextResult()?
Are you planning for the moment to just set up a limit of number of subqueries, or doing the full chunking thing where we send a batch of some thousands until a matching NextResult()?
I think that for now, a limit over the number of subqueries is fine, we can always improve it with batching if users complain (I would be surprised but let's wait and see).
As for the limit, I don't remember what number we said before... We need to make sure that our buffer can hold all the ParseComplete and BindComplete responses the server sends (plus one RowDescription). That's 10 bytes per pair (i.e. query). Does 5000 sound like a good limit? That translates to 6250 bytes for all pairs...
Interesting problem: SQLClient actually allows you to prepare a multiquery and rebind parameters to it (did Npgsql support this up to now?). Supporting this probably means that a single prepared NpgsqlCommand must own several backend prepared statements, and also probably description rows. The PG messages would be something like:
Prepare: P/P/S
Execute 1st time: B/D/E/B/D/E/S (portals are anonymous)
Execute 2nd time: B/E/B/E/S (descriptions already stored at frontend)
Npgsql has not supported prepared multiqueries (at least with protocol v3, don't know about v2).
SqlClient has no problem with supporting prepared multiqueries, since their protocol supports it. I don't think however they cache row descriptions. In case you didn't know, for the latest versions of SQL Server, the cmd.Prepare() call does not do anything on the network, instead it sets a flag that the first time the command is executed, it should be prepared.
If you manage to support prepared multiqueries, that would be a nice enhancement ;)
You could for a first version ignore the caching maybe, if that's complicated...
Thanks for the info @Emill. I think that if we do implement prepared multiqueries, the added work of caching descriptions is probably not a lot. This is because we have to track multiple backend prepared statements anyway, so we can also track multiple row descriptions?
Anyway, I'll take a stab at it and we'll see how it goes...
@Emill, just pushed an implementation for multiqueries. The code is a little bit messy and there's room for refactoring/cleanup, I'm waiting until the end of the flow/async work to do that.
We now support prepared multiqueries and even multiqueries with parameters.
Note that I ended up doing P/D/B/E/S and not P/B/D/E/S, despite the above. The reason is that it's much simpler to have always have all the DescriptionRows before you start the execute phase; you can cache them for prepared statements and hand them off to the reader on each execute, etc. The only downside is the useless ParameterDescription message and the need to fixup the DescriptionRows for binary/text format (it seemed like a good tradeoff).
I hope you'll have some time to look at my bugs :)
Activity
Emill commentedon Jan 19, 2015
To optimize for the common queries (single queries), those should probably just be written as
P/B/D/E/S
but other queries (multi queries) should be written as
P/B/P/B.../D/E/C/D/E/C../S with unnamed statements but named portals.
Note that close messages are actually only needed if the user starts an explicit transaction, since named portals last all the way until the end of the current transaction (otherwise Sync closes the transaction). But we can't know that, so we probably have to include them. I'm not sure however what happens if some early Execute message reports an error (then the rest of the Close messages will be ignored). But that probably doesn't matter since a failed transaction is likely to be closed very soon anyway.
What do you think?
roji commentedon Jan 19, 2015
Note that currently single queries are written as P/D/B/E/S, i.e. with a Describe being sent on the statement and not on the portal. The downside is that the resulting DescriptionRow doesn't have the correct Binary/Text indicator, so we have to fix that up. However, this makes the code more consistent with the prepared flow, which is P/D, and then B/E/S cycles for each execution. Any comment on that?
Yep, I think so.
If I remember correctly, we said that multi-queries have to provide transaction semantics, i.e. if the second query bombs the first should be rolled back. This means we have to wrap the multi-query in an (implicit) transaction, but only if a transaction isn't already in progress (we can know this from the last ReadyForQuery message we got). If we go down this route, the named portals will be closed anyway at the end of the transaction so we don't have to do anything special, right?
Emill commentedon Jan 19, 2015
Note that a BEGIN or COMMIT can happen inside a multi-query. That is the problem...
Aha.. A "problem" is that you get the worthless ParameterDescription message you have to throw away. Is it hard to change it to P/B/D/E/S?
roji commentedon Jan 20, 2015
Yeah, that's a problem - but it seems to be the only way to ensure transaction semantics for multiqueries, isn't it? It seems like the least bad solution here, if the user attempts to do BEGIN/COMMIT inside a multiquery (which seems weird?) they get an error...
Unless you have another idea...
It's not a problem at all, I was thinking about it. However, in this case, in the prepared statement flow we only send a Parse when preparing. Then, on the first Bind we need to be aware that we still don't have a description row, and add a Describe there. This isn't a bad idea, it eliminates the useless ParameterDescription and also the fixup (which sets the binary/text). Any objections or more thoughts?
Emill commentedon Jan 20, 2015
I'm not sure what you're thinking about...
I think queries such as
can be quite common. But as you say, I think it's quite uncommon you have a multiquery with BEGIN without END... Single queries with only BEGIN is probably more common.
Do you mean prepared statements are run as P/S at prepare, and B/D/E/S at execution?
And for simple queries we just run P/B/D/E/S?
roji commentedon Jan 20, 2015
One question regarding transactions and the extended protocol, to make sure I understand... The docs say:
This seems to indicate that a multiquery is automatically wrapped in an implicit transaction (if it isn't already part of an explicit one), but I think you said this isn't the case? What I mean is, if you send P/B/P/B/D/E/C/D/E/C/S without being in an external explicit transaction, and the second command fails, is the first one rolled back?
If the above isn't true (PG doesn't do transaction semantics for us), then we have two options:
I don't currently see a third option... Since I can't see the transactional semantics of multiqueries grounded in any specs or documentation, we can decide it's not really mandatory and go with option 1. That's where I'm leaning now, also because in general I don't like implicit stuff happening and think Npgsql should be as simple as possible... Let me know what you think.
That's it, but note that the second execution of the same prepared statement can be optimized to not send the describe (so only B/E/S), since the description row from the previous bind is still valid. The two advantages are the elimination of the ParameterDescription and the text/binary fixup.
One last thing about what you originally wrote:
You're right, and we can know whether we're in an explicit transaction by looking at ReadyForQuery which tells you what the transaction state is. The connector would always have an enum field describing its transaction state, and it would be updated every time an RFQ is seen. Based on this we can send Close or not for multiquery named portals. On the other hand, I'm not really sure it's worth the bother - we can also always send them for multiquery named portals, the difference is negligible isn't it?
Emill commentedon Jan 20, 2015
Correct. If the second one fails, the first one is rolled back. It has exactly the same semantics as running the simple query protocol with multiple queries separated by semicolon.
OK ;)
You mean if we receive a ReadyForQuery with the transaction flag active after processing a multi query, we should the next time we send a query to the database first send some close messages first that closes the previous query?
roji commentedon Jan 20, 2015
Great! So we don't have to do anything, and get the exact same transaction semantics as with simple query. I somehow thought this didn't happen when sending multiple queries in the extended protocol. I guess there's nothing more to say here.
I meant that if we go into a multiquery and an explicit transaction is not active (via the RFQ from the previous statement), we don't have to send Close messages since the Sync at the end will close them anyway.
But then I'm not sure it's worth the bother of actually checking for the transaction status, we can just always send Close for all named portals in multi-queries, regardless of whether they're in an explicit transaction or not. In other words, the optimization of not sending Close for multi-queries not wrapped in an explicit transaction seems negligible...
Aside from all the above, I think we've solved all the problems with this, right? And just to avoid the enum situation again, I plan to work on this soon :)
Emill commentedon Jan 21, 2015
It is not the previous transaction state that is important, but the current one. So if a user runs BEGIN inside a multi-query, and we noticed that in the previous query, there was no transaction, so we don't close when we should, because now we have a transaction.
Yes, seems reasonable.
Great ;)
Are you planning for the moment to just set up a limit of number of subqueries, or doing the full chunking thing where we send a batch of some thousands until a matching NextResult()?
roji commentedon Jan 21, 2015
I think that for now, a limit over the number of subqueries is fine, we can always improve it with batching if users complain (I would be surprised but let's wait and see).
As for the limit, I don't remember what number we said before... We need to make sure that our buffer can hold all the ParseComplete and BindComplete responses the server sends (plus one RowDescription). That's 10 bytes per pair (i.e. query). Does 5000 sound like a good limit? That translates to 6250 bytes for all pairs...
roji commentedon Jan 25, 2015
Interesting problem: SQLClient actually allows you to prepare a multiquery and rebind parameters to it (did Npgsql support this up to now?). Supporting this probably means that a single prepared NpgsqlCommand must own several backend prepared statements, and also probably description rows. The PG messages would be something like:
Prepare: P/P/S
Execute 1st time: B/D/E/B/D/E/S (portals are anonymous)
Execute 2nd time: B/E/B/E/S (descriptions already stored at frontend)
Comments? Am I missing something?
Emill commentedon Jan 25, 2015
Npgsql has not supported prepared multiqueries (at least with protocol v3, don't know about v2).
SqlClient has no problem with supporting prepared multiqueries, since their protocol supports it. I don't think however they cache row descriptions. In case you didn't know, for the latest versions of SQL Server, the cmd.Prepare() call does not do anything on the network, instead it sets a flag that the first time the command is executed, it should be prepared.
If you manage to support prepared multiqueries, that would be a nice enhancement ;)
You could for a first version ignore the caching maybe, if that's complicated...
roji commentedon Jan 25, 2015
Thanks for the info @Emill. I think that if we do implement prepared multiqueries, the added work of caching descriptions is probably not a lot. This is because we have to track multiple backend prepared statements anyway, so we can also track multiple row descriptions?
Anyway, I'll take a stab at it and we'll see how it goes...
roji commentedon Feb 3, 2015
@Emill, just pushed an implementation for multiqueries. The code is a little bit messy and there's room for refactoring/cleanup, I'm waiting until the end of the flow/async work to do that.
We now support prepared multiqueries and even multiqueries with parameters.
Note that I ended up doing P/D/B/E/S and not P/B/D/E/S, despite the above. The reason is that it's much simpler to have always have all the DescriptionRows before you start the execute phase; you can cache them for prepared statements and hand them off to the reader on each execute, etc. The only downside is the useless ParameterDescription message and the need to fixup the DescriptionRows for binary/text format (it seemed like a good tradeoff).
I hope you'll have some time to look at my bugs :)