PPAS下安装 pg_stat_statements过程记录

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

PostgreSQL中,如何安装pg_stat_statements,网上有很多这方面的论述。这里说说如何在PPAS下安装它:

实验一:首先,看是否可以不安装,直接拿来用:

复制代码
[root@rhjp001 ~]# su - enterprisedb
-bash-3.2$ pwd
/opt/PostgresPlus/9.2AS
-bash-3.2$ ./bin/psql -d edb
psql (9.2.1.3)
"help" でヘルプを表示します.

edb=# select count(*) from pg_stat_statements;
ERROR:  pg_stat_statements must be loaded via shared_preload_libraries
edb=# 
复制代码

实验二:直接创建:

edb=# create extension pg_stat_statments;
ERROR:  拡張機能の制御ファイル "/opt/PostgresPlus/9.2AS/share/extension/pg_stat_statments.control" をオープンできませんでした: そのようなファイルやディレクトリはありません
edb=# 

实验三:改配置文件后,再创建:

复制代码
-bash-3.2$ pwd
/opt/PostgresPlus/9.2AS/data
-bash-3.2$ vim postgresql.conf
-bash-3.2$ cat postgresql.conf | grep preload
shared_preload_libraries = '$libdir/dbms_pipe,$libdir/edb_gen,pg_stat_statements'
#local_preload_libraries = ''
-bash-3.2$ 
复制代码

重新启动后,创建,其实已经不用创建了。

复制代码
[root@rhjp001 ~]# su - enterprisedb
-bash-3.2$ pwd
/opt/PostgresPlus/9.2AS
-bash-3.2$ ./bin/psql -d edb
psql (9.2.1.3)
"help" でヘルプを表示します.

edb=# create extension pg_stat_statements;
ERROR:  拡張機能 "pg_stat_statements" はすでに存在します
edb=# 
复制代码
复制代码
[root@rhjp001 ~]# su - enterprisedb
-bash-3.2$ ./bin/psql -d edb
psql (9.2.1.3)
"help" でヘルプを表示します.

edb=# select count(*) from pg_stat_statements;
 count 
-------
    47
(1 行)

edb=# 
复制代码

也就是说,其实在PPAS安装好之后,如果想要使用 pg_stat_statements ,只要配置postgresql.conf的 shared_preload_libraries 就可以了。

卸载ppas,重新安装,然后再重新开始吧:

配置postgresql.conf:

把 shared_preload_libraries = '$libdir/dbms_pipe,$libdir/edb_gen'

改成:

shared_preload_libraries = '$libdir/dbms_pipe,$libdir/edb_gen,pg_stat_statements'

重新启动数据库: service ppas-9.2 restart

复制代码
-bash-3.2$ ./bin/psql -d edb
psql (9.2.1.3)
"help" でヘルプを表示します.

edb=# select count(*) from pg_stat_statements;
 count 
-------
    42
(1 行)

edb=# 
复制代码

这个pg_stat_statements,主要可以用于区分运行最慢的sql文:

例如:

复制代码
edb=# \d pg_stat_statements;
    ビュー "enterprisedb.pg_stat_statements"
         列          || 修飾語 
---------------------+------------------+--------
 userid              | oid              | 
 dbid                | oid              | 
 query               | text             | 
 calls               | bigint           | 
 total_time          | double precision | 
 rows                | bigint           | 
 shared_blks_hit     | bigint           | 
 shared_blks_read    | bigint           | 
 shared_blks_dirtied | bigint           | 
 shared_blks_written | bigint           | 
 local_blks_hit      | bigint           | 
 local_blks_read     | bigint           | 
 local_blks_dirtied  | bigint           | 
 local_blks_written  | bigint           | 
 temp_blks_read      | bigint           | 
 temp_blks_written   | bigint           | 
 blk_read_time       | double precision | 
 blk_write_time      | double precision | 

edb=# 
复制代码

查找最慢的10条sql文(这里用的是累计时间)

