NpgsqlCopyIn未能在超时("的CommandTimeout"设置忽略)能在、NpgsqlCopyIn、CommandTimeout、QUOT

2023-09-06 11:13:43 作者:朕好帅

我有存储在CSV文件中的一个客户端应用程序(.NET 4.0)一个非常大的数据集(900K记录,140MB的磁盘空间)。我需要这个数据加载到Postgres的9分贝的最快方法。我使用NpgsqlNpgsqlCopyIn技术(Npgsql库版本= 2.1.0)。

有关探头负载(138K)插入工作正常 - 这大约需要7 secons。 但对于整批(900K),在code抛出超时异常:

 错误:57014:取消语句由于语句超时
 

堆栈跟踪是:

  

Npgsql.NpgsqlState.d_的 9.MoveNext()在   Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector   上下文)在Npgsql.NpgsqlCopyInState.SendCopyDone(NpgsqlConnector   上下文)在Npgsql.NpgsqlCopyInState.StartCopy(NpgsqlConnector   背景下,NpgsqlCopyFormat copyFormat)在   Npgsql.NpgsqlState.d 的_9.MoveNext()在   Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector   上下文)在   Npgsql.NpgsqlConnector.ProcessAndDiscardBackendResponses()在   Npgsql.NpgsqlCommand.ExecuteBlind()在Npgsql.NpgsqlCopyIn.Start()

我试着设置的CommandTimeout来基洛值(> 7200),为零;尝试相同的值,用于连接超时参数。我也试图通过连接字符串设定的CommandTimeout,但仍然没有结果 - 错误57014出来一次又一次

组图 梁静茹悼念李玟 晒昔日节目画面追忆 会记得你最美的笑容

请,有利于正确加载批!

下面是code我用:

 私有静态无效pgBulkCopy(字符串connection_string,FileInfo的fiDataFile)
{
    使用(Npgsql.NpgsqlConnection CON =新Npgsql.NpgsqlConnection(connection_string))
    {
        con.Open();

        的FileStream IFS =新的FileStream(fiDataFile.FullName,FileMode.Open,FileAccess.Read);
        字符串查询字符串=COPY schm.Addresses(FullAddress,纬度,经度)从标准输入;;
        NpgsqlCommand CMD =新NpgsqlCommand(查询字符串,CON);
        cmd.CommandTimeout = 7200; // 7200sec,120分钟,2个小时

        NpgsqlCopyIn COPYIN =新NpgsqlCopyIn(CMD,CON,IFS);

        尝试{
            copyIn.Start();
            copyIn.End();
        }赶上(例外前)
        {
            Console.WriteLine([DB] pgBulkCopy错误:+ ex.Message);
        }
        最后
        {
            con.Close();
        }
    }
}
 

解决方案

Npgsql有关于命令超时和NpgsqlCopyIn处理的错误。

是否有可能为你测试我们目前掌握的,我们有很多关于命令超时的修复处理的?

您可以下载该项目的副本在我们的GitHub页面:的https:// github上.COM / npgsql / Npgsql /存档/ master.zip

请,给它一个尝试,让我们知道,如果你的作品。

在此先感谢。

I have a quite large dataset (900K records, 140Mb disk space) stored in CSV file in a client app (.NET 4.0). I need to load this data to Postgres 9 db the fastest way. I use Npgsql "NpgsqlCopyIn" technique (Npgsql library version=2.1.0).

For a probe load (138K) insertion works fine - it takes about 7 secons. But for the whole batch (900K), the code throws timeout exception:

  "ERROR: 57014: canceling statement due to statement timeout"

The stack trace is:

Npgsql.NpgsqlState.d_9.MoveNext() at Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector context) at Npgsql.NpgsqlCopyInState.SendCopyDone(NpgsqlConnector context) at Npgsql.NpgsqlCopyInState.StartCopy(NpgsqlConnector context, NpgsqlCopyFormat copyFormat) at Npgsql.NpgsqlState.d_9.MoveNext() at Npgsql.NpgsqlState.ProcessAndDiscardBackendResponses(NpgsqlConnector context) at Npgsql.NpgsqlConnector.ProcessAndDiscardBackendResponses() at Npgsql.NpgsqlCommand.ExecuteBlind() at Npgsql.NpgsqlCopyIn.Start()

I tried setting CommandTimeout to kilo values(>7200), zero; tried same values for connection "Timeout" parameter. Also I was trying to set "CommandTimeout" via connection string, but still with no result - "ERROR 57014" comes out again and again.

Please, help to load the batch correctly!

Here is the code I use:

private static void pgBulkCopy(string connection_string, FileInfo fiDataFile)
{
    using (Npgsql.NpgsqlConnection con = new Npgsql.NpgsqlConnection(connection_string))
    {
        con.Open();

        FileStream ifs = new FileStream(fiDataFile.FullName, FileMode.Open, FileAccess.Read);
        string queryString = "COPY schm.Addresses(FullAddress,lat,lon) FROM STDIN;";
        NpgsqlCommand cmd = new NpgsqlCommand(queryString, con);
        cmd.CommandTimeout = 7200; //7200sec, 120 min, 2 hours

        NpgsqlCopyIn copyIn = new NpgsqlCopyIn(cmd, con, ifs);

        try{
            copyIn.Start();    
            copyIn.End();
        }catch(Exception ex)
        {
            Console.WriteLine("[DB] pgBulkCopy error: " + ex.Message );
        }
        finally
        {
            con.Close();
        }
    }
}

解决方案

Npgsql has a bug regarding command timeout and NpgsqlCopyIn handling.

Is it possible for you to test our current master where we had a lot of fixes about command timeout handling?

You can download a copy of the project in our github page: https://github.com/npgsql/Npgsql/archive/master.zip

Please, give it a try and let us know if it works for you.

Thanks in advance.