Postgresql在Windows重启后一次查询失败

本文关键字:一次 查询 失败 Windows 重启 Postgresql | 更新日期: 2023-09-27 18:02:32

我在c#应用程序中使用Windows上的Postgresql。我遇到的问题很奇怪,可以这样描述:

  • 我重新启动我的Windows
  • 我运行程序
  • 一个特定查询失败:SELECT COUNT(*) AS c FROM files WHERE total_bytes IS NOT NULL
  • 我再次运行程序,一切正常

奇怪指出:

  • 我试着在那之前做另一个查询(甚至使用相同的表),它工作了:SELECT COUNT(*) AS c FROM files
  • 我无法重现重新启动Postgresql的错误。它只在Windows重启时发生。而且这种情况只发生一次。

异常回溯:

Npgsql.NpgsqlException: Exception while reading from stream
   at Npgsql.ReadBuffer.Ensure(Int32 count, Boolean dontBreakOnTimeouts)
   at Npgsql.NpgsqlConnector.DoReadMessage(DataRowLoadingMode dataRowLoadingMode, Boolean isPrependedMessage)
   at Npgsql.NpgsqlConnector.ReadMessageWithPrepended(DataRowLoadingMode dataRowLoadingMode)
   at Npgsql.NpgsqlConnector.ReadMessage(DataRowLoadingMode dataRowLoadingMode)
   at Npgsql.NpgsqlConnector.ReadExpecting[T]()
   at Npgsql.NpgsqlDataReader.NextResultInternal()
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Npgsql.NpgsqlCommand.ExecuteReader()
   at DriveShare.Database.Postgresql.ExecuteQuery(NpgsqlCommand command) in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'Postgresql.cs:line 216
   at DriveShare.Database.Postgresql.Query(String sql, Object[] args) in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'Postgresql.cs:line 72
   at DriveShare.Database.Postgresql.QueryOne(String sql, Object[] args) in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'Postgresql.cs:line 83
   at DriveShare.Database.Postgresql.QueryValue(String key, String sql, Object[] args) in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'Postgresql.cs:line 97
   at DriveShare.Database.Postgresql.QueryValue(String key, String sql) in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'Postgresql.cs:line 92
   at DriveShare.Database.FileIndexDataSet.CountIndexedFiles() in c:'projetos'driveshareclient'DriveShare'DriveShare'Database'FileIndexDataSet.cs:line 89
   at DriveShare.Engine.DriveShareEngine.Start() in c:'projetos'driveshareclient'DriveShare'DriveShare'Engine'DriveShareEngine.cs:line 156
   at DriveShareWebService.Program.Main(String[] args) in c:'projetos'driveshareclient'DriveShare'DriveShareWebService'Program.cs:line 19

因为我必须保持程序工作,我写了一个变通办法,使应用程序重试查询前继续。我并不为此感到骄傲:

public void WaitForConnection()
{
    int limitSeconds = 3 * 60;
    var start = DateTime.Now;
    while (true)
    {
        try
        {
            Log.WaitingForDatabaseConnection();
            Query("SELECT COUNT(*) AS c FROM files WHERE total_bytes IS NOT NULL");
            Log.DatabaseConnectionAquired();
            break;
        }
        catch (Exception e)
        {
            var wastedTime = DateTime.Now - start;
            if (wastedTime.TotalSeconds > limitSeconds)
                throw;
            else
                Log.Exception(e);
        }
        Thread.Sleep(1000);
    }
}

我使用Npgsql(在一个瘦抽象类)连接到Postgresql。Postgresql日志显示了一些关于winsock错误的条目,我还没有理解:

2016-08-16 10:14:34 BRT LOG:  database system was shut down at 2016-08-16 10:12:07 BRT
2016-08-16 10:14:34 BRT FATAL:  the database system is starting up
2016-08-16 10:14:34 BRT LOG:  MultiXact member wraparound protections are now enabled
2016-08-16 10:14:34 BRT LOG:  sistema de banco de dados está pronto para aceitar conexões
2016-08-16 10:14:34 BRT LOG:  autovacuum launcher started
2016-08-16 10:17:16 BRT LOG:  could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:17:27 BRT LOG:  could not send data to client: unrecognized winsock error 10054
2016-08-16 10:17:27 BRT STATEMENT:  SELECT path FROM files
2016-08-16 10:17:27 BRT FATAL:  connection to client lost
2016-08-16 10:17:27 BRT STATEMENT:  SELECT path FROM files
2016-08-16 10:17:27 BRT LOG:  could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:17:27 BRT LOG:  unexpected EOF on client connection with an open transaction
2016-08-16 10:17:33 BRT LOG:  unexpected EOF on client connection with an open transaction
2016-08-16 10:25:14 BRT LOG:  could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:25:15 BRT LOG:  could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:25:15 BRT LOG:  unexpected EOF on client connection with an open transaction
2016-08-16 10:26:30 BRT LOG:  could not send data to client: unrecognized winsock error 10054
2016-08-16 10:26:30 BRT FATAL:  connection to client lost
2016-08-16 10:26:50 BRT LOG:  could not send data to client: unrecognized winsock error 10054
2016-08-16 10:26:50 BRT FATAL:  connection to client lost
2016-08-16 10:26:50 BRT LOG:  could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:26:50 BRT LOG:  unexpected EOF on client connection with an open transaction
2016-08-16 10:27:06 BRT LOG:  could not send data to client: unrecognized winsock error 10054
2016-08-16 10:27:06 BRT FATAL:  connection to client lost
2016-08-16 10:27:06 BRT LOG:  could not send data to client: unrecognized winsock error 10054
2016-08-16 10:27:06 BRT FATAL:  connection to client lost
2016-08-16 10:27:30 BRT LOG:  pedido de desligamento rápido foi recebido
2016-08-16 10:27:30 BRT LOG:  interrompendo quaisquer transações ativas
2016-08-16 10:27:30 BRT LOG:  autovacuum launcher shutting down
2016-08-16 10:27:30 BRT ERROR:  canceling statement due to user request
2016-08-16 10:27:30 BRT LOG:  autovacuum launcher shutting down
2016-08-16 10:27:30 BRT LOG:  shutting down
2016-08-16 10:27:30 BRT LOG:  database system is shut down

我不期望有人知道我的问题到底是什么。我只是希望有人可能有类似的问题,可以给我们一些启示。

Postgresql在Windows重启后一次查询失败

在一些帮助下,我在Npgsql文档中找到了解决方案。

Npgsql默认带有一些连接和命令的超时参数。在Windows重启后,第一次访问表非常慢,触发命令超时。

使用连接字符串上的其他参数,我能够更改这些设置并解决我的问题:

connectionString += ";Timeout=180;Command Timeout=180";

额外提示: Postgresql函数pg_sleep(seconds)帮助我重现了这个问题,而没有实际重启。很有帮助:

SELECT pg_sleep(60);