pg_stat_monitor
1. Overview
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.
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.
2. Installation
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:
To build pg_stat_monitor from source code, you require the following:
-
git
-
make
-
gcc
-
pg_config
You can download the source code of the latest release of pg_stat_monitor from the releases page on GitHub or using git:
git clone https://github.com/percona/pg_stat_monitor.git
Compile and install the extension. Assuming IvorySQL is already installed in the environment with the installation path at /usr/ivory-5.
cd pg_stat_monitor
export PG_CONFIG=/usr/ivory-5/bin/pg_config
make USE_PGXS=1
make USE_PGXS=1 install
2.1. Building from source on Windows (PostgreSQL 19)
The PostgreSQL 19 development version of IvorySQL requires an extension version that supports PostgreSQL 19 interfaces. This procedure tests pg_stat_monitor 2.4.1 with commit 7c1742cde16b from the IvorySQL Windows port pull request. The build uses the Visual Studio 2022 C/C++ toolchain, Git, Perl, winflexbison3, Meson, and Ninja.
git clone https://github.com/IvorySQL/IvorySQL.git IvorySQL
Set-Location IvorySQL
git fetch https://github.com/Muzzammil242/IvorySQL.git 7c1742cde16b699045c94fe053cc8798c1e093cd
git checkout --detach FETCH_HEAD
git clone --depth 1 --branch 2.4.1 https://github.com/percona/pg_stat_monitor.git contrib/pg_stat_monitor
In pgsm_startup() in contrib/pg_stat_monitor/src/hash_query.c, move the preprocessor conditional out of the argument list for dsa_create_in_place(). Declare int tranche_id and preserve the version condition. Assign the return value of LWLockNewTrancheId("pg_stat_monitor_dsa") for PostgreSQL 19 and later, or LWLockNewTrancheId() for earlier versions. Pass tranche_id as the third argument in the original call. This avoids MSVC error C2059 when preprocessing the macro invocation.
Add-Content contrib/meson.build "`nsubdir('pg_stat_monitor')"
meson setup build --vsenv --auto-features=disabled -Duuid=none -Dssl=none -Dbuildtype=release --prefix "$PWD/pg-install"
meson compile -C build pg_stat_monitor
meson install -C build
Use the test installation under pg-install to preload the module and run CREATE EXTENSION as described below. Run SELECT 42 AS monitor_smoke; in one IPv4 session on the PostgreSQL listener. From a separate session, confirm that the statistics view contains the query with client_ip set to 127.0.0.1. The SQL extension version for release 2.4.1 is 2.4. See the Windows build and startup test.
3. Load the module
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.
modify the shared_preload_libraries parameter:
shared_preload_libraries = 'pg_stat_monitor';
| 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. |
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements, pg_stat_monitor';
Start or restart the IvorySQL instance to apply the changes.
pg_ctl restart -D data
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.
4. Create the extension view
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:
CREATE EXTENSION pg_stat_monitor;
After the setup is complete, you can see the stats collected by pg_stat_monitor.
5. Usage
To obtain query execution time information as an example, connect to the database and execute the following SQL:
SELECT userid, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, query FROM pg_stat_monitor;
userid | total_exec_time | min_exec_time | max_exec_time | mean_exec_time | query
--------+-----------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------
10 | 1.532168 | 0.749108 | 0.78306 | 0.766084 | SELECT userid, datname, queryid, substr(query,$1, $2) AS query, calls FROM pg_stat_monitor
10 | 0.755857 | 0.755857 | 0.755857 | 0.755857 | SELECT application_name, client_ip, substr(query,$1,$2) as query FROM pg_stat_monitor
10 | 0 | 0 | 0 | 0 | SELECT userid, total_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
10 | 0 | 0 | 0 | 0 | SELECT userid, total_exec_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
(4 rows)
For more information on using pg_stat_monitor, please refer to pg_stat_monitor Official documentation