Browse files

Add Sync calls to extended query execution

This follows the protocol flow described in
http://www.postgresql.org/docs/9.1/static/protocol-flow.html

  "At completion of each series of extended-query messages, the
  frontend should issue a Sync message."
  • Loading branch information...
1 parent f24cee4 commit 7b0e0118e70d57407ce030f7515182cc44c603d5 @seth seth committed Feb 23, 2012
Showing with 9 additions and 0 deletions.
  1. +9 −0 src/pgsql_connection.erl
View
9 src/pgsql_connection.erl
@@ -460,11 +460,20 @@ executing({$s, <<>>}, State) ->
%% CommandComplete
executing({$C, Bin}, State) ->
+ %% After completing an extended query, the frontend should issue a
+ %% Sync message according to the PostgreSQL protocol flow docs
+ %% (http://www.postgresql.org/docs/9.1/static/protocol-flow.html)
+ %% in the extended query section. FIXME: any reason for send,
+ %% notify vs notify, send? Since the communication is all async, I
+ %% don't think it really matters, but seems to me to make more
+ %% sense to send the sync before notify.
+ send(State, $S, []),
notify(State, {complete, decode_complete(Bin)}),
{next_state, ready, State};
%% EmptyQueryResponse
executing({$I, _Bin}, State) ->
+ send(State, $S, []),
notify(State, {complete, empty}),
{next_state, ready, State};

3 comments on commit 7b0e011

@mabrek

not every Execute requires Sync, you can send several Binds/Execute and then one Sync.
from the linked documentation
"The purpose of Sync is to provide a resynchronization point for error recovery. When an error is detected while processing any extended-query message, the backend issues ErrorResponse, then reads and discards messages until a Sync is reached, then issues ReadyForQuery and returns to normal message processing."

@seth
Chef Software, Inc. member

Thank you for reading and commenting. Rereading the docs again I see where I got confused. A very common use for us is to bind and execute a single query. I think I will take a look at extending the pgsql:equery path to support such single query execution, but with reuse of a prepared query.

@mabrek

I'm thinking about making something similar in my asynchronous fork. There is execute_batch function (https://github.com/mabrek/epgsql/blob/async/README#L204) that sends one or more Bind/Execute and one Sync.

Please sign in to comment.