VACUUM
VACUUM — 資源回收並且選擇性地重整資料庫
語法
說明
VACUUM 回收不再使用的儲存空間。在普通的 PostgreSQL 操作中,被刪除或被更新的儲存空間實際上並不會真實在磁碟上刪除;它們會一直存在,直到 VACUUM 完成。因此,必須定期執行 VACUUM,尤其是在經常更新的資料表上。
在沒有指定 TABLE 及欄位的情況下,VACUUM 處理目前資料庫中目前使用者有權清理的每個資料表。使用參數的話,VACUUM 就能只處理某個資料表。
普通的 VACUUM(不帶FULL)只是回收空間並使其可供重複使用。由於沒有獲得排他鎖定,此指令的這種形式可以與正常讀取和寫入資料表平行操作。但是,額外的空間不會還回到作業系統(大多數情況下)。它只是保持在同一張資料表內重新使用。我們可以利用多個 CPU 來處理索引。此功能稱為平行清理 (parallel vacuum)。要停用此功能,可以使用 PARALLEL 選項並將平行工作程序數量指定為零。 VACUUM FULL 會將資料表中的全部內容重寫為新的磁碟檔案,不會遺留額外的空間佔用,可將未使用的空間還回作業系統。這種形式顯然要慢得多,並且在處理每個資料表時需要排它鎖定 (exclusive lock)。
當選項列表被括號包圍時,選項可以按任意順序書寫。如果沒有括號,必須按照上面所示的順序指定選項。PostgreSQL 9.0 中加入了括號語法;未使用括號的語法已被棄用。
參數
FULL
選擇「FULL」清理,可以回收更多的空間,但需要更長的時間並且會完全鎖定資料表。此方法還需要額外的磁碟空間,因為它會寫入資料表的新的副本,並且在操作完成之前不會釋放舊副本。通常這只能在需要從資料表內回收大量空間時才會使用。
FREEZE
VERBOSE
為每個資料表輸出詳細的清理活動報告。
ANALYZE
更新查詢規劃單元需要使用的統計訊息,以決定最有效執行查詢的方式。
DISABLE_PAGE_SKIPPING
通常情況下,VACUUM 將根據可見性記錄跳過頁面。已知所有 tuple 都被凍結的頁面總是可以被跳過,並且所有 tuple 被知道對所有交易事務都可見的頁面也可能會被跳過,除非執行積極的清理。此外,除了執行積極的清理時,可能會跳過某些頁面以避免等待其他連線完成使用。此選項禁用所有頁面跳轉行為,並且僅用於可見性映射的內容被認為是可疑的,只有在存在導致資料庫損壞的硬體或軟體問題時才會發生。
SKIP_LOCKED
指定 VACUUM 在開始處理時不要等待任何關連鎖定被釋放:如果不等待就不能立即鎖定關連,則跳過該關連物件。 請注意,即使使用此選項,VACUUM 在處理關連的索引時仍可能會阻塞。 此外,VACUUM ANALYZE 在從分割區、資料表繼承的子項和某些型態的外部資料表中取得樣本行時仍可能會阻塞。 此外,雖然 VACUUM 通常處理指定分割區資料表的所有分割區,但如果分割區資料表上存在衝突鎖定,此選項將導致 VACUUM 跳過所有分割區。
INDEX_CLEANUP
Normally, VACUUM
will skip index vacuuming when there are very few dead tuples in the table. The cost of processing all of the table's indexes is expected to greatly exceed the benefit of removing dead index tuples when this happens. This option can be used to force VACUUM
to process indexes when there are more than zero dead tuples. The default is AUTO
, which allows VACUUM
to skip index vacuuming when appropriate. If INDEX_CLEANUP
is set to ON
, VACUUM
will conservatively remove all dead tuples from indexes. This may be useful for backwards compatibility with earlier releases of PostgreSQL where this was the standard behavior.
This option has no effect for tables that have no index and is ignored if the FULL
option is used. It also has no effect on the transaction ID wraparound failsafe mechanism. When triggered it will skip index vacuuming, even when INDEX_CLEANUP
is set to ON
.
PROCESS_TOAST
Specifies that VACUUM
should attempt to process the corresponding TOAST
table for each relation, if one exists. This is usually the desired behavior and is the default. Setting this option to false may be useful when it is only necessary to vacuum the main relation. This option is required when the FULL
option is used.
TRUNCATE
Specifies that VACUUM
should attempt to truncate off any empty pages at the end of the table and allow the disk space for the truncated pages to be returned to the operating system. This is normally the desired behavior and is the default unless the vacuum_truncate
option has been set to false for the table to be vacuumed. Setting this option to false may be useful to avoid ACCESS EXCLUSIVE
lock on the table that the truncation requires. This option is ignored if the FULL
option is used.
PARALLEL
boolean
Specifies whether the selected option should be turned on or off. You can write TRUE
, ON
, or 1
to enable the option, and FALSE
, OFF
, or 0
to disable it. The boolean
value can also be omitted, in which case TRUE
is assumed.
integer
Specifies a non-negative integer value passed to the selected option.
table_name
要清理的特定資料表名稱(可選擇性加上綱要)。如果省略,則目前資料庫中的所有常態的資料表和具體化檢視表都會被清理。如果指定的資料表是分割資料表,則其所有子分區都將被清理。
column_name
要分析的特定欄位的名稱。預設為所有欄位。 如果指定了列表,則隱含 ANALYZE。
輸出
當指定 VERBOSE 時,VACUUM 發出進度訊息以表示目前正在處理哪個資料表。有關資料表的各種統計訊息也會顯示出來。
注意
要清理資料表,通常必須是資料表的擁有者或超級使用者。但是,資料庫擁有者可以清理資料庫中的所有資料表,共享目錄除外。(對共享目錄的限制意味著真正的資料庫範圍內的 VACUUM 只能由超級使用者執行。)VACUUM 將跳過無權清理的所有資料表。
VACUUM 不能在交易事務區塊內執行。
FULL 選項不推薦於日常使用,但在特殊情況下可能會有用。例如,您刪除或更新了資料表中的大部分資料列,並且希望資料表在物理上縮小以佔用較少的磁碟空間以允許更快的資料表掃描。VACUUM FULL 通常會縮小資料表,而不是簡單的 VACUUM。
PARALLEL 選項僅用於單純的清理目的。 如果此選項與 ANALYZE 選項一起使用話,則也不會影響 ANALYZE。
範例
要清理單個資料表,請為其進行最佳化程序分析並輸出詳細的清理活動報告:
相容性
SQL 標準中並沒有 VACUUM 語句。
參閱
Was this helpful?