-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to Prepare multi-statement #20699
Comments
Interesting - thanks for the report. I wasn't aware that it was possible to prepare compound statements like that. |
I recall the Postgres wire protocol doesn't support multiple statements in a prepared statement. I could be mistaken about that. |
I don't think it's usually possible to prepare multiple statements like that - the For comparison, mysql supports multiple statements like this, but only if you opt in by setting the CLIENT_MULTI_STATEMENTS option on the session. Oracle supports multiple INTO/VALUES clauses on a single INSERT ALL statement, which would allow this to be written as one statement. In current versions of CockroachDB, you could use UNION and INSERT RETURNING to combine both inserts into the same statement. Something like |
I've confirmed Postgres doesn't support this. It's unlikely that we will either. |
Thank you guys for the detailed response, and i'm sorry I did not check the Postgres behavior before posting. I appreciate the SELECT/INSERT/UNION/RETURNING workaround, but it feels a bit hacky... Out of curiosity: If I have a bunch of statements to execute together, and I want to avoid an RTT from client->cockroach for each, is there no smart way to do that? if not, feel free to close this issue, and thanks again for the responses! |
@oliverkofoed this is a known good use case for a user-defined function or procedure. We actually plan to support these at some point (#17511). I'll close this issue for now, and feel free to track #17511 for a more durable solution. Cheers! |
There are (valid?) use cases for this, for example, use cockroachdb session variable for a certain queries, and still want to avoid RTT: set default_transaction_priority = ?; set statement_timeout = ?; select 1; |
Hi @knz What do you think about the use case (above)? UDF/Stored Procedure may not be the answer for adhoc query like this? (mainly because it introduce maintenance cost - unsuitable for start-up?). |
This issue is closed; can you open a new issue to discuss? Or better yet, head to our community Slack. |
FEATURE REQUEST
The database/sql driver lib/pg prepares all statements before executing. This however fails when trying to do two inserts to different tables in one go:
This will panic with:
panic: pq: prepared statement had 2 statements, expected 1 [recovered]
It would be nice-to-have if Cockroach was able to prepare multi-statements so the above would work.
The text was updated successfully, but these errors were encountered: