3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PostgreSQL:VACUUMコマンドで不要領域を削除する

Last updated at Posted at 2024-03-24

概要

VACUUM コマンドの使い方を確認。
VACUUMコマンドを使うことによって、不要領域の回収ができます。
不要領域が増えると、ディスク領域の圧迫や、I/O増加に性能低下といった弊害が出てきます。

今回は以下2点の動きを確認します。
1.不要領域を作成して、VACUUM VERBOSEコマンドで不要領域を削除できることを確認する
2.VACUUM ANALYZEコマンドを実行して、pg_statisticが更新されていることを確認

1.不要領域の削除(VACUUM VERBOSE)

# データベース名examdbに入る
psql -d  examdb

# 不要領域を0の時
# VACUUM VERBOSEを実行して、ログで不要領域が0であることを確認する
# ※
examdb=# VACUUM  VERBOSE customer;

INFO:  vacuuming "public.customer"
INFO:  table "customer": found 0 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 767
Skipped 0 pages due to buffer pins, 0 frozen pages.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUM


# 不要領域を1個作る
UPDATE customer SET name ='CC' WHERE age=30;
UPDATE 1

# 不要領域を削除して、ログに出力されていることを確認する
# ※INFO:  table "customer": found 1 removable, 2 nonremovable row 
examdb=# VACUUM  VERBOSE customer;
INFO:  vacuuming "public.customer"
INFO:  table "customer": found 1 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 768
Skipped 0 pages due to buffer pins, 0 frozen pages.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUM

# 不要領域を2個作る
UPDATE customer SET name ='DD' WHERE age=30;
UPDATE 1
UPDATE customer SET name ='EE' WHERE age=30;
UPDATE 1

# 不要領域を削除して、ログに出力されていることを確認する
# ※INFO:  table "customer": found 2 removable, 2 nonremovable row 
examdb=# VACUUM  VERBOSE customer;
INFO:  vacuuming "public.customer"
INFO:  table "customer": found 2 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 770
Skipped 0 pages due to buffer pins, 0 frozen pages.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUM



2.VACUUM ANALYZE 実行 → pg_statisticを確認

# pg_statisticを削除する & 削除されることを確認
examdb=# DELETE FROM pg_statistic;
DELETE 2
examdb=# select * from pg_statistic;
(0 rows)

# VACUUM ANALYZE customerを実行 & pg_statisticテーブルが作成されていることを確認する
VA
examdb=# VACUUM ANALYZE customer;
VACUUM
examdb=# select * from pg_statistic;
-[ RECORD 1 ]------------------
starelid    | 16404
staattnum   | 1
stainherit  | f
stanullfrac | 0
stawidth    | 6
stadistinct | -1
stakind1    | 2
stakind2    | 3
stakind3    | 0
stakind4    | 0
stakind5    | 0
staop1      | 1058
staop2      | 1058
staop3      | 0
staop4      | 0
staop5      | 0
stacoll1    | 100
stacoll2    | 100
stacoll3    | 0
stacoll4    | 0
stacoll5    | 0
stanumbers1 | 
stanumbers2 | {-1}
stanumbers3 | 
stanumbers4 | 
stanumbers5 | 
stavalues1  | {"EE   ","b    "}
stavalues2  | 
stavalues3  | 
stavalues4  | 
stavalues5  | 
-[ RECORD 2 ]------------------
starelid    | 16404
staattnum   | 2
stainherit  | f
stanullfrac | 0
stawidth    | 4
stadistinct | -1
stakind1    | 2
stakind2    | 3
stakind3    | 0
stakind4    | 0
stakind5    | 0
staop1      | 97
staop2      | 97
staop3      | 0
staop4      | 0
staop5      | 0
stacoll1    | 0
stacoll2    | 0
stacoll3    | 0
stacoll4    | 0
stacoll5    | 0
stanumbers1 | 
stanumbers2 | {1}
stanumbers3 | 
stanumbers4 | 
stanumbers5 | 
stavalues1  | {29,30}
stavalues2  | 
stavalues3  | 
stavalues4  | 
stavalues5  | 


pg_statisticについて
よくわかんないですね。
stavalue1からして、RECORD 1=name,RECORD 2 = age ですかね

前提条件

以下のテーブルを作成済み

create.sql

-- /home/postgres/ch6/1-plainbackup/create.sql
--テーブルの追加と適当にデータを追加する。
CREATE TABLE customer(
name CHAR(5) NOT NULL,
age INTEGER NOT NULL);

INSERT INTO customer(name,age) VALUES('aiue',30);
INSERT INTO customer(name,age) VALUES('abcd',29);

参考

ANALYZE
https://www.postgresql.jp/docs/9.4/sql-analyze.html

OSS教科書 OSS-DB Silver Ver.3.0対応
p165

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?