I'm using psychopg2 (version 2.7.7) with PostgreSQL 11, and using the new stored procedures in PG, and I'm running this code:
conn = psycopg2.connect(connect_str)
cursor = conn.cursor()
statement = "CALL table.procedure();"
cursor.execute(statement)
cursor.close()
conn.close()
I'm getting the following error: "Error:invalid transaction termination
CONTEXT: PL/pgSQL function table.procedure() line 22 at COMMIT"
That same exact statement works just fine in psql - the procedure runs fine. And psycopg2 gives the same error whether or not the semicolon is present.
More information:
We tested a variety of stored procedures, and it appears that a COMMIT statement in the stored procedure is the thing that causes this error to arise. Again, these scripts run fine via psql or PGAdmin, so they are valid procedures.
From PG docs:
If CALL is executed in a transaction block, then the called procedure cannot execute transaction control statements. Transaction control statements are only allowed if CALL is executed in its own transaction.
psycopg connection normally start operation with a BEGIN. if you don't want that you must set the connection in autocommit with conn.autocommit = True.
Most helpful comment
From PG docs:
psycopg connection normally start operation with a BEGIN. if you don't want that you must set the connection in autocommit with
conn.autocommit = True.