pg_stat_statements扩展安装步骤说明

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介:

一.安装 pg_stat_statements

1.安装插件pg_stat_statements

[root@localhost soft]# cd postgresql-9.3.0

[root@localhost postgresql-9.3.0]# ls

aclocal.m4  config.log     configure     contrib    doc          GNUmakefile.in  INSTALL   README

config      config.status  configure.in  COPYRIGHT  GNUmakefile  HISTORY         Makefile  src

[root@localhost postgresql-9.3.0]# cd contrib/

[root@localhost contrib]# ls

adminpack          cube            hstore    pageinspect        pg_standby          pg_xlogdump    tablefunc    xml2

auth_delay         dblink          intagg    passwordcheck      pg_stat_statements  postgres_fdw   tcn

auto_explain       dict_int        intarray  pg_archivecleanup  pgstattuple         README         test_parser

btree_gin          dict_xsyn       isn       pgbench            pg_test_fsync       seg            tsearch2

btree_gist         dummy_seclabel  lo        pg_buffercache     pg_test_timing      sepgsql        unaccent

chkpass            earthdistance   ltree     pgcrypto           pg_trgm             spi            uuid-ossp

citext             file_fdw        Makefile  pg_freespacemap    pg_upgrade          sslinfo        vacuumlo

contrib-global.mk  fuzzystrmatch   oid2name  pgrowlocks         pg_upgrade_support  start-scripts  worker_spi

[root@localhost contrib]# cd pg_stat_statements/

[root@localhost pg_stat_statements]# ls

Makefile                          pg_stat_statements--1.1.sql  pg_stat_statements.control

pg_stat_statements--1.0--1.1.sql  pg_stat_statements.c         pg_stat_statements--unpackaged--1.0.sql

[root@localhost pg_stat_statements]# make 

gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -I. -I. -I../../src/include -D_GNU_SOURCE   -c -o pg_stat_statements.o pg_stat_statements.c

gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fpic -shared -o pg_stat_statements.so pg_stat_statements.o -L../../src/port -L../../src/common -Wl,--as-needed -Wl,-rpath,'/opt/pg/9.3/lib',--enable-new-dtags  

[root@localhost pg_stat_statements]# make install

/bin/mkdir -p '/opt/pg/9.3/lib/postgresql'

/bin/mkdir -p '/opt/pg/9.3/share/postgresql/extension'

/bin/mkdir -p '/opt/pg/9.3/share/postgresql/extension'

/usr/bin/install -c -m 755  pg_stat_statements.so '/opt/pg/9.3/lib/postgresql/pg_stat_statements.so'

/usr/bin/install -c -m 644 ./pg_stat_statements.control '/opt/pg/9.3/share/postgresql/extension/'

/usr/bin/install -c -m 644 ./pg_stat_statements--1.1.sql ./pg_stat_statements--1.0--1.1.sql ./pg_stat_statements--unpackaged--1.0.sql  '/opt/pg/9.3/share/postgresql/extension/'

[root@localhost pg_stat_statements]# 


2.修改配置文件

vi postgresql.conf


shared_preload_libraries = 'pg_stat_statements'


pg_stat_statements.max = 10000

pg_stat_statements.track = all


3.重新启动数据库


二、测试


1.建立测试数据库bench

[postgres@localhost data]$ createdb bench

2.重置计数

psql bench

bench=# SELECT pg_stat_statements_reset();

 pg_stat_statements_reset 

--------------------------


(1 row)


3.初始化测试库

[postgres@localhost data]$ pgbench -i bench

NOTICE:  table "pgbench_history" does not exist, skipping

NOTICE:  table "pgbench_tellers" does not exist, skipping

NOTICE:  table "pgbench_accounts" does not exist, skipping

NOTICE:  table "pgbench_branches" does not exist, skipping

creating tables...

100000 of 100000 tuples (100%) done (elapsed 0.72 s, remaining 0.00 s).

vacuum...

set primary keys...

done.

[postgres@localhost data]$ 


4.加载扩展

psql bench

bench=# CREATE EXTENSION pg_stat_statements;


卸载模块方法

bench=# DROP EXTENSION pg_stat_statements;

bench=# \dx

                                     List of installed extensions

        Name        | Version |   Schema   |                        Description     

                   

--------------------+---------+------------+----------------------------------------

-------------------

 pg_stat_statements | 1.1     | public     | track execution statistics of all SQL statements executed

 plpgsql            | 1.0     | pg_catalog | PL/pgSQL procedural language

(2 rows)


bench=# 


5.执行测试

[postgres@localhost data]$ pgbench -c10 -t300 bench

starting vacuum...end.

transaction type: TPC-B (sort of)

scaling factor: 1

query mode: simple

number of clients: 10

number of threads: 1

number of transactions per client: 300

number of transactions actually processed: 3000/3000

tps = 409.872797 (including connections establishing)

tps = 411.678326 (excluding connections establishing)

[postgres@localhost data]$ 


6.性能查询


SELECT query, calls, total_time, rows, 100.0 * shared_blks_hit /

               nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent

          FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;



本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1721988
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
SQL 关系型数据库 API
【PostgreSQL】PostgreSQL扩展:pg_stat_statements 优化SQL
【PostgreSQL】PostgreSQL扩展:pg_stat_statements 优化SQL
|
关系型数据库 数据库 PostgreSQL
pg_stat_statements 插件
pg_stat_statements 插件
172 0
|
关系型数据库 数据库 对象存储
PostgreSQL系统函数:pg_relation_filepath
pg_relation_filepath:数据对象存储位置 Name Return Type Description pg_relation_filepath text file path name of specified relation 创建表空间 postgres=# .
3253 0
|
SQL 监控 关系型数据库
|
SQL 存储 关系型数据库
|
SQL 关系型数据库 开发工具