postgresql - JDBC postgres 真空超时

标签 postgresql jdbc vacuum

我正在尝试通过在 Java 中运行以下 SQL 指令来清理我的 Postgres 数据库:

真空详分割析

有时它似乎只是“挂起”有没有任何干净的方法来中止该过程?我试过了

将 statement_timeout 设置为 XXXX

但我收到错误消息“VACCUM 无法在事务 block 内运行”

最佳答案

我刚刚测试过,“vacuum”确实支持“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)

也许您必须(临时)在您的连接上启用自动提交。

关于postgresql - JDBC postgres 真空超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1281333/

相关文章:

sql - 根据与先前数据的差异更新数据

django - 破译 psycopg2 :OperationalError 的特例

python - 在网络浏览器中处理和显示大数据集

jdbc - SQL Server 的自定义处理器 + DBCPConnectionPool : driver jar not loaded

mysql - 如何防止执行过大的搜索?

python - postgresql 中的动态数据

c# - 查询不会从 npgsql 运行,而是在 postgresql pgAdmin 上运行

java - 一般如何从 ResultSet 中获取值?

database - Postgresql:数据库不接受命令以避免环绕数据丢失