plprofiler 定位执行慢的代码位置

os: ubuntu 16.04
db: postgresql 10.6

plprofiler 可以找到执行慢的代码位置,对于定位性能问题非常棒.

版本

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:        16.04
Codename:       xenial
# 
# 
# su - postgres
$ psql -c "select version();"
                                                     version
-----------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609, 64-bit
(1 row)

下载安装

安装依赖

# apt install python python-dev

# cd /root;
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py;
python get-pip.py;

# pip install setuptools;
pip install --upgrade setuptools;

# pip install psycopg2

# cd /home/postgres
# wget https://github.com/pinkonio/plprofiler/archive/REL3_5.zip
# unzip ./REL3_5.zip
# cd /home/postgres/plprofiler-REL3_5/python-plprofiler
# python ./setup.py install

Installed /usr/local/lib/python2.7/dist-packages/psycopg2-2.8.3-py2.7-linux-x86_64.egg
Finished processing dependencies for plprofiler==3.5


# which plprofiler
/usr/local/bin/plprofiler

make
make install

# cd /home/postgres/plprofiler-REL3_5/
# export PATH=/usr/pgsql-10/bin:$PATH

# USE_PGXS=1 make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O2 -fPIC -I. -I./ -I/usr/pgsql-10/include/server -I/usr/pgsql-10/include/internal  -D_GNU_SOURCE -I/usr/include/libxml2   -c -o plprofiler.o plprofiler.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O2 -fPIC -shared -o plprofiler.so plprofiler.o -L/usr/pgsql-10/lib    -Wl,--as-needed -Wl,-rpath,'/usr/pgsql-10/lib',--enable-new-dtags 
 
# USE_PGXS=1 make install

/bin/mkdir -p '/usr/pgsql-10/lib'
/bin/mkdir -p '/usr/pgsql-10/share/extension'
/bin/mkdir -p '/usr/pgsql-10/share/extension'
/usr/bin/install -c -m 755  plprofiler.so '/usr/pgsql-10/lib/plprofiler.so'
/usr/bin/install -c -m 644 .//plprofiler.control '/usr/pgsql-10/share/extension/'
/usr/bin/install -c -m 644 .//plprofiler--1.0--2.0.sql .//plprofiler--2.0--3.0.sql .//plprofiler--3.0--3.5.sql .//plprofiler--3.5.sql  '/usr/pgsql-10/share/extension/'

不需要修改参数 shared_preload_libraries ,不需要重启 postgresql

$ psql
psql (10.6)
Type "help" for help.

postgres=# select * from pg_available_extensions where name like '%profile%';
    name    | default_version | installed_version |                       comment                        
------------+-----------------+-------------------+------------------------------------------------------
 plprofiler | 3.5             |                   | server-side support for profiling PL/pgSQL functions
(1 row)

使用

下载的源码包含 example

# su - postgres
$ cd ~/plprofiler-REL3_5/examples
$ ls -l 
total 44
-rw-rw-r-- 1 postgres postgres  534 Apr  5 05:19 pgbench_default-9.6.profile
-rw-rw-r-- 1 postgres postgres  594 Apr  5 05:19 pgbench_default.profile
-rw-rw-r-- 1 postgres postgres  169 Apr  5 05:19 pgbench_pl-9.6.profile
-rw-rw-r-- 1 postgres postgres  271 Apr  5 05:19 pgbench_pl.collect-9.6.profile
-rw-rw-r-- 1 postgres postgres  331 Apr  5 05:19 pgbench_pl.collect.profile
-rw-rw-r-- 1 postgres postgres  240 Apr  5 05:19 pgbench_pl.interval-9.6.profile
-rw-rw-r-- 1 postgres postgres  300 Apr  5 05:19 pgbench_pl.interval.profile
-rw-rw-r-- 1 postgres postgres  229 Apr  5 05:19 pgbench_pl.profile
-rw-rw-r-- 1 postgres postgres 1853 Apr  5 05:19 pgbench_pl.sql
-rwxr-xr-x 1 postgres postgres 1824 Apr  5 05:19 prepdb.sh
-rw-rw-r-- 1 postgres postgres  511 Apr  5 05:19 tpcb_queries.sql

初始化,先创建 pgbench_plprofiler 数据库

$ psql -c "create database pgbench_plprofiler;"
$ ~/plprofiler-REL3_5/examples/prepdb.sh
$
$
$ psql pgbench_plprofiler
psql (10.6)
Type "help" for help.

pgbench_plprofiler=# \dx
                                     List of installed extensions
        Name        | Version |   Schema   |                        Description                      
  
--------------------+---------+------------+-----------------------------------------------------------
 plpgsql            | 1.0     | pg_catalog | PL/pgSQL procedural language
 plprofiler         | 3.5     | public     | server-side support for profiling PL/pgSQL functions
(2 rows)

pgbench_plprofiler=# \du+ 
                                           List of roles
 Role name  |                         Attributes                         | Member of | Description 
------------+------------------------------------------------------------+-----------+-------------
 plprofiler | Cannot login                                               | {}        | 
 postgres   | Superuser, Create role, Create DB, Replication, Bypass RLS | {}        | 

