[SQL] Postgres 遇到 out of shared memory 問題

由於工作需要,我在 Postgresql 中創建了 20000 張 20 columns x 1000 entries 的 tables,約莫了 18G 的大小。

由於要一次性創建如此海量的資料,因此我寫了段小 Script 來協助完成。

期間在大約 2000 筆衣料左右的時候程式就會碰到 out of shared memory 的問題,困擾我一段時間。

查詢了很多網站都沒有找到良好的解釋或是解決方案。

直到後來查到 stackoverflow 上一個沒什麼人回應的解決方案

A quick fix for PSQLException error out of shared memory is to set the

max_locks_per_transaction

parameter in PostgreSQL config file, which specifies number of object locks allocated for each transaction (if there are statements/routines that work on large number of tables to finish.

其實主要就是 PostgresSQL 在 transaction lock 有其上限。當 default 上限用完了,就會 out of shared memory.

要解決的方法也不難,我們可以去 postgresql 的 config 中找到 max_locks_per_transaction 將 default 的 64 加大。

至於 postgresql config 是放在哪裡?

這部分我們可以使用 SHOW config_file; 這行 sql 查看。

Default 應該都是在 /var/lib/postgresql/data/postgresql.conf 的。

--

--

Les Lee
一個小小工程師的隨手筆記

對新事物總是興緻勃勃、嘗試新技術、解決新挑戰; 也時常陷入許多無謂的思索,卡在其中得不出答案。