Skip to content

How can I know when Postgre is returning a idle_in_transaction_session_timeout? #988

@dvarrazzo

Description

@dvarrazzo

Originally posted by @b0uh in #987

Hello,

I'm migrating some code from psycopg2 to psycopg3 and it is going well so far, thanks for the hard work.
I have some difficulties around the behavior of idle_in_transaction_session_timeout setting though.

With psycopg3, the error/exception is generic:

import psycopg
from time import sleep

with psycopg.connect("...") as conn:
    with conn.cursor() as cursor:
        cursor.execute("SET SESSION idle_in_transaction_session_timeout = 1000")
        sleep(2)
        cursor.execute("SELECT * from pg_tables")

# Exception raised:
# OperationalError: consuming input failed: server closed the connection unexpectedly
# 	This probably means the server terminated abnormally
# 	before or while processing the request.

With psycopg2, the error/exception is more explicit:

import psycopg2
conn = psycopg2.connect("....")
with conn.cursor() as cursor:
    cursor.execute("SET SESSION idle_in_transaction_session_timeout = 1000")
    sleep(2)
    cursor.execute("SELECT * from pg_tables")
conn.close()

# Exception raised:
# psycopg2.errors.IdleInTransactionSessionTimeout: terminating connection due to idle-in-transaction timeout
# server closed the connection unexpectedly
# 	This probably means the server terminated abnormally
# 	before or while processing the request.

While looking at the code of psycopg3 I noticed the IdleInTransactionSessionTimeout exception which makes me wonder if maybe I missed something.

Does someone has been able to have clear error for idle_in_transaction_session_timeout?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions