title: Psycopg2 Operational Error SSL SYSCALL error: EOF detected
body: <p>We are using python for communication to postgres sqlalchemy database and time to time there occurs error</p>
  
  <pre><code>    ERROR 2017-05-24 16:45:55,072 45610 (psycopg2.OperationalError) SSL SYSCALL error: EOF detected
   [SQL: '\n        SELECT sys.check_permissions(%(username)s, ARRAY[%(permission_0)s]);\n    '] [parameters: {'username': 'miksanikm', 'permission_0': 'searchBrokerContact'}]
  Traceback (most recent call last):
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/spyne/application.py", line 122, in process_request
      self.event_manager.fire_event('method_call', ctx)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/spyne/_base.py", line 442, in fire_event
      handler(ctx)
    File "wsgi.py", line 161, in _on_method_call
      if not ctx.udc.hasPermissions(ctx.udc.method_name):
    File "wsgi.py", line 145, in hasPermissions
      return system_lib.checkPermission(self.username, *args)
    File "./app/system/lib.py", line 126, in checkPermission
      ''' % permissions), args)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute
      return meth(self, multiparams, params)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
      return connection._execute_clauseelement(self, multiparams, params)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
      compiled_sql, distilled_params
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
      context)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
      exc_info
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
      reraise(type(exception), exception, tb=exc_tb, cause=cause)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
      context)
    File "/home/python/projects/allrisk_isac/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
      cursor.execute(statement, parameters)
  </code></pre>
  
  <p>so after this call whole app fails. So it is quite severe error, but I think it's in postgres itself. Do you have any ideas, why is this happening?</p>
  
  <p>EDIT:</p>
  
  <p>Python query code
  PS: If you have tips to edit it to be more readible, tell me</p>
  
  <pre><code>def searchBrokerContact(rcic, name, surname, city, phone, email, id_person_company, id_broker):
      def regExPercent(x):
          if x is None:
              return '%'
          return '%'+x+'%'
      connection = connections['default']
      if len(rcic) &gt; 0:
          condition = u' (pc.ico = :rcic OR pc.rc = :rcic) LIMIT 50'
      else:
          condition = u''' ((lower(pc.name) like lower(:name) or (pc.name IS NULL AND :name IN ('%%', '%')))
          AND (lower(pc.surname) like lower(:surname) OR lower(pc.company_name) like lower(:surname))
          AND (lower(ad.city) like lower(:city) OR (ad.city IS NULL AND :city IN ('%%', '%')))
          AND (regexp_replace(pc.phone_mobile, '[ +]*', '', 'g') like regexp_replace(:phone, '[ +]*', '', 'g') OR (pc.phone_mobile IS NULL AND :phone IN ('%%', '%')))
          AND (pc.email like :email OR (pc.email IS NULL AND :email IN ('%%', '%') )) ) LIMIT 50'''
      query = connection.execute(text(
      u"""
      SELECT DISTINCT
      CASE WHEN pc.organization_type = 10 THEN pc.rc ELSE pc.ico END AS client_icorc,
      CASE
        WHEN pc.organization_type IN (10, 20) THEN
          btrim((((COALESCE(pc.degree, ''::character varying)::text || ' '::text) || COALESCE(pc.name, ''::character varying)::text) || ' '::text) || COALESCE(pc.surname, ''::character varying)::text)
        ELSE
          pc.company_name
      END AS client_name,
      pc.organization_type,
      pc.id_person_company AS id_person_company,
      pc.email AS email,
      pc.phone_mobile AS phone,
      btrim((((((COALESCE(ad.street, ''::character varying)::text || ' '::text) || COALESCE(ad.land_registry_num, ad.house_num, ''::character varying)::text) || ', '::text) || COALESCE(ad.zip, ''::character varying)::text) || ' '::text) || COALESCE(ad.city, ''::character varying)::text) AS address_client,
      CASE
        WHEN ad.id_address = ac.id_address THEN
          CASE WHEN pc.organization_type = 10
          THEN 'shodná s adresou trvalého pobytu'
              ELSE 'shodná s adresou sídla'
          END
        ELSE
          btrim((((((COALESCE(ac.street, ''::character varying)::text || ' '::text) || COALESCE(ac.land_registry_num, ac.house_num, ''::character varying)::text) || ', '::text) || COALESCE(ac.zip, ''::character varying)::text) || ' '::text) || COALESCE(ac.city, ''::character varying)::text)
      END AS address_c,
      pc.degree,
      pc.sex,
      pc.name,
      pc.surname,
      pc.organization_type,
      pc.rc,
      pc.ico,
      pc.bank_account,
      pc.bank_code,
      pc.bank_prefix,
      ad.street,
      ad.house_num,
      ad.land_registry_num,
      ad.city,
      ad.zip,
      ad.id_country,
      ac.street AS street_c,
      ac.house_num AS house_num_c,
      ac.land_registry_num AS land_registry_num_c,
      ac.city AS city_c,
      ac.zip AS zip_c,
      CASE WHEN ac.id_address = ad.id_address THEN false ELSE true END AS has_cor,
      pc.identity_card,
      pc.company_name,
      id.card_from,
      id.card_to,
      id.card_num,
      id.card_authority,
      id.card_type,
      CASE WHEN id.id_identity NOTNULL THEN true ELSE false END AS has_identity,
      bc.created::date
      FROM
          personal.person_company AS pc
          INNER JOIN personal.broker_contact AS bc ON bc.id_person_company = pc.id_person_company
          INNER JOIN personal.address AS ad ON ad.id_address = pc.id_address
          LEFT JOIN personal.address AS ac ON ac.id_address = pc.id_address_mailing
          LEFT JOIN personal.identity AS id ON id.id_identity = pc.id_identity
          LEFT JOIN personal.client AS cl ON cl.id_person_company = pc.id_person_company
      WHERE bc.deleted IS NULL and pc.deleted IS Null AND cl.id_client Is NULL
          AND bc.id_broker = :id_broker
          AND
      """+condition), id_person_company=id_person_company, rcic=rcic, name=regExPercent(name), surname=regExPercent(surname), city=regExPercent(city), phone=regExPercent(phone), email=regExPercent(email), id_broker=id_broker)
      output = []
      for row in query:
          item = ClientListItem(id_person_company=row['id_person_company'],
                                client_name=row['client_name'],
                                client_icorc=row['client_icorc'],
                                address=row['address_client'],
                                address_cor=row['address_c'],
                                phone=row['phone'],
                                email=row['email'],
                                city=row['city'],
                                created=row['created'])
          output.append(item)
      return output
  </code></pre>
  
  <p>It is in restAPI for java here is service.py code</p>
  
  <pre><code>@rpc(ByteArray(default=['']), ByteArray(default=['']), ByteArray(default=['']), ByteArray(default=['']), ByteArray(default=['']), ByteArray(default=['']), Integer(default=None), Integer(default=None), _returns=ClientList)
      def searchBrokerContact(ctx, rcic, name, surname, city, phone, email, id_person_company, id_broker):
          '''
          Service returns list of contacts
          '''
          list= external_contract_lib.searchBrokerContact(rcic[0], name[0], surname[0], city[0], phone[0], email[0], id_person_company, id_broker)
          return ClientList(list=list)
  </code></pre>