复制代码
edb=# select * from pg_stat_statements order by total_time desc limit 10;
-[ RECORD 1 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | drop extension pg_stat_statements;
calls               | 2
total_time          | 3099.557
rows                | 0
shared_blks_hit     | 673
shared_blks_read    | 27
shared_blks_dirtied | 9
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 2 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | create database gaodb owner gao;
calls               | 1
total_time          | 2068.82
rows                | 0
shared_blks_hit     | 54
shared_blks_read    | 4
shared_blks_dirtied | 7
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 3 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | CREATE TEMP TABLE pga_tmp_zombies(jagpid int4)
calls               | 3
total_time          | 1061.702
rows                | 0
shared_blks_hit     | 479
shared_blks_read    | 109
shared_blks_dirtied | 37
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 4 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | SELECT count(*) As count, pg_backend_pid() AS pid FROM pg_class cl JOIN pg_na
mespace ns ON ns.oid=relnamespace WHERE relname=? AND nspname=?
calls               | 3
total_time          | 929.614
rows                | 3
shared_blks_hit     | 6
shared_blks_read    | 15
shared_blks_dirtied | 0
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 5 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | SELECT COUNT(*)    FROM pg_proc  WHERE proname = ? AND pronamespace = (
SELECT oid    FROM pg_namespace  WHERE nspname = ?) AND prorettype = (SELECT oid   ROM pg_type  WHERE typnam e = ?) AND   proargtypes = ?                                     
calls               | 3
total_time          | 337.312
rows                | 3
shared_blks_hit     | 15
shared_blks_read    | 15
shared_blks_dirtied | 0
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 6 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | SELECT pgagent.pgagent_schema_version()
calls               | 3
total_time          | 321.264
rows                | 3
shared_blks_hit     | 12
shared_blks_read    | 12
shared_blks_dirtied | 0
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 7 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | DROP TABLE pga_tmp_zombies
calls               | 3
total_time          | 282.334
rows                | 0
shared_blks_hit     | 360
shared_blks_read    | 21
shared_blks_dirtied | 0
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 8 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 10
dbid                | 14000
query               | create extension pg_stat_statements;
calls               | 1
total_time          | 155.641
rows                | 0
shared_blks_hit     | 585
shared_blks_read    | 22
shared_blks_dirtied | 25
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 9 ]-------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 16684
dbid                | 16685
query               | create table gaotab(id integer);
calls               | 1
total_time          | 143.838
rows                | 0
shared_blks_hit     | 195
shared_blks_read    | 59
shared_blks_dirtied | 21
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0
-[ RECORD 10 ]------+------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
userid              | 16684
dbid                | 16685
query               | select pg_available_extensions();
calls               | 2
total_time          | 92.16
rows                | 100
shared_blks_hit     | 5
shared_blks_read    | 1
shared_blks_dirtied | 0
shared_blks_written | 0
local_blks_hit      | 0
local_blks_read     | 0
local_blks_dirtied  | 0
local_blks_written  | 0
temp_blks_read      | 0
temp_blks_written   | 0
blk_read_time       | 0
blk_write_time      | 0

edb=#  
复制代码

需要注意,只有管理员用户才可以看到这个视图。







本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/3257581.html,如需转载请自行联系原作者


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
关系型数据库 数据库 PostgreSQL
pg_stat_statements 插件
pg_stat_statements 插件
158 0
|
SQL 关系型数据库
利用pg_stat_activity做日常检查及异常SQL诊断
AnalyticDB for PostgreSQL(原HybridDB for PostgreSQL,以下简称ADB for PG)作为高性能分析型数据库,可以支持用户对其业务数据进行实时分析,能够让企业敏锐感知市场动态,做出必要决策。
3725 0
|
关系型数据库 数据库 PostgreSQL
|
SQL 监控 Go
SQL Server中如何识别、查找未使用的索引(unused indexes)
原文:SQL Server中如何识别、查找未使用的索引(unused indexes)   在SQL Server中,索引是优化SQL性能的一大法宝。但是由于各种原因,索引会被当做“银弹”滥用,一方面有些开发人员(甚至是部分数据库管理员)有一些陋习,不管三七二十一,总是根据所谓的"感觉"或“经验”先增加一些索引,而不管这些索引是否未被使用或是否合理。
1260 0
|
SQL 存储 关系型数据库