Problem while handling server restart with postgresql 8.4.12

Issue #2570 resolved
Former user created an issue

(original reporter: abdelrahman) I have a system where I am upgrading my postgresql from version 8.4.3 to 8.4.12 while my sqlachamy version is 0.6.4 which i also update to version 0.7.8. I had a problem that the sqlachamy was not able to handle the server restart anymore where any requests from my session if the restart fails with:

(InterfaceError) connection already closed None None

I had a debugged the problem for a bit and it seems that the exception msg raised by the postgresql server when its shutdown is changes from 8.4.3 to 8.4.12, where in 8.4.3 it was

OperationalError('terminating connection due to administrator command\nserver closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n\tbefore or while processing the request.\n',)

while the msg in 8.4.12 is

OperationalError('terminating connection due to administrator command\nFATAL:  terminating connection due to administrator command\n',)

in the psycopg2.py in both the latest stable version and the development version of sqlalchamy, the method check if the connection is lost is:

def is_disconnect(self, e, connection, cursor):
        if isinstance(e, self.dbapi.OperationalError):
            # these error messages from libpq: interfaces/libpq/fe-misc.c.
            # TODO: these are sent through gettext in libpq and we can't
            # check within other locales - consider using connection.closed
            return 'closed the connection' in str(e) or \
                    'connection not open' in str(e) or \
                    'could not receive data from server' in str(e)
        elif isinstance(e, self.dbapi.InterfaceError):
            # psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h
            return 'connection already closed' in str(e) or \
                    'cursor already closed' in str(e)
        elif isinstance(e, self.dbapi.ProgrammingError):
            # not sure where this path is originally from, it may
            # be obsolete.   It really says "losed", not "closed".
            return "losed the connection unexpectedly" in str(e)
        else:
            return False

Notice that when the OperationalError is raised(which is the one raised on server shutdown), the exception message is parsed to match of the expected error msgs which does not match in postgresql 8.4.12

Comments (5)

  1. Mike Bayer repo owner
    • changed component to postgres
    • changed milestone to 0.7.9
    • assigned issue to

    can you confirm this patch fixes please:

    diff -r 4af7743ce70bce50a8630ba91060101fc941281c lib/sqlalchemy/dialects/postgresql/psycopg2.py
    --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py    Sun Sep 23 22:14:17 2012 -0400
    +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py    Mon Sep 24 10:22:33 2012 -0400
    @@ -373,7 +373,8 @@
                 # these error messages from libpq: interfaces/libpq/fe-misc.c.
                 # TODO: these are sent through gettext in libpq and we can't
                 # check within other locales - consider using connection.closed
    -            return 'closed the connection' in str(e) or \
    +            return 'terminating connection' in str(e) or \
    +                    'closed the connection' in str(e) or \
                         'connection not open' in str(e) or \
                         'could not receive data from server' in str(e)
             elif isinstance(e, self.dbapi.InterfaceError):
    
  2. Former user Account Deleted

    (original author: abdelrahman) yes i tested the fix and it seems to fix the problem. Thank you for the quick response

  3. Log in to comment