ReadyForQuery (B)
Byte1('Z')
Identifies the message type. ReadyForQuery is sent whenever the backend is ready for a new query cycle.
Int32(5)
Length of message contents in bytes, including self.
Byte1
Current backend transaction status indicator. Possible values are 'I' if idle (not in a transaction block); 'T' if in a transaction block; or 'E' if in a failed transaction block (queries will be rejected until block is ended).
In [21]: con = psycopg2.connect(database="postgres", user="postgres", host="localhost")
In [22]: c = con.cursor()
In [23]: c.execute("BEGIN")
In [24]: c.execute("SELECT 1/0")---------------------------------------------------------------------------DataError Traceback (most recent call last)<ipython-input-24-1b5c505f414d> in <module>()----> 1 c.execute("SELECT 1/0")
DataError: division by zero
In [25]: c.execute("SELECT 1")---------------------------------------------------------------------------InternalError Traceback (most recent call last)<ipython-input-25-261af617f27d> in <module>()----> 1 c.execute("SELECT 1")
InternalError: current transaction is aborted, commands ignored until end of transaction block
In [26]: c.statusmessageOut[26]: ''
In [27]: c.execute("COMMIT")
In [28]: c.statusmessageOut[28]: 'ROLLBACK'
In [29]: c.execute("SELECT 1")
In [30]: c.fetchall()Out[30]: [(1,)]
CommandComplete (B)
Byte1('C')
Identifies the message as a command-completed response.
Int32
Length of message contents in bytes, including self.
String
The command tag. This is usually a single word that identifies which SQL command was completed.
For an INSERT command, the tag is INSERT oid rows, where rows is the number of rows inserted. oid is the object ID of the inserted row if rows is 1 and the target table has OIDs; otherwise oid is 0.
For a DELETE command, the tag is DELETE rows where rows is the number of rows deleted.
For an UPDATE command, the tag is UPDATE rows where rows is the number of rows updated.
For a SELECT or CREATE TABLE AS command, the tag is SELECT rows where rows is the number of rows retrieved.
For a MOVE command, the tag is MOVE rows where rows is the number of rows the cursor's position has been changed by.
For a FETCH command, the tag is FETCH rows where rows is the number of rows that have been retrieved from the cursor.
For a COPY command, the tag is COPY rows where rows is the number of rows copied. (Note: the row count appears only in PostgreSQL 8.2 and later.)
1> {ok, C} = epgsql:connect(#{host => "localhost", username => "postgres", password => "postgres", port => 5433}).
{ok,<0.97.0>}
2> epgsql:equery(C, "BEGIN").
{ok,[],[]}
3> epgsql:squery(C, "SELECT 1/0").
{error,{error,error,<<"22012">>,division_by_zero,
<<"division by zero">>,[]}}
4> epgsqli:squery(C, "COMMIT"). %# it is "epgsqli", not "epgsql"
#Ref<0.0.1.632>
5> flush().
Shell got {<0.97.0>,#Ref<0.0.1.632>,{complete,rollback}}
Shell got {<0.97.0>,#Ref<0.0.1.632>,done}
sure_commit(C)->
Ref = epgsqli:squery(C,"COMMIT"),
CompleteCmd = receive {C, Ref, {complete, CompleteCommand}} -> CompleteCommand end,
receive {C, Ref, done} ->ok end,
case CompleteComand of
commit -> ok;
rollback -> {error, rollback}
end.
Hi everyone,