2014-06-12 82 views
0

我的應用程序真的非常非常困難,它看起來與數據庫有關。該應用程序處理大量的大量數據,並同時處理數百個用戶。在努力加快數據加載,我加載一些記錄是這樣的:這種模型方法是否鎖定我的PostgreSQL數據庫?

def load(filename) 

    rc = Publication.connection.raw_connection 
    rc.exec("COPY invoice_line_items FROM STDIN WITH CSV HEADER") 

    # open up your CSV file looping through line by line and getting the line into a format suitable for pg's COPY... 
    error = false 
    begin 
    CSV.foreach(filename) do |line| 
     until rc.put_copy_data(line.to_csv) 
     ErrorPrinter.print " waiting for connection to be writable..." 
     sleep 0.1 
     end 
    end 
    rescue Errno => err 
    User.inform_admin(false, User.me, "Line Item import failed with #{err.class.name} the following error: #{err.message}", err.backtrace) 
    error = true 
    else 
    rc.put_copy_end 
    while res = rc.get_result 
     if (res.result_status != 1) 
     User.inform_admin(false, User.me, "Line Item import result of COPY was: %s" % [ res.res_status(res.result_status) ], "") 
     error = true 
     end 
    end 
    end 

end 

我也Sidekiq約90線程中運行。這種加載方法是否在該表上設置了獨佔鎖?這些工作是否可能互相碰撞?如果他們是,我最好只是做插入?

+0

「真的很難碰到」真令人回味,但沒有提供信息。究竟發生了什麼? – jjanes

+0

應用程序服務器完全停止響應。數據庫中有大量看起來不會移動的進程。 – AKWF

回答

1

COPYINSERT的鎖定級別相同。 (它從explicit locking一章中缺失,但在源代碼中可見)。所以無論給你什麼麻煩,都可能不是這樣。

你應該看看pg_lockspg_stat_activity看看是否有什麼東西卡在鎖上。有關SO或DBA.SE,手冊和PostgreSQL wiki的其他問題的更多信息。

相關問題