1

我们有 Postgres 作为我们的后端数据库。我们的进程运行一些工作(即它在数据库中进行一些插入/更新),然后休眠一个小时

这是我们注意到的。当我们的进程处于休眠状态时,我们的 Postgres 连接状态被视为空闲。

postgres  5045  0.3  0.4 231220 33780 ?        Ss   12:13   0:16 postgres: scp scp_test x.x.x.x(60400) idle    

现在我的问题是?

如果,我有一个睡眠一个小时的进程。

Postgres 是否会在一段时间后关闭空闲连接?

因为在下次运行时,该进程无法在 DB 中插入/更新任何记录

这是我的代码的样子。

  $logger  = Logger.new('log/checker.log')
  last_modified = Time.now
  while
    if (last_modified == File.mtime(file_path))
      $logger.info "Sleeping for 1 hour"
      sleep 3600
    else
      $logger.info "Inserting the file mtime changed .."
      last_modified = File.mtime(file_path)
      $logger.info "File modified ....."
      attributes = Test.testFile(file_path)
      index = 0 
      $logger.info "........Index set......"
      header_attributes = attributes.shift
      $logger.info "...........Header removed..........."
      trailer_attributes = attributes.pop
      $logger.info "...Trailer removed......."
      count = attributes.count
      $logger.info "............Count calculated #{count} ........."
      attributes.each_slice(50000) { |records|
        _records = initialize_records(records)
        _records.each { |record| 
          record.save 
          index += 1 
          $logger.info "Inserting ...... [#{index}/#{count}]"
       }
     }
     $logger.info "Completed insertion of #{count}" 
   end
 end

测试了这个

Ruby-2.2.2  -  ActiveRecord-4.2.6  - pg-0.18.0 
Ruby-2.3.0  -  ActiveRecord-4.2.6  - pg-0.18.0  
Jruby-9.0.5.0 - ActiveRecord-4.2.6 - activerecord-jdbc-adapter

Postgres 版本。

 PostgreSQL 9.4.5 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)

Ruby 和 JRuby 输出之间有 1 个区别?

虽然两个进程在从睡眠中醒来后都卡住了

Ruby 进程因PG::UnableToSend: SSL SYSCALL error: EOF detected错误而死

但是 JRuby 进程永远卡住了(不会死)。

不确定问题出在哪里,因为此时我无法确定任何特定的库或代码。

注意:在环回接口上工作得很好。有问题的postgres服务器是远程服务器..

4

1 回答 1

0

在数据文件夹中有一个文件,postgresql.conf您必须将其配置为向keepalive客户端发送消息。阅读内容以获取更多信息。

配置postgresql.conf

您的postgresql.conf文件应该有这样的一行tcp_keepalives_idle。如果您想keepalive每 5 分钟向客户端计算机发送一次消息;像这样更新tcp_keepalives_idle

tcp_keepalives_idle = 300

确保通过删除 # 标记取消注释该行。

于 2016-04-17T16:38:25.347 回答