pgbench_plprofiler=# \d+
                                    List of relations
 Schema |            Name             |   Type   |   Owner    |    Size    | Description 
--------+-----------------------------+----------+------------+------------+-------------
 public | pgbench_accounts            | table    | postgres   | 1302 MB    | 
 public | pgbench_branches            | table    | postgres   | 40 kB      | 
 public | pgbench_category_seq        | sequence | postgres   | 8192 bytes | 
 public | pgbench_history             | table    | postgres   | 0 bytes    | 
 public | pgbench_tellers             | table    | postgres   | 40 kB      | 
 public | pl_profiler_saved           | table    | plprofiler | 8192 bytes | 
 public | pl_profiler_saved_callgraph | table    | plprofiler | 8192 bytes | 
 public | pl_profiler_saved_functions | table    | plprofiler | 8192 bytes | 
 public | pl_profiler_saved_linestats | table    | plprofiler | 8192 bytes | 
 public | pl_profiler_saved_s_id_seq  | sequence | plprofiler | 8192 bytes |
(10 rows)
 

plprofiler 操作

$ plprofiler --help

usage: plprofiler COMMAND [OPTIONS]

    plprofiler is a command line tool to control the plprofiler extension
    for PostgreSQL.

    The input of this utility are the call and execution statistics, the
    plprofiler extension collects. The final output is an HTML report of
    the statistics gathered. There are several ways to collect the data,
    save the data permanently and even transport it from a production
    system to a lab system for offline analysis.

    Use

        plprofiler COMMAND --help

    for detailed information about one of the commands below.

GENERAL OPTIONS:

    All commands implement the following command line options to specify
    the target database:

        -h, --host=HOST     The host name of the database server.

        -p, --port=PORT     The PostgreSQL port number.

        -U, --user=USER     The PostgreSQL user name to connect as.

        -d, --dbname=DB     The PostgreSQL database name or the DSN.
                            plprofiler currently uses psycopg2 to connect
                            to the target database. Since that is based
                            on libpq, all the above parameters can also
                            be specified in this option with the usual
                            conninfo string or URI formats.

        --help              Print the command specific help information
                            and exit.

TERMS:

    The following terms are used in the text below and the help output of
    individual commands:

    local-data      By default the plprofiler extension collects run-time
                    data in per-backend hashtables (in-memory). This data is
                    only accessible in the current session and is lost when
                    the session ends or the hash tables are explicitly reset.

    shared-data     The plprofiler extension can copy the local-data
                    into shared hashtables, to make the statistics available
                    to other sessions. See the "monitor" command for
                    details. This data still relies on the local database's
                    system catalog to resolve Oid values into object
                    definitions.

    saved-dataset   The local-data as well as the shared-data can
                    be turned into a named, saved dataset. These sets
                    can be exported and imported onto other machines.
                    The saved datasets are independent of the system
                    catalog, so a report can be generated again later,
                    even even on a different system.


COMMANDS:

    run             Runs one or more SQL statements with the plprofiler
                    extension enabled and creates a saved-dataset and/or
                    an HTML report from the local-data.

    monitor         Monitors a running application for a requested time
                    and creates a saved-dataset and/or an HTML report from
                    the resulting shared-data.

    reset           Deletes the data from shared hash tables.

    save            Saves the current shared-data as a saved-dataset.

    list            Lists the available saved-datasets.

    edit            Edits the metadata of one saved-dataset. The metadata
                    is used in the generation of the HTML reports.

    report          Generates an HTML report from either a saved-dataset
                    or the shared-data.

    delete          Deletes a saved-dataset.

    export          Exports one or all saved-datasets into a JSON file.

    import          Imports the saved-datasets from a JSON file, created
                    with the export command.
					
$ plprofiler run --command "SELECT tpcb(1, 2, 3, -42)" \
--output /tmp/tpcb_test1.html -d pgbench_plprofiler

$ plprofiler run --file /home/postgres/plprofiler-REL3_5/examples/tpcb_queries.sql \
--output /tmp/tpcb_queries.html -d pgbench_plprofiler

会话级别设置 plprofiler

SET plprofiler.enabled TO true;
SET plprofiler.collect_interval TO 10;
SELECT tpcb(1, 2, 3, -42);

用户级别设置默认的plprofiler
需要配置 shared_preload_libraries,并重启 postgresql

$ vi $PGDATA/postgresql.conf
shared_preload_libraries = 'plprofiler'

$ pg_ctl stop -m fast -D /data/pgsql-10/data
$ 
$ 
$ pg_ctl start -D /data/pgsql-10/data

ALTER USER myuser SET plprofiler.enabled TO on;
ALTER USER myuser SET plprofiler.collect_interval TO 10;

特定进程设置监控

$ plprofiler reset
$ plprofiler monitor --pid  --interval 10 --duration 300
$ plprofiler report --from-shared --name tpcb-using-monitor --output tpcb-using-monitor.html

参考:
https://github.com/pinkonio/plprofiler

报告如下,还是很容易看懂
plprofiler 定位执行慢的代码位置_第1张图片

你可能感兴趣的:(#,postgresql,extension,postgresql,extension,plprofiler)