1. 程式人生 > >postgresql 查詢慢sql之二: pg_stat_statements

postgresql 查詢慢sql之二: pg_stat_statements

pg_stat_statements模組提供一種方法追蹤一個伺服器所執行的所有 SQL 語句的執行統計資訊。

該模組必須通過在 postgresql.conf 的 shared_preload_libraries 中增加pg_stat_statements 來載入,因為它需要額外的共享記憶體。這意味著增加或移除該模組需要一次伺服器重啟。

當 pg_stat_statements 被載入時,它會跟蹤該伺服器 的所有資料庫的統計資訊。該模組提供了一個檢視 pg_stat_statements 以及函式 pg_stat_statements_reset 和pg_stat_statements 用於訪問和操縱這些統計資訊。這些檢視 和函式不是全域性可用的,但是可以用 CREATE EXTENSION pg_stat_statements 為特定資料庫啟用它們。

apt 、yum 安裝

需要安裝相同版本的contrib包,以ubuntu為例。

# apt install postgresql-contrib-9.6
# dpkg -l |grep -i postgresql-contrib-9.6
ii  postgresql-contrib-9.6              9.6.8-1.pgdg16.04+1                        amd64        additional facilities for PostgreSQL

原始碼編譯安裝

編譯安裝時如果是如下方式,則已經安裝了。

$ make world
$ make install-world

否則,需要進入到原始碼的 \contrib\pg_stat_statements\ 下單獨編譯安裝

$ cd /tmp/postgresql-9.6.8\contrib\pg_stat_statements\
$ make 
$ make install

建立 pg_stat_statements

需要修改 postgrsql.conf 的 shared_preload_libraries 值後,重啟postgresql。

$ vi postgresql.conf
shared_preload_libraries = 'pg_stat_statements'         # (change requires restart)

# systemctl stop postgresql
# systemctl start postgresql

由於pg_stat_statements針對的是資料庫級別,所以需要首先進入指定資料庫

postgres=# \c peiybdb
peiybdb=# create extension pg_stat_statements;

建立好後,會多出一個檢視 pg_stat_statements

peiybdb=# \d
               List of relations
 Schema |        Name        | Type  |  Owner   
--------+--------------------+-------+----------
 public | pg_stat_statements | view  | postgres


peiybdb=# \d+ pg_stat_statements
                      View "public.pg_stat_statements"
       Column        |       Type       | Modifiers | Storage  | Description 
---------------------+------------------+-----------+----------+-------------
 userid              | oid              |           | plain    | 
 dbid                | oid              |           | plain    | 
 queryid             | bigint           |           | plain    | 
 query               | text             |           | extended | 
 calls               | bigint           |           | plain    | 
 total_time          | double precision |           | plain    | 
 min_time            | double precision |           | plain    | 
 max_time            | double precision |           | plain    | 
 mean_time           | double precision |           | plain    | 
 stddev_time         | double precision |           | plain    | 
 rows                | bigint           |           | plain    | 
 shared_blks_hit     | bigint           |           | plain    | 
 shared_blks_read    | bigint           |           | plain    | 
 shared_blks_dirtied | bigint           |           | plain    | 
 shared_blks_written | bigint           |           | plain    | 
 local_blks_hit      | bigint           |           | plain    | 
 local_blks_read     | bigint           |           | plain    | 
 local_blks_dirtied  | bigint           |           | plain    | 
 local_blks_written  | bigint           |           | plain    | 
 temp_blks_read      | bigint           |           | plain    | 
 temp_blks_written   | bigint           |           | plain    | 
 blk_read_time       | double precision |           | plain    | 
 blk_write_time      | double precision |           | plain    | 
View definition:
 SELECT pg_stat_statements.userid,
    pg_stat_statements.dbid,
    pg_stat_statements.queryid,
    pg_stat_statements.query,
    pg_stat_statements.calls,
    pg_stat_statements.total_time,
    pg_stat_statements.min_time,
    pg_stat_statements.max_time,
    pg_stat_statements.mean_time,
    pg_stat_statements.stddev_time,
    pg_stat_statements.rows,
    pg_stat_statements.shared_blks_hit,
    pg_stat_statements.shared_blks_read,
    pg_stat_statements.shared_blks_dirtied,
    pg_stat_statements.shared_blks_written,
    pg_stat_statements.local_blks_hit,
    pg_stat_statements.local_blks_read,
    pg_stat_statements.local_blks_dirtied,
    pg_stat_statements.local_blks_written,
    pg_stat_statements.temp_blks_read,
    pg_stat_statements.temp_blks_written,
    pg_stat_statements.blk_read_time,
    pg_stat_statements.blk_write_time
   FROM pg_stat_statements(true) pg_stat_statements(userid, dbid, queryid, query, calls, total_time, min_time, max_time, mean_time, stddev_time, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time, blk_write_time);

可以使用 pg_stat_statements_reset() 函式來重置 pg_stat_statements,方便階段性的分析慢sql,比如專項優化、大版本上線監控。

peiybdb=# select pg_stat_statements_reset();
 pg_stat_statements_reset 
--------------------------
 
(1 row)
peiybdb=#
peiybdb=#
peiybdb=# select count(1) from pg_stat_statements;
 count 
-------
     1
(1 row)