Psycopg2: Problem with new CALL statements?

Created on 5 Mar 2019  ·  2Comments  ·  Source: psycopg/psycopg2

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.

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiamo picture jiamo  ·  3Comments

camillehuot picture camillehuot  ·  4Comments

rofi93 picture rofi93  ·  4Comments

vitorpontual picture vitorpontual  ·  5Comments

ProgVal picture ProgVal  ·  3Comments