Open davidmartos96 opened 7 months ago
42601: cannot insert multiple commands into a prepared statement
This is coming from the postgresql server, and apparently it is coming from the extended query protocol's prepare phase.
I don't really know how this was implemented in v2, it had a non-trivial complexity around queries that I never ventured into. Maybe it converted it to simple query protocol - I am just guessing here.
In v3 we are doing mostly the minimum over the query protocols, in this case the extended query protocol (which is usually more efficient). If the server rejects the queries, my best guess is that you rewrite them into:
;\n INSERT INTO todos (id, description) VALUES
string with ,
)./cc @simolus3 for further comments
Maybe it converted it to simple query protocol - I am just guessing here.
Yeah, v2 essentially inlined the variables into the SQL string. If you want to do that yourself, you can set queryMode: QueryMode.simple
on execute
and use SQL without variables.
My opinion is that this package shouldn't do these kinds of tricks, there probably are a lot of edge cases around typing that we'll get slightly wrong, and those issues are very tricky for users to diagnose.
(1) separate queries but reusing their prepared state with https://pub.dev/documentation/postgres/latest/postgres/Session/prepare.html
This is perhaps something we should investigate further. At the moment, running a prepared statement essentially locks the connection until we get a ReadyForQueryMessage
(see the _scheduleStatement
call in the _PgResultStreamSubscription
constructor). This means that running n prepared statements requires a sequence of n roundtrips without opportunities for interleaving or concurrency.
This will complicate the state machine in this package a bit, but I think statement responses are always coming back in-order, so we could keep a queue of outstanding statements and issue all requests at once without waiting for responses. That would reduce the overall latency when batching statements.
@simolus3 That's interesting, I didn't know it was binding the values as literals. My use case for these fortunately are inserts, which we can refactor to use a single one with multiple values. Thank you!
Feel free to keep it open with a different title or close.
Hello! I was in the process of migrating a project using
postgres
v2. One thing we are doing in this project to improve the performance of batch uploads to Postgres is to run multiple INSERT statements in the sameexecute
and it has been working great for us.The problem we are seeing is that in v3 it throws with an error:
Severity.error 42601: cannot insert multiple commands into a prepared statement
Are we missing some special configuration to support this?
Sample code:
Version 2.x
Version 3.x