V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Soulboy
V2EX  ›  Python

psycopg2 查询 sql 时报错

  •  
  •   Soulboy · 15 天前 · 871 次点击

    V 友们,帮忙给点排查意见

    问题不是必现的,偶尔就出现一次

    问了 AI 给的也是模糊的的回答

    连接 pg 数据库用的是 pool = PooledDB()

    执行代码如下

        def dbQuery(self, sql):
            conn = pool.connection()
            cur = conn.cursor()
            cur.execute(sql)
            result = cur.fetchall()
            cur.close()
            conn.close()
            return result
    

    报错如下:

    could not receive data from server: Software caused connection abort (0x00002745/10053)
    
    Traceback (most recent call last):
      File "aaTestV5.py", line 844, in process
        input_values = self.getInputValuesInfo()
      File "aa_process.py", line 117, in getInputValuesInfo
        order_list = super(tf, self).getInputValuesInfo()
      File "aaTestV5.py", line 555, in getInputValuesInfo
        results_fromno, no_list = self.getFromnoResult()
      File "aa_process.py", line 366, in getFromnoResult
        return self.getFromnoResultInner(False)
      File "aa_process.py", line 320, in getFromnoResultInner
        results_fromno, no_list = super(tf, self).getFromnoResult(True)
      File "aaTestV5.py", line 119, in getFromnoResult
        result_fromno1 = self.dbQuery(query_sql)
      File "aa_process.py", line 391, in dbQuery
        return super(tf, self).dbQuery(sql)
      File "aaTestV5.py", line 823, in dbQuery
        cur.execute(sql)
      File "\python\lib\site-packages\dbutils\steady_db.py", line 598, in tough_method
        result = method(*args, **kwargs)  # try to execute
    psycopg2.DatabaseError: could not receive data from server: Software caused connection abort (0x00002745/10053)
    
    8 条回复    2025-07-17 17:11:18 +08:00
    irisdev
        1
    irisdev  
       14 天前
    偶尔会不会全表查锁表了,超时时间设长点或者加个 no wait 试试
    Soulboy
        2
    Soulboy  
    OP
       14 天前
    @irisdev 应该不是锁表呢,更倾向于网络层面的问题
    Soulboy
        3
    Soulboy  
    OP
       14 天前
    @irisdev 看 pg 数据库日志没有这条 sql 的记录,说明还没有发到服务端
    Ackvincent
        4
    Ackvincent  
       14 天前
    网络问题就 telnet 一下。
    Soulboy
        5
    Soulboy  
    OP
       14 天前
    @Ackvincent 能 ping 通,公司用的是 windows server 2008 ,没有 telnet 命令😢
    spritecn
        6
    spritecn  
       14 天前
    网络连接环境不好 or 连接数没配够, 我们内网开发环境也经常这样,线上就没有任何问题
    Ackvincent
        7
    Ackvincent  
       14 天前
    先确定防火墙放开了对应端口,端口也监听正常。
    bosoncat
        8
    bosoncat  
       14 天前
    psycopg 利用 libpq 连接数据库,错误来源于 https://github.com/postgres/postgres/blob/afa5c365ec5ad978878e5d26c536d8f865abf1ae/src/interfaces/libpq/win32.c#L126
    可以根据错误代码 WSAECONNABORTED [^1] 排查一下 PostgreSQL 所在机器的网络问题

    [^1]: https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2#WSAECONNABORTED
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5633 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 113ms · UTC 02:24 · PVG 10:24 · LAX 19:24 · JFK 22:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.