|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += pg_stat_monitor |
| 5 | + |
| 6 | +== Overview |
| 7 | + |
| 8 | +pg_stat_monitor is a Query Performance Monitoring tool for PostgreSQL. pg_stat_monitor collects performance statistics and provides query performance insights in a single view and graphically in histogram. |
| 9 | + |
| 10 | +These insights allow database users to understand query origins, execution, planning statistics and details, query information, and metadata. This significantly improves observability, enabling users to debug and tune query performance. |
| 11 | + |
| 12 | +== Installation |
| 13 | + |
| 14 | +The IvorySQL installation package already includes the pg_stat_monitor plugin. If you installed IvorySQL using the installation package, you typically don't need to manually install pg_stat_monitor. If you installed IvorySQL from source code, you can proceed to install the pg_stat_monitor plugin also from source. The IvorySQL community provides step-by-step instructions for source code installation: |
| 15 | + |
| 16 | +To build pg_stat_monitor from source code, you require the following: |
| 17 | + |
| 18 | +* git |
| 19 | +* make |
| 20 | +* gcc |
| 21 | +* pg_config |
| 22 | + |
| 23 | +You can download the source code of the latest release of pg_stat_monitor from https://github.com/Percona/pg_stat_monitor/releases[the releases page on GitHub] or using git: |
| 24 | + |
| 25 | +[source,bash] |
| 26 | +---- |
| 27 | +git clone https://github.com/percona/pg_stat_monitor.git |
| 28 | +---- |
| 29 | + |
| 30 | +Compile and install the extension. Assuming IvorySQL is already installed in the environment with the installation path at /usr/ivory-5. |
| 31 | + |
| 32 | +[source,bash] |
| 33 | +---- |
| 34 | +cd pg_stat_monitor |
| 35 | +export PG_CONFIG=/usr/ivory-5/bin/pg_config |
| 36 | +make USE_PGXS=1 |
| 37 | +make USE_PGXS=1 install |
| 38 | +---- |
| 39 | + |
| 40 | +== Load the module |
| 41 | + |
| 42 | +Load pg_stat_monitor at the start time by adding it to the shared_preload_libraries configuration parameter. This is because pg_stat_monitor requires additional shared memory. |
| 43 | + |
| 44 | +modify the shared_preload_libraries parameter: |
| 45 | + |
| 46 | +[source,conf] |
| 47 | +---- |
| 48 | +shared_preload_libraries = 'pg_stat_monitor'; |
| 49 | +---- |
| 50 | + |
| 51 | +NOTE: If you’ve added other modules to the shared_preload_libraries parameter (for example, pg_stat_statements), list all of them separated by commas for the ALTER SYSTEM command. |
| 52 | + |
| 53 | +[source,conf] |
| 54 | +---- |
| 55 | +shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements, pg_stat_monitor'; |
| 56 | +---- |
| 57 | + |
| 58 | +Start or restart the IvorySQL instance to apply the changes. |
| 59 | + |
| 60 | +[source,bash] |
| 61 | +---- |
| 62 | +pg_ctl restart -D data |
| 63 | +---- |
| 64 | + |
| 65 | +After you have added pg_stat_monitor to the shared_preload_libraries, it starts collecting statistics data for all existing databases. To access this data, you need to create the view on every database that you wish to monitor. |
| 66 | + |
| 67 | +== Create the extension view |
| 68 | + |
| 69 | +Create the extension view with the user that has the privileges of a superuser or a database owner. Connect to psql as a superuser for a database and run the CREATE EXTENSION command: |
| 70 | + |
| 71 | +[source,sql] |
| 72 | +---- |
| 73 | +CREATE EXTENSION pg_stat_monitor; |
| 74 | +---- |
| 75 | + |
| 76 | +After the setup is complete, you can see the stats collected by pg_stat_monitor. |
| 77 | + |
| 78 | +== Usage |
| 79 | + |
| 80 | +To obtain query execution time information as an example, connect to the database and execute the following SQL: |
| 81 | + |
| 82 | +[source,sql] |
| 83 | +---- |
| 84 | +SELECT userid, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, query FROM pg_stat_monitor; |
| 85 | +---- |
| 86 | + |
| 87 | +[source,text] |
| 88 | +---- |
| 89 | +userid | total_exec_time | min_exec_time | max_exec_time | mean_exec_time | query |
| 90 | +--------+-----------------+---------------+---------------+----------------+---------------------------------------------------------------------------------------------- |
| 91 | + 10 | 1.532168 | 0.749108 | 0.78306 | 0.766084 | SELECT userid, datname, queryid, substr(query,$1, $2) AS query, calls FROM pg_stat_monitor |
| 92 | + 10 | 0.755857 | 0.755857 | 0.755857 | 0.755857 | SELECT application_name, client_ip, substr(query,$1,$2) as query FROM pg_stat_monitor |
| 93 | + 10 | 0 | 0 | 0 | 0 | SELECT userid, total_time, min_time, max_time, mean_time, query FROM pg_stat_monitor; |
| 94 | + 10 | 0 | 0 | 0 | 0 | SELECT userid, total_exec_time, min_time, max_time, mean_time, query FROM pg_stat_monitor; |
| 95 | +(4 rows) |
| 96 | +---- |
| 97 | + |
| 98 | +For more information on using pg_stat_monitor, please refer to pg_stat_monitor https://docs.percona.com/pg-stat-monitor/user_guide.html[ Official documentation] |
0 commit comments