2009-08-15 64 views
0

我试图通过运行在Java下面的SQL指令真空我的Postgres数据库:JDBC Postgres的真空超时

真空详细分析

有时它似乎只是“挂”有没有干净的方式中止这个过程?我曾尝试

SET statement_timeout为XXXX

但我得到的错误信息

回答

2

我刚刚测试“真空”不荣誉“statement_timeout”。示例程序:

import java.sql.*; 

class test 
{ 
     public static void main(String[] args) { 
       try { 
         Class.forName("org.postgresql.Driver"); 
         Connection connection = 
           DriverManager.getConnection(
             "jdbc:postgresql://hostname/dbname", 
             "username", 
             "password" 
           ); 
         connection.createStatement().executeUpdate(
           "set statement_timeout to 500" 
         ); 
         connection.createStatement().executeUpdate(
           "vacuum analyze" 
         ); 
       } catch (Exception ex) { 
         ex.printStackTrace(); 
       } 
     } 
} 

我得到以下错误:

org.postgresql.util.PSQLException: ERROR: canceling statement due to statement timeout 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:299) 
    at test.main(test.java:14) 

也许你不得不(临时)启用连接上自动提交。

1

我不认为有一个很好的(即安全)的方式来杀死“VACCUM不能在事务块内运行”过程而不是重新启动数据库。我不知道任何事务超时选项。

最好的解决办法是要弄清楚是什么造成的窍门,修复该问题。真空正在等待事务锁被释放。使用pg_locks视图来查看是否属于这种情况。如果您可以看到哪些资源被锁定,则可以开始解决该问题。

1

如果Postgres的服务器占用了这么多的时间来加载数据即如果某个特定查询,他们是如此多的结果可能会出现此错误,这需要时间来加载,将抛出此异常。