PostgreSQL DBA(92) - PG 12 Improving VACUUM

VACUUM命令在PG 12有所增强,提供新的Option可供选择,本节介绍了其中一个选项:INDEX_CLEANUP

PG 11 vacuum命令

[xdb@localhost ~]$ psql -d testdb -p 5433psql (11.2)Type "help" for help.testdb=# \help vacuum;Command:     VACUUMDescription: garbage-collect and optionally analyze a databaseSyntax:VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]where option can be one of:    FULL    FREEZE    VERBOSE    ANALYZE    DISABLE_PAGE_SKIPPINGand table_and_columns is:    table_name [ ( column_name [, ...] ) ]testdb=#

PG 12 vacuum命令

[local]:5432 pg12@pgbench=# select version();                                                  version                                   -------------------------------------------------------------------------------------------- PostgreSQL 12beta3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit(1 row)Time: 11.981 ms[local]:5432 pg12@pgbench=# \help vacuum Command:     VACUUMDescription: garbage-collect and optionally analyze a databaseSyntax:VACUUM [ ( option [, ...] ) ] [ table_and_columns [, ...] ]VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ table_and_columns [, ...] ]where option can be one of:    FULL [ boolean ]    FREEZE [ boolean ]    VERBOSE [ boolean ]    ANALYZE [ boolean ]    DISABLE_PAGE_SKIPPING [ boolean ]    SKIP_LOCKED [ boolean ]    INDEX_CLEANUP [ boolean ]    TRUNCATE [ boolean ]and table_and_columns is:    table_name [ ( column_name [, ...] ) ]URL: https://www.postgresql.org/docs/12/sql-vacuum.html[local]:5432 pg12@pgbench=#

下面通过pgbench执行简单的测试

[pg12@localhost ~]$ createdb pgbench;[pg12@localhost ~]$  pgbench --initialize --scale=100 pgbenchdropping old tables...NOTICE:  table "pgbench_accounts" does not exist, skippingNOTICE:  table "pgbench_branches" does not exist, skippingNOTICE:  table "pgbench_history" does not exist, skippingNOTICE:  table "pgbench_tellers" does not exist, skippingcreating tables...generating data...100000 of 10000000 tuples (1%) done (elapsed 0.14 s, remaining 14.30 s)

使用pgbench执行60s的压力测试

[pg12@localhost ~]$ pgbench --no-vacuum --time=60 --client=2 --jobs=2 pgbenchtransaction type: <builtin: TPC-B (sort of)>scaling factor: 100query mode: simplenumber of clients: 2number of threads: 2duration: 60 snumber of transactions actually processed: 42499latency average = 2.824 mstps = 708.298224 (including connections establishing)tps = 708.325760 (excluding connections establishing)[pg12@localhost ~]$

执行常规的vacuum

[local]:5432 pg12@testdb=#  VACUUM (VERBOSE) pgbench_accounts ;ERROR:  relation "pgbench_accounts" does not existTime: 54.069 ms[local]:5432 pg12@testdb=# \c pgbenchYou are now connected to database "pgbench" as user "pg12".[local]:5432 pg12@pgbench=#  VACUUM (VERBOSE) pgbench_accounts ;INFO:  vacuuming "public.pgbench_accounts"INFO:  scanned index "pgbench_accounts_pkey" to remove 37520 row versionsDETAIL:  CPU: user: 1.28 s, system: 0.68 s, elapsed: 2.54 sINFO:  "pgbench_accounts": removed 37520 row versions in 37520 pagesDETAIL:  CPU: user: 0.21 s, system: 0.04 s, elapsed: 0.28 sINFO:  index "pgbench_accounts_pkey" now contains 10000000 row versions in 27422 pagesDETAIL:  37520 index row versions were removed.0 index pages have been deleted, 0 are currently reusable.CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.INFO:  "pgbench_accounts": found 37444 removable, 9976515 nonremovable row versions in 164166 out of 164551 pagesDETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 48279There were 7 unused item identifiers.Skipped 0 pages due to buffer pins, 0 frozen pages.0 pages are entirely empty.CPU: user: 3.15 s, system: 7.45 s, elapsed: 11.31 s.VACUUMTime: 11811.362 ms (00:11.811)[local]:5432 pg12@pgbench=#

再次使用pgbench执行压力测试,但在执行vacumm指定INDEX_CLEANUP选项为false

https://yiyuan.9939.com/yyk_62925/jieshao.html

[pg12@localhost ~]$ pgbench --no-vacuum --time=60 --client=2 --jobs=2 pgbenchtransaction type: <builtin: TPC-B (sort of)>scaling factor: 100query mode: simplenumber of clients: 2number of threads: 2duration: 60 snumber of transactions actually processed: 41268latency average = 2.908 mstps = 687.790258 (including connections establishing)tps = 687.817603 (excluding connections establishing)[local]:5432 pg12@pgbench=# VACUUM (INDEX_CLEANUP False, VERBOSE) pgbench_accounts ;INFO:  vacuuming "public.pgbench_accounts"INFO:  "pgbench_accounts": found 36498 removable, 10000000 nonremovable row versions in 164967 out of 164967 pagesDETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 168578There were 161 unused item identifiers.Skipped 0 pages due to buffer pins, 0 frozen pages.0 pages are entirely empty.CPU: user: 0.96 s, system: 4.10 s, elapsed: 5.30 s.VACUUMTime: 5314.340 ms (00:05.314)[local]:5432 pg12@pgbench=#

跳过了Index的清理,再次执行vacumm,这次指定INDEX_CLEANUP为true

[local]:5432 pg12@pgbench=# VACUUM (INDEX_CLEANUP true, VERBOSE) pgbench_accounts ;INFO:  vacuuming "public.pgbench_accounts"INFO:  scanned index "pgbench_accounts_pkey" to remove 84133 row versionsDETAIL:  CPU: user: 2.48 s, system: 0.98 s, elapsed: 3.53 sINFO:  scanned index "idx_accounts_bid" to remove 84133 row versionsDETAIL:  CPU: user: 1.54 s, system: 1.13 s, elapsed: 2.80 sINFO:  "pgbench_accounts": removed 84133 row versions in 81168 pagesDETAIL:  CPU: user: 1.09 s, system: 2.47 s, elapsed: 5.04 sINFO:  index "pgbench_accounts_pkey" now contains 10000000 row versions in 27422 pagesDETAIL:  84133 index row versions were removed.0 index pages have been deleted, 0 are currently reusable.CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.INFO:  index "idx_accounts_bid" now contains 10000000 row versions in 27665 pagesDETAIL:  25763 index row versions were removed.0 index pages have been deleted, 0 are currently reusable.CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.INFO:  "pgbench_accounts": found 0 removable, 10000000 nonremovable row versions in 164967 out of 164967 pagesDETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 168578There were 161 unused item identifiers.Skipped 0 pages due to buffer pins, 0 frozen pages.0 pages are entirely empty.CPU: user: 6.03 s, system: 5.34 s, elapsed: 13.06 s.VACUUMTime: 13109.490 ms (00:13.109)[local]:5432 pg12@pgbench=#

这次操作只对index执行清理.郑州不孕不育医院:http://www.zzchyy110.com/

PG 12提供的INDEX_CLEANUP vacumm选项在对大表并存在多索引进行清理时可有效的缩短执行时间,但index上的废弃tuple仍然保留,空间仍会膨胀,只不过时间滞后了而已.


猜你喜欢

转载自blog.51cto.com/14510269/2438334