【问题标题】:Too many PostgreSQL processes running (using Django to write to DB)运行的 PostgreSQL 进程过多(使用 Django 写入数据库)
【发布时间】:2013-01-28 19:07:16
【问题描述】:

我正在使用 Django 写入服务器上的 Postgres 数据库。服务器每 15 分钟接收大约 800 个插入请求。最近,当我在服务器上运行 ps aux 时,这是我看到的 postgres 进程数:

postgres  5721  0.0  0.6 102380 27372 ?        Ss   Jan27   0:04 postgres: writer process                                                      
postgres  5722  0.0  0.0 102280  1684 ?        Ss   Jan27   0:01 postgres: wal writer process                                                  
postgres  5723  0.0  0.0 102416  1800 ?        Ss   Jan27   0:03 postgres: autovacuum launcher process                                         
postgres  5724  0.0  0.0  73828  1480 ?        Ss   Jan27   0:12 postgres: stats collector process                                             
root      8621  0.0  0.2 107912  8736 ?        Ss   Jan27   0:03 /usr/sbin/apache2 -k start
www-data  8628  0.0  0.0 104444  3096 ?        S    Jan27   0:00 /usr/sbin/apache2 -k start
www-data  8629  0.0  0.0 104444  2908 ?        S    Jan27   0:00 /usr/sbin/fcgi-pm -k start
www-data  8630  0.0  0.0 105620  2976 ?        S    Jan27   0:00 /usr/sbin/apache2 -k start
www-data 29332  1.2  1.1 471932 47432 ?        Sl   18:45   0:08 /usr/sbin/apache2 -k start
www-data 29350  1.0  1.2 468712 49372 ?        Sl   18:45   0:07 /usr/sbin/apache2 -k start
www-data 29351  1.2  1.2 541820 51836 ?        Sl   18:45   0:08 /usr/sbin/apache2 -k start
postgres 29386  0.1  0.7 104932 31680 ?        Ss   18:45   0:00 postgres: abs ap 127.0.0.1(48450) INSERT                 
postgres 29401  0.1  0.7 104932 29784 ?        Ss   18:45   0:00 postgres: abs ap 127.0.0.1(48463) INSERT                 
postgres 29407  0.1  0.7 105060 29888 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48467) COMMIT                 
postgres 29408  0.1  0.7 104932 31852 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48468) INSERT                 
www-data 29409  1.2  1.0 467728 42204 ?        Sl   18:46   0:07 /usr/sbin/apache2 -k start
postgres 29428  0.1  0.7 105060 31972 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48470) COMMIT                 
postgres 29433  0.1  0.7 104932 31788 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48474) INSERT                 
postgres 29434  0.1  0.7 104932 31908 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48475) COMMIT                 
postgres 29435  0.1  0.7 104932 31836 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48476) COMMIT                 
postgres 29439  0.1  0.7 104932 31736 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48480) COMMIT                 
postgres 29440  0.1  0.7 105060 31352 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48481) COMMIT                 
postgres 29441  0.1  0.7 105056 31960 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48482) COMMIT                 
postgres 29443  0.1  0.7 104932 31248 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48484) COMMIT                 
postgres 29444  0.1  0.7 104932 31976 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48485) COMMIT                 
www-data 29445  1.0  1.0 470256 44348 ?        Sl   18:46   0:06 /usr/sbin/apache2 -k start
postgres 29487  0.1  0.7 104932 32012 ?        Ss   18:46   0:00 postgres: abs ap 127.0.0.1(48487) COMMIT                 
postgres 29489  0.1  0.7 105060 31480 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48489) INSERT                 
postgres 29491  0.1  0.7 104932 31788 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48490) INSERT                 
postgres 29492  0.1  0.7 104932 31944 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48491) INSERT                 
postgres 29496  0.1  0.7 104932 31260 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48493) COMMIT                 
postgres 29499  0.1  0.7 105056 32088 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48495) COMMIT                 
postgres 29502  0.1  0.7 105060 31936 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48497) INSERT                 
postgres 29506  0.1  0.7 104932 31916 ?        Ss   18:47   0:00 postgres: abs ap 127.0.0.1(48501) INSERT                 
postgres 29518  0.1  0.7 104932 31736 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48511) COMMIT                 
postgres 29523  0.1  0.7 104932 31524 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48512) INSERT                 
postgres 29525  0.1  0.6 104932 27776 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48514) COMMIT                 
postgres 29526  0.1  0.7 104932 30992 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48515) COMMIT                 
postgres 29531  0.1  0.7 104932 28992 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48516) INSERT                 
postgres 29532  0.1  0.7 104932 29792 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48517) COMMIT                 
postgres 29533  0.1  0.7 104932 30396 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48518) INSERT                 
postgres 29536  0.1  0.7 104932 31028 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48520) INSERT                 
postgres 29537  0.1  0.7 104932 29264 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48521) INSERT                 
postgres 29542  0.1  0.7 104932 29596 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48525) COMMIT                 
postgres 29543  0.1  0.7 104932 30560 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48526) COMMIT                 
postgres 29545  0.1  0.7 104932 30864 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48528) INSERT                 
postgres 29550  0.1  0.7 104932 30964 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48529) COMMIT                 
postgres 29552  0.1  0.7 104932 31012 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48530) COMMIT                 
postgres 29553  0.1  0.7 104932 31260 ?        Ss   18:48   0:00 postgres: abs ap 127.0.0.1(48531) SELECT                 
postgres 29554  0.1  0.7 104932 30976 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48532) INSERT                 
postgres 29556  0.1  0.7 104932 30084 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48534) INSERT                 
postgres 29557  0.1  0.7 104932 30644 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48535) INSERT                 
postgres 29558  0.1  0.7 104932 30408 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48536) INSERT                 
postgres 29559  0.1  0.7 104932 30048 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48537) INSERT                 
postgres 29560  0.1  0.7 104932 30488 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48538) COMMIT                 
postgres 29561  0.1  0.7 104932 29840 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48539) INSERT                 
postgres 29562  0.1  0.7 104932 30616 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48540) COMMIT                 
postgres 29564  0.1  0.7 104932 30456 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48541) INSERT                 
postgres 29565  0.2  0.7 104932 30612 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48542) COMMIT                 
postgres 29566  0.2  0.7 104932 30512 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48543) INSERT                 
postgres 29567  0.2  0.7 104932 30964 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48544) INSERT                 
postgres 29568  0.2  0.7 104932 30460 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48545) INSERT                 
postgres 29569  0.2  0.7 104932 30684 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48546) INSERT                 
postgres 29571  0.2  0.7 104932 31188 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48548) COMMIT                 
postgres 29572  0.2  0.7 104932 31280 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48549) INSERT                 
postgres 29573  0.2  0.7 104932 30632 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48550) COMMIT                 
postgres 29574  0.2  0.7 104932 30784 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48551) INSERT                 
postgres 29575  0.2  0.7 104932 31060 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48552) INSERT                 
postgres 29576  0.2  0.7 104932 31064 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48553) COMMIT                 
postgres 29577  0.2  0.7 105008 31184 ?        Ss   18:49   0:00 postgres: abs ap 127.0.0.1(48554) INSERT                 
postgres 29578  0.2  0.7 105016 31168 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48555) COMMIT                 
postgres 29579  0.2  0.7 105064 30632 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48556) INSERT                 
postgres 29580  0.2  0.7 105016 31208 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48557) COMMIT                 
postgres 29582  0.2  0.7 104932 31268 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48559) COMMIT                 
postgres 29583  0.2  0.7 104936 31348 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48560) COMMIT                 
postgres 29585  0.2  0.7 104932 31280 ?        Ss   18:50   0:00 postgres: abs ap 127.0.0.1(48561) COMMIT                 
postgres 29586  0.2  0.7 104936 31376 ?        Ds   18:50   0:00 postgres: abs ap 127.0.0.1(48562) COMMIT                 
www-data 29587  1.5  0.8 462216 35916 ?        Sl   18:50   0:05 /usr/sbin/apache2 -k start
postgres 29775  0.2  0.7 104932 30528 ?        Ss   18:51   0:00 postgres: abs ap 127.0.0.1(48722) INSERT                 
postgres 29776  0.2  0.7 104936 31180 ?        Ss   18:51   0:00 postgres: abs ap 127.0.0.1(48723) COMMIT                 
postgres 29777  0.2  0.7 104932 31292 ?        Ss   18:51   0:00 postgres: abs ap 127.0.0.1(48724) INSERT                 
postgres 29944  0.2  0.6 105016 28424 ?        Ss   18:51   0:00 postgres: abs ap 127.0.0.1(48890) COMMIT                 
postgres 30128  0.3  0.7 104936 29836 ?        Ss   18:52   0:00 postgres: abs ap 127.0.0.1(49070) INSERT                 
postgres 30129  0.3  0.7 104932 29444 ?        Ss   18:52   0:00 postgres: abs ap 127.0.0.1(49071) INSERT                 
postgres 30225  0.3  0.7 104932 30020 ?        Ss   18:52   0:00 postgres: abs ap 127.0.0.1(49167) INSERT                 
postgres 30301  0.2  0.7 104932 28884 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49242) COMMIT                 
postgres 30302  0.2  0.6 104932 28136 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49243) COMMIT                 
postgres 30305  0.2  0.6 104936 28116 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49245) COMMIT                 
postgres 30307  0.2  0.7 104936 29852 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49247) COMMIT                 
postgres 30308  0.4  0.7 104932 30512 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49248) INSERT                 
postgres 30309  0.2  0.6 104936 28060 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49249) COMMIT                 
postgres 30312  0.3  0.7 104932 28440 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49252) COMMIT                 
postgres 30327  0.3  0.7 104932 29324 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49267) COMMIT                 
postgres 30328  0.3  0.6 104932 27984 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49268) COMMIT                 
postgres 30375  0.3  0.7 104936 28912 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49314) COMMIT                 
postgres 30378  0.3  0.6 104936 27128 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49317) COMMIT                 
postgres 30381  0.3  0.6 104936 26372 ?        Ss   18:53   0:00 postgres: abs ap 127.0.0.1(49320) COMMIT                 
postgres 30384  0.3  0.6 104932 27720 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49323) COMMIT                 
postgres 30385  0.2  0.6 104416 26980 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49324) COMMIT                 
postgres 30386  0.2  0.6 104416 27104 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49325) INSERT                 
postgres 30387  0.2  0.6 104364 28160 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49326) COMMIT                 
postgres 30389  0.2  0.6 104416 27496 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49327) INSERT                 
postgres 30391  0.2  0.6 104396 27688 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49329) COMMIT                 
postgres 30392  0.2  0.6 104416 28252 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49330) COMMIT                 
postgres 30393  0.2  0.6 104420 28072 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49331) COMMIT                 
postgres 30394  0.2  0.6 104416 28156 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49332) COMMIT                 
postgres 30439  0.2  0.7 104420 28504 ?        Ss   18:54   0:00 postgres: abs ap 127.0.0.1(49377) INSERT                 
postgres 30658  0.0  0.2 104416  8316 ?        Ss   18:55   0:00 postgres: abs ap 127.0.0.1(49596) COMMIT                 
postgres 30896  0.0  0.1 104336  7688 ?        Ss   18:55   0:00 postgres: abs ap 127.0.0.1(49830) COMMIT                 
postgres 30946  0.0  0.1 104220  7272 ?        Ss   18:56   0:00 postgres: abs ap 127.0.0.1(49880) COMMIT                 
postgres 31073  2.5  0.6 104268 25816 ?        Ss   18:56   0:00 postgres: abs ap 127.0.0.1(50006) SELECT                 
1000     31104  0.2  0.0  19296  2068 pts/0    Ss   18:56   0:00 -bash
1000     31134  0.0  0.0  16332  1136 pts/0    R+   18:56   0:00 ps aux
postgres 31135  0.0  0.1 103960  4380 ?        Rs   18:56   0:00 postgres: abs ap 127.0.0.1(50057) startup  

似乎我丢失了很多插入,因为有很多进程正在运行。我经常看到这样的信息:

psql: FATAL: 超出非超级用户的连接限制

对可能出现的问题有任何想法吗?为什么进程活动这么长时间?

【问题讨论】:

  • 您是否为每个新的 INSERT 语句打开一个新连接?
  • 是的。您能否发布一个链接,说明如何使用 Django 将多个 INSERT 分组到一个事务中?

标签: django postgresql apache2 connection


【解决方案1】:

您的存储似乎无法处理这么多交易。 PID 为 29586 的 postgres 进程当前正在等待设备完成请求(它处于“D”状态,这意味着“不可中断的睡眠(通常是 IO)”)并且可能正在阻塞其他请求。它是虚拟机吗?例如 VPS?它们的 IO 响应通常不是很灵敏——尤其是对于很多 fsync requests(在默认 Postgres 配置中每次提交都会生成一个 fsync)。

如果您有能力在崩溃的情况下丢失一些已确认的事务,那么您可以通过关闭 postgresql.conf 中的 synchronous_commit 或仅针对这些事务来使其更快并减少对 IO 延迟的依赖。

如果您可以让您的写入在一个事务中而不是多个事务中运行,这也可以变得更快。

【讨论】:

    【解决方案2】:

    您能否将插入内容放在bulk create 中?

    to_insert = []
    
    for item in items:
        to_insert.append(ObjectModel(item=item))
    
    ObjectModel.objects.bulk_create(to_insert)
    

    如果您正在创建@cyroxx 提到的新连接,这将有所帮助。

    【讨论】:

      【解决方案3】:

      错误信息

      致命:非超级用户超出连接限制

      是指向设置max_connections的明确指针

      postgresql.conf 中设置更高的限制(并重新启动 postgres 服务器进程)。比如:

      max_connections = 200
      

      或者你需要什么来满足要求。请注意,这需要一些资源。 More info in the manual.

      理想情况下,您不需要如此高的设置并将这些 INSERT 请求分散开或将它们捆绑到单个(或几个)连接中。甚至可能变成一个 INSERT 命令。

      如果你有autocommit on in Django,可能也值得检查一下:

      DATABASE_OPTIONS = {
          "autocommit": True,
      }
      

      如果没有,您需要在交易结束时发送COMMIT

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-23
        • 2011-05-01
        • 1970-01-01
        • 2012-03-14
        • 2016-12-01
        • 2011-08-28
        • 2021-07-11
        • 2015-08-16
        相关资源
        最近更新 更多