我正在使用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:致命错误:非超级用户超出连接限制
关于可能出错的任何想法?为什么这些过程活跃了这么久?
答案 0 :(得分:1)
看起来您的存储无法处理那么多交易。具有PID 29586的postgres进程当前正在等待设备完成请求(它处于“D”状态,这意味着“不间断睡眠(通常是IO)”)并且可能阻止其他请求。它是虚拟机 - 例如VPS吗?它们通常没有非常敏感的IO - 特别是对于很多fsync
requests(每次提交都会在默认的Postgres配置中生成fsync
。
如果您能够承受在发生崩溃时丢失一些已确认的交易,那么您可以通过关闭synchronous_commit
中的postgresql.conf
或仅针对这些交易来更快地减少对IO延迟的依赖。
如果您可以让您的写入在一个事务中运行而不是多个事务,那么这可以更快地完成。
答案 1 :(得分:1)
您可以将插入内容放在bulk create吗?
中to_insert = []
for item in items:
to_insert.append(ObjectModel(item=item))
ObjectModel.objects.bulk_create(to_insert)
如果您正在创建@cyroxx所提及的新连接,这将有所帮助。
答案 2 :(得分:0)
错误讯息
致命:非超级用户超出连接限制
是指向设置max_connections
在postgresql.conf
中设置更高的限制(并重新启动postgres服务器进程)。类似的东西:
max_connections = 200
或者您需要满足哪些要求。请注意,需要一些资源。 More info in the manual.
理想情况下,您不需要设置那么高并分散这些INSERT
请求或将它们捆绑到单个(或几个)连接中。甚至可能只用一个INSERT
命令。
也可能值得检查一下是否autocommit on in Django:
DATABASE_OPTIONS = {
"autocommit": True,
}
如果没有,您需要在交易结束时发送COMMIT
。