Skip to content

Commit

Permalink
Add Sync calls to extended query execution
Browse files Browse the repository at this point in the history
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
seth committed Feb 24, 2012
1 parent f24cee4 commit 7b0e011
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/pgsql_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down

3 comments on commit 7b0e011

@mabrek
Copy link

@mabrek mabrek commented on 7b0e011 Feb 24, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Author

@seth seth commented on 7b0e011 Feb 24, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

@mabrek mabrek commented on 7b0e011 Feb 24, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.