os: centos 7.4
db: postgresql 11.5
oracle 之前有个 statspack,pgsql也不能落后,同样有 pgstatspack,哈哈,前面加 pg 两个字母。
最新的版本为 pgstatspack_version_2.3.1,最后更新日期为 October 16, 2011。
明显已经被废弃了,不过有想法的话可以在这个基础上自己添加一些信息。
现在试用下。
# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
#
# su - postgres
Last login: Tue Oct 22 13:56:27 CST 2019 on pts/0
$
$ psql -c "select version();"
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
(1 row)
# su - postgres
$ wget http://pgfoundry.org/frs/download.php/3151/pgstatspack_version_2.3.1.tar.gz
$ tar -zxvf ./pgstatspack_version_2.3.1.tar.gz
$ ls -l
total 20
drwx------. 4 postgres postgres 33 Oct 5 21:26 11
drwxr-xr-x 4 postgres postgres 303 Oct 16 2011 pgstatspack
-rw-r--r-- 1 postgres postgres 17049 Oct 22 14:07 pgstatspack_version_2.3.1.tar.gz
$ cd pgstatspack/
$ more README
安装说明
Installation: (Run all scripts as the postgres OS user)
1. Create or use a postgresql user which has superuser privilege
2. Run the install_pgstats.sh this will install the script on all databases except the postgres db and templates
3. Check the output for errors
4. Create a cron job to call the snapshot.sh for automated snapshot creation recommended every 15 minutes
5. Create a daily cron job to delete old snapshots
$ ./install_pgstats.sh
install_pgstats.sh 内容很容易看得懂
#!/bin/bash
#db call to get database name
PSQL="psql -q --set ON_ERROR_STOP=on"
install_stats () {
set -e
$PSQL -d "${dbname}" -f "sql/pgstatspack_create_tables.sql"
$PSQL -d "${dbname}" -f "sql/pgstatspack_create_snap.sql"
$PSQL -d "${dbname}" -f "sql/pgstatspack_delete_old_stats.sql"
$PSQL -d "${dbname}" -f "sql/pgstatspack_get_unused_indexes.sql"
set +e
}
for dbname in `$PSQL -t -f "sql/db_name_install.sql"`
do
echo "Results for database ${dbname}"
if [ `$PSQL -d "${dbname}" -t -c "select count(lanname) from pg_language where lanname='plpgsql';"` -lt 1 ]
then
echo "Installing language plpgsql for database ${dbname}"
$PSQL -d "${dbname}" -c "create language plpgsql;"
fi
x=`$PSQL -t -d "${dbname}" -f "sql/pgstats_exist.sql"`
if [ $x -eq "0" ]
then
echo "Installing Statistics Package for database ${dbname}"
install_stats
elif [ $x -lt "6" ]
then
echo "Previous install of statisics package was incomplete. Reinstalling Stats for database ${dbname}"
install_stats
else
echo "Statistics package already exists for database: ${dbname}"
fi
x=0
done
在 bin 目录下有几个文件
$ cd ~/pgstatspack/bin
$ ls -l
total 28
-rwxr-xr-x 1 postgres postgres 262 Oct 16 2011 delete_snapshot.sh
-rwxr-xr-x 1 postgres postgres 17355 Oct 16 2011 pgstatspack_report.sh
-rwxr-xr-x 1 postgres postgres 313 Oct 16 2011 snapshot.sh
手动生成第一个快照
$ ~/pgstatspack/bin/snapshot.sh
Results for database peiybdb
pgstatspack_snap
------------------
1
(1 row)
snapshot.sh 需要写成 crontab 的方式定时运行
$ crontab -e
*/30 * * * * /var/lib/pgsql/pgstatspack/bin/snapshot.sh 1 >> /var/lib/pgsql/pgstatspack/logs/snapshot.log 2>&1
2 3 * * * /var/lib/pgsql/pgstatspack/bin/delete_snapshot.sh 1 >> /var/lib/pgsql/pgstatspack/logs/delete_snapshot.log 2>&1
生成数据
peiybdb=# create table tmp_t0(c0 varchar(100),c1 varchar(100));
CREATE TABLE
peiybdb=# insert into tmp_t0 select id::varchar,md5(id::varchar) from generate_series(1,100000) as id;
INSERT 0 100000
peiybdb=# select count(1) from tmp_t0;
count
--------
100000
(1 row)
手动生成一个新的快照
$ ~/pgstatspack/bin/snapshot.sh
Results for database peiybdb
pgstatspack_snap
------------------
2
(1 row)
生成报告,输出帮助信息
$ ~/pgstatspack/bin/pgstatspack_report.sh -h
~/pgstatspack/bin ~/pgstatspack/bin
Usage:
pgstatspack_report.sh [-u username] [-d database] [-f filename] [-h]
Generates a statspack style report for a given postgres database
-u username specifies the database user to connect to the database with
-d database specifies the database to connect to
-f filename specifies the filename where the report will be written
-h prints this help message
生成报告
$ ~/pgstatspack/bin/pgstatspack_report.sh -u postgres -d peiybdb
~/pgstatspack/bin ~/pgstatspack/bin
snapid | ts | description
--------+----------------------------+---------------------
2 | 2019-10-22 14:54:36.205711 | cron based snapshot
1 | 2019-10-22 14:52:31.695282 | cron based snapshot
(2 rows)
Enter start snapshot id : 1
Enter stop snapshot id : 2
Using file name: /tmp/pgstatreport_peiybdb_1_2.txt
###########################################################################################################
PGStatspack version 2.3 by [email protected]
###########################################################################################################
Snapshot information
Begin snapshot :
snapid | ts | description
--------+----------------------------+---------------------
1 | 2019-10-22 14:52:31.695282 | cron based snapshot
(1 row)
End snapshot :
snapid | ts | description
--------+----------------------------+---------------------
2 | 2019-10-22 14:54:36.205711 | cron based snapshot
(1 row)
Seconds in snapshot: 124.510429
Database version
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
(1 row)
Database information
current_database | dbsize
------------------+--------
peiybdb | 15 MB
(1 row)
Database statistics
database | tps | hitrate | lio_ps | pio_ps | rollbk_ps
-----------+------+---------+--------+--------+-----------
peiybdb | 0.10 | 96.00 | 61.52 | 1.85 | 0.00
postgres | 0.05 | 99.00 | 2.14 | 0.00 | 0.00
template1 | 0.03 | 99.00 | 1.17 | 0.00 | 0.00
template0 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
(4 rows)
Top 20 tables ordered by table size changes
table | table_growth | index_growth
------------------------------------+--------------+--------------
pg_catalog.pg_enum | | 0
pg_catalog.pg_foreign_server | | 0
pg_catalog.pg_default_acl | | 0
pg_catalog.pg_depend | | 0
pg_catalog.pg_extension | | 0
pg_catalog.pg_foreign_data_wrapper | | 0
pg_catalog.pg_aggregate | | 0
pg_catalog.pg_database | | 0
pg_catalog.pg_amop | | 0
pg_catalog.pg_amproc | | 0
pg_catalog.pg_event_trigger | | 0
pg_catalog.pg_attribute | | 0
pg_catalog.pg_authid | | 0
pg_catalog.pg_auth_members | | 0
pg_catalog.pg_cast | | 0
pg_catalog.pg_class | | 0
pg_catalog.pg_collation | | 0
pg_catalog.pg_am | | 0
pg_catalog.pg_conversion | | 0
pg_catalog.pg_foreign_table | | 0
(20 rows)
Top 20 tables ordered by high table to index read ratio
table | system_read_pct | table_read_pct | index_read_pct
--------------------------+-----------------+----------------+----------------
pg_catalog.pg_class | 38 | 91 | 8
pg_catalog.pg_proc | 31 | 98 | 1
public.pgstatspack_names | 13 | 99 | 0
pg_catalog.pg_index | 7 | 92 | 7
pg_catalog.pg_opclass | 3 | 0 | 100
pg_catalog.pg_attribute | 3 | 0 | 100
pg_catalog.pg_am | 0 | 100 | 0
pg_catalog.pg_namespace | 0 | 94 | 5
pg_catalog.pg_database | 0 | 56 | 43
pg_catalog.pg_authid | 0 | 0 | 100
pg_catalog.pg_language | 0 | 0 | 100
pg_catalog.pg_aggregate | 0 | 0 | 100
pg_catalog.pg_attrdef | 0 | 0 | 100
pg_catalog.pg_operator | 0 | 0 | 100
pg_catalog.pg_amproc | 0 | 0 | 100
pg_catalog.pg_rewrite | 0 | 0 | 100
pg_catalog.pg_sequence | 0 | 0 | 100
pg_catalog.pg_statistic | 0 | 0 | 100
pg_catalog.pg_tablespace | 0 | 0 | 100
pg_catalog.pg_type | 0 | 0 | 100
(20 rows)
Top 20 tables ordered by inserts
table | table_inserts
----------------------------------------+---------------
public.pgstatspack_names | 325
public.pgstatspack_indexes | 146
public.pgstatspack_tables | 101
public.pgstatspack_settings | 39
pg_catalog.pg_statistic | 29
public.pgstatspack_database | 4
public.pgstatspack_sequences | 2
public.pgstatspack_snap | 1
public.pgstatspack_bgwriter | 1
information_schema.sql_sizing_profiles | 0
pg_catalog.pg_attribute | 0
pg_catalog.pg_attrdef | 0
pg_catalog.pg_authid | 0
pg_catalog.pg_auth_members | 0
information_schema.sql_sizing | 0
information_schema.sql_languages | 0
pg_catalog.pg_amproc | 0
pg_catalog.pg_conversion | 0
pg_catalog.pg_amop | 0
information_schema.sql_parts | 0
(20 rows)
Top 20 tables ordered by updates
table | table_updates
--------------------------------------------+---------------
information_schema.sql_implementation_info | 0
information_schema.sql_languages | 0
information_schema.sql_packages | 0
information_schema.sql_parts | 0
information_schema.sql_sizing | 0
information_schema.sql_sizing_profiles | 0
pg_catalog.pg_aggregate | 0
pg_catalog.pg_am | 0
pg_catalog.pg_amop | 0
pg_catalog.pg_amproc | 0
pg_catalog.pg_attrdef | 0
pg_catalog.pg_attribute | 0
pg_catalog.pg_authid | 0
pg_catalog.pg_auth_members | 0
pg_catalog.pg_cast | 0
pg_catalog.pg_class | 0
pg_catalog.pg_collation | 0
pg_catalog.pg_constraint | 0
pg_catalog.pg_conversion | 0
information_schema.sql_features | 0
(20 rows)
Top 20 tables ordered by deletes
table | table_deletes
--------------------------------------------+---------------
information_schema.sql_implementation_info | 0
information_schema.sql_languages | 0
information_schema.sql_packages | 0
information_schema.sql_parts | 0
information_schema.sql_sizing | 0
information_schema.sql_sizing_profiles | 0
pg_catalog.pg_aggregate | 0
pg_catalog.pg_am | 0
pg_catalog.pg_amop | 0
pg_catalog.pg_amproc | 0
pg_catalog.pg_attrdef | 0
pg_catalog.pg_attribute | 0
pg_catalog.pg_authid | 0
pg_catalog.pg_auth_members | 0
pg_catalog.pg_cast | 0
pg_catalog.pg_class | 0
pg_catalog.pg_collation | 0
pg_catalog.pg_constraint | 0
pg_catalog.pg_conversion | 0
information_schema.sql_features | 0
(20 rows)
Tables ordered by percentage of tuples scanned
table | rows_read_pct | tab_hitrate | idx_hitrate | tab_read | tab_hit | idx_read | idx_hit
--------------------------+---------------+-------------+-------------+----------+---------+----------+---------
pg_catalog.pg_class | 38 | 99 | 99 | 1 | 846 | 3 | 1495
pg_catalog.pg_proc | 31 | 72 | 94 | 69 | 182 | 9 | 178
public.pgstatspack_names | 13 | 98 | 99 | 5 | 362 | 7 | 817
pg_catalog.pg_index | 7 | 97 | 99 | 2 | 143 | 0 | 110
pg_catalog.pg_attribute | 3 | 90 | 99 | 30 | 301 | 3 | 648
pg_catalog.pg_opclass | 3 | 99 | 98 | 0 | 274 | 0 | 51
pg_catalog.pg_cast | 0 | 97 | 99 | 0 | 42 | 0 | 214
pg_catalog.pg_database | 0 | 97 | 97 | 0 | 37 | 0 | 44
pg_catalog.pg_language | 0 | 0 | 0 | 1 | 0 | 2 | 0
pg_catalog.pg_aggregate | 0 | 50 | 66 | 0 | 1 | 0 | 2
pg_catalog.pg_operator | 0 | 81 | 91 | 14 | 65 | 5 | 65
pg_catalog.pg_rewrite | 0 | 30 | 76 | 8 | 4 | 2 | 10
pg_catalog.pg_sequence | 0 | 33 | 25 | 1 | 1 | 2 | 1
pg_catalog.pg_statistic | 0 | 82 | 97 | 10 | 52 | 2 | 120
pg_catalog.pg_tablespace | 0 | 75 | 83 | 0 | 3 | 0 | 5
pg_catalog.pg_type | 0 | 94 | 98 | 3 | 64 | 1 | 148
pg_toast.pg_toast_1255 | 0 | 33 | 25 | 1 | 1 | 2 | 1
pg_toast.pg_toast_2618 | 0 | 9 | 66 | 9 | 1 | 2 | 6
pg_toast.pg_toast_2619 | 0 | 0 | 66 | 1 | 0 | 0 | 2
pg_catalog.pg_namespace | 0 | 87 | 90 | 1 | 14 | 0 | 9
pg_catalog.pg_am | 0 | 75 | 0 | 0 | 3 | 0 | 0
pg_catalog.pg_amop | 0 | 96 | 99 | 2 | 80 | 0 | 114
pg_catalog.pg_amproc | 0 | 96 | 98 | 0 | 26 | 0 | 55
pg_catalog.pg_attrdef | 0 | 33 | 40 | 1 | 1 | 2 | 2
pg_catalog.pg_authid | 0 | 83 | 90 | 0 | 5 | 0 | 10
(25 rows)
Indexes ordered by scans
index | table | scans | tup_read | tup_fetch | idx_blks_read | idx_blks_hit
----------------------------------------------------------+-------------------------------+-------+----------+-----------+---------------+--------------
pg_catalog.pg_class_oid_index | pg_catalog.pg_class | 709 | 709 | 574 | 0 | 1421
pg_catalog.pg_attribute_relid_attnum_index | pg_catalog.pg_attribute | 323 | 754 | 754 | 3 | 648
pg_catalog.pg_cast_source_target_index | pg_catalog.pg_cast | 212 | 42 | 42 | 0 | 214
pg_catalog.pg_proc_oid_index | pg_catalog.pg_proc | 86 | 86 | 86 | 3 | 170
pg_catalog.pg_index_indexrelid_index | pg_catalog.pg_index | 85 | 85 | 81 | 0 | 88
pg_catalog.pg_type_oid_index | pg_catalog.pg_type | 64 | 63 | 63 | 0 | 130
pg_catalog.pg_statistic_relid_att_inh_index | pg_catalog.pg_statistic | 44 | 11 | 11 | 2 | 120
pg_catalog.pg_amop_fam_strat_index | pg_catalog.pg_amop | 37 | 37 | 37 | 0 | 76
pg_catalog.pg_class_relname_nsp_index | pg_catalog.pg_class | 37 | 25 | 25 | 3 | 73
pg_catalog.pg_db_role_setting_databaseid_rol_index | pg_catalog.pg_db_role_setting | 32 | 0 | 0 | 0 | 32
pg_catalog.pg_opclass_oid_index | pg_catalog.pg_opclass | 30 | 30 | 30 | 0 | 33
pg_catalog.pg_amproc_fam_proc_index | pg_catalog.pg_amproc | 26 | 46 | 46 | 0 | 55
pg_catalog.pg_index_indrelid_index | pg_catalog.pg_index | 20 | 28 | 28 | 0 | 22
pg_catalog.pg_operator_oid_index | pg_catalog.pg_operator | 20 | 20 | 20 | 0 | 42
pg_catalog.pg_database_oid_index | pg_catalog.pg_database | 18 | 18 | 18 | 0 | 26
pg_catalog.pg_amop_opr_fam_index | pg_catalog.pg_amop | 18 | 45 | 45 | 0 | 38
pg_catalog.pg_opclass_am_name_nsp_index | pg_catalog.pg_opclass | 16 | 684 | 684 | 0 | 18
pg_catalog.pg_operator_oprname_l_r_n_index | pg_catalog.pg_operator | 13 | 81 | 81 | 5 | 23
public.idx_pgstatspack_names_name | public.pgstatspack_names | 12 | 6 | 6 | 5 | 491
pg_catalog.pg_rewrite_rel_rulename_index | pg_catalog.pg_rewrite | 11 | 13 | 13 | 2 | 10
pg_catalog.pg_database_datname_index | pg_catalog.pg_database | 10 | 10 | 10 | 0 | 18
pg_catalog.pg_type_typname_nsp_index | pg_catalog.pg_type | 9 | 4 | 4 | 1 | 18
pg_catalog.pg_statistic_ext_relid_index | pg_catalog.pg_statistic_ext | 9 | 0 | 0 | 0 | 9
pg_toast.pg_toast_2618_index | pg_toast.pg_toast_2618 | 7 | 22 | 22 | 2 | 6
pg_catalog.pg_proc_proname_args_nsp_index | pg_catalog.pg_proc | 6 | 21 | 21 | 6 | 8
pg_catalog.pg_namespace_nspname_index | pg_catalog.pg_namespace | 5 | 4 | 2 | 0 | 6
pg_catalog.pg_constraint_conrelid_contypid_conname_index | pg_catalog.pg_constraint | 4 | 0 | 0 | 2 | 3
pg_catalog.pg_authid_oid_index | pg_catalog.pg_authid | 3 | 3 | 3 | 0 | 6
pg_catalog.pg_tablespace_oid_index | pg_catalog.pg_tablespace | 3 | 3 | 3 | 0 | 5
pg_catalog.pg_authid_rolname_index | pg_catalog.pg_authid | 2 | 2 | 2 | 0 | 4
pg_toast.pg_toast_1255_index | pg_toast.pg_toast_1255 | 2 | 4 | 4 | 2 | 1
pg_catalog.pg_attrdef_adrelid_adnum_index | pg_catalog.pg_attrdef | 2 | 2 | 2 | 2 | 2
pg_catalog.pg_sequence_seqrelid_index | pg_catalog.pg_sequence | 2 | 2 | 2 | 2 | 1
pg_catalog.pg_namespace_oid_index | pg_catalog.pg_namespace | 1 | 1 | 1 | 0 | 3
pg_catalog.pg_aggregate_fnoid_index | pg_catalog.pg_aggregate | 1 | 1 | 1 | 0 | 2
pg_toast.pg_toast_2619_index | pg_toast.pg_toast_2619 | 1 | 1 | 1 | 0 | 2
pg_catalog.pg_language_oid_index | pg_catalog.pg_language | 1 | 1 | 1 | 2 | 0
pg_catalog.pg_class_tblspc_relfilenode_index | pg_catalog.pg_class | 0 | 0 | 0 | 0 | 1
public.pgstatspack_bgwriter_pk | public.pgstatspack_bgwriter | 0 | 0 | 0 | 2 | 0
public.pgstatspack_database_pk | public.pgstatspack_database | 0 | 0 | 0 | 2 | 4
public.pgstatspack_indexes_pk | public.pgstatspack_indexes | 0 | 0 | 0 | 2 | 146
public.pgstatspack_names_pkey | public.pgstatspack_names | 0 | 0 | 0 | 2 | 326
public.pgstatspack_sequences_pk | public.pgstatspack_sequences | 0 | 0 | 0 | 2 | 2
public.pgstatspack_tables_pk | public.pgstatspack_tables | 0 | 0 | 0 | 2 | 101
public.pgstatspack_settings_pk | public.pgstatspack_settings | 0 | 0 | 0 | 2 | 39
(45 rows)
Sequences ordered by blks_read
sequence | blks_read | blks_hit
--------------------------+-----------+----------
public.pgstatspackid | 1 | 0
public.pgstatspacknameid | 1 | 324
(2 rows)
Top 20 SQL statements ordered by total_time
calls | total_time | total_time_percent | rows | user | query
-------+------------+--------------------+------+------+-------
(0 rows)
Top 20 user functions ordered by total_time
funcid | function_name | calls | total_time | self_time
--------+---------------+-------+------------+-----------
(0 rows)
background writer stats
checkpoints_timed | checkpoints_req | buffers_checkpoint | buffers_clean | maxwritten_clean | buffers_backend | buffers_alloc
-------------------+-----------------+--------------------+---------------+------------------+-----------------+---------------
0 | 0 | 0 | 0 | 0 | 0 | 230
(1 row)
background writer relative stats
checkpoints_timed | minutes_between_checkpoint | buffers_checkpoint | buffers_clean | buffers_backend | total_writes | avg_checkpoint_write
-------------------+----------------------------+--------------------+---------------+-----------------+--------------+----------------------
| | | | | 0.000 MB/s |
(1 row)
Parameters
name | start_setting | stop_setting | source
----------------------------+----------------------------------------+----------------------------------------+----------------------
DateStyle | ISO, MDY | ISO, MDY | configuration file
log_filename | postgresql-%a.log | postgresql-%a.log | configuration file
log_directory | log | log | configuration file
max_connections | 100 | 100 | configuration file
wal_segment_size | 16777216 | 16777216 | override
config_file | /var/lib/pgsql/11/data/postgresql.conf | /var/lib/pgsql/11/data/postgresql.conf | override
application_name | psql | psql | client
log_timezone | Asia/Shanghai | Asia/Shanghai | configuration file
shared_preload_libraries | pg_stat_statements | pg_stat_statements | configuration file
hba_file | /var/lib/pgsql/11/data/pg_hba.conf | /var/lib/pgsql/11/data/pg_hba.conf | override
lc_ctype | en_US.UTF-8 | en_US.UTF-8 | override
lc_messages | en_US.UTF-8 | en_US.UTF-8 | configuration file
max_stack_depth | 2048 | 2048 | environment variable
TimeZone | Asia/Shanghai | Asia/Shanghai | configuration file
max_wal_size | 1024 | 1024 | configuration file
min_wal_size | 80 | 80 | configuration file
lc_numeric | en_US.UTF-8 | en_US.UTF-8 | configuration file
transaction_deferrable | off | off | override
data_directory | /var/lib/pgsql/11/data | /var/lib/pgsql/11/data | override
client_encoding | UTF8 | UTF8 | client
data_checksums | off | off | override
transaction_read_only | off | off | override
log_rotation_age | 1440 | 1440 | configuration file
default_text_search_config | pg_catalog.english | pg_catalog.english | configuration file
ident_file | /var/lib/pgsql/11/data/pg_ident.conf | /var/lib/pgsql/11/data/pg_ident.conf | override
listen_addresses | * | * | configuration file
server_encoding | UTF8 | UTF8 | override
log_truncate_on_rotation | on | on | configuration file
lc_time | en_US.UTF-8 | en_US.UTF-8 | configuration file
wal_buffers | 512 | 512 | override
lc_collate | en_US.UTF-8 | en_US.UTF-8 | override
shared_buffers | 16384 | 16384 | configuration file
dynamic_shared_memory_type | posix | posix | configuration file
logging_collector | on | on | configuration file
log_destination | stderr | stderr | configuration file
log_line_prefix | %m [%p] | %m [%p] | configuration file
transaction_isolation | read committed | read committed | override
log_rotation_size | 0 | 0 | configuration file
lc_monetary | en_US.UTF-8 | en_US.UTF-8 | configuration file
(39 rows)
This report is saved as /tmp/pgstatreport_peiybdb_1_2.txt
~/pgstatspack/bin
参考:
http://pgfoundry.org/projects/pgstatspack/