|
| 1 | +.. _pg_stat-monitor: |
| 2 | + |
| 3 | +pg_stat_monitor |
| 4 | +**************************** |
| 5 | + |
| 6 | +.. note:: |
| 7 | + |
| 8 | + This is a technical preview feature and it is subject to further changes. |
| 9 | + |
| 10 | +Overview |
| 11 | +============================ |
| 12 | + |
| 13 | +``pg_stat_monitor`` is a |postgres| Query Performance Monitoring |
| 14 | +tool. It collects statistics data and writes it in a storage unit called *bucket*. The data is added and stored in a bucket for the defined period – the bucket lifetime. |
| 15 | + |
| 16 | +You can specify the following: |
| 17 | + |
| 18 | +- The number of buckets. Together they form a bucket chain. |
| 19 | +- Bucket size. This is the amount of shared memory allocated for buckets. Memory is divided equally among buckets. |
| 20 | +- Bucket lifetime. |
| 21 | + |
| 22 | +When a bucket lifetime expires, ``pg_stat_monitor`` resets all statistics and writes the data in the next bucket in the chain. When the last bucket's lifetime expires, ``pg_stat_monitor`` returns to the first bucket. |
| 23 | + |
| 24 | +.. important:: |
| 25 | + |
| 26 | + The contents of the bucket will be overwritten. In order not to lose the data, make sure to read the bucket before ``pg_stat_monitor`` starts writing new data to it. |
| 27 | + |
| 28 | +.. _pg_stat-setup: |
| 29 | + |
| 30 | +Setup |
| 31 | +=========================== |
| 32 | + |
| 33 | +After the :ref:`installation <pdp.installing>`, ``pg_stat_monitor`` requires additional setup in order to use it with |postgres|. The setup steps are the following: |
| 34 | + |
| 35 | +1. Add ``pg_stat_monitor`` in the ``shared_preload_libraries`` configuration parameter. |
| 36 | + |
| 37 | + The recommended way to modify |postgres| configuration file is using the `ALTER SYSTEM <https://www.postgresql.org/docs/13/sql-altersystem.html>`_ command. :ref:`Connect to psql <server-connect>` and use the following command: |
| 38 | + |
| 39 | + .. code-block:: bash |
| 40 | +
|
| 41 | + $ ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_monitor'; |
| 42 | +
|
| 43 | + The parameter value is written to the :file:`postgresql.auto.conf` file which is read in addition with :file:`postgresql.conf` file. |
| 44 | + |
| 45 | +#. Start or restart the PostgreSQL instance to enable ``pg_stat_monitor``. Use the following command for restart: |
| 46 | + |
| 47 | + - On Debian and Ubuntu: |
| 48 | + |
| 49 | + .. code-block:: bash |
| 50 | +
|
| 51 | + $ sudo systemctl restart postgresql.service |
| 52 | +
|
| 53 | + - On |rhel| and CentOS: |
| 54 | + |
| 55 | + .. code-block:: bash |
| 56 | + |
| 57 | + $ sudo systemctl restart postgresql-13 |
| 58 | +
|
| 59 | +#. Create the extension. Connect to ``psql`` and use the following command: |
| 60 | + |
| 61 | + .. code-block:: bash |
| 62 | + |
| 63 | + $ CREATE EXTENSION pg_stat_monitor; |
| 64 | +
|
| 65 | +Usage |
| 66 | +=================== |
| 67 | + |
| 68 | +``pg_stat_monitor`` provides two views: |
| 69 | + |
| 70 | +- ``pg_stat_monitor`` is the view where statistics data is presented. |
| 71 | +- ``pg_stat_monitor_settings`` shows available configuration options which you can change. To learn more, see :ref:`change-config`. |
| 72 | + |
| 73 | +Use the following query to view what metrics ``pg_stat_monitor`` can collect: |
| 74 | + |
| 75 | +.. code-block:: bash |
| 76 | +
|
| 77 | + $ \d pg_stat_monitor; |
| 78 | +
|
| 79 | +**Output** |
| 80 | + |
| 81 | +.. code-block:: text |
| 82 | +
|
| 83 | + View "public.pg_stat_monitor" |
| 84 | + Column | Type | Collation | Nullable | Default |
| 85 | + ---------------------+--------------------------+-----------+----------+--------- |
| 86 | + bucket | integer | | | |
| 87 | + bucket_start_time | timestamp with time zone | | | |
| 88 | + userid | oid | | | |
| 89 | + dbid | oid | | | |
| 90 | + client_ip | inet | | | |
| 91 | + queryid | text | | | |
| 92 | + query | text | | | |
| 93 | + application_name | text | | | |
| 94 | + relations | text[] | | | |
| 95 | + cmd_type | text[] | | | |
| 96 | + elevel | integer | | | |
| 97 | + sqlcode | integer | | | |
| 98 | + message | text | | | |
| 99 | + plans | bigint | | | |
| 100 | + plan_total_time | double precision | | | |
| 101 | + plan_min_timei | double precision | | | |
| 102 | + plan_max_time | double precision | | | |
| 103 | + plan_mean_time | double precision | | | |
| 104 | + plan_stddev_time | double precision | | | |
| 105 | + calls | bigint | | | |
| 106 | + total_time | double precision | | | |
| 107 | + min_time | double precision | | | |
| 108 | + max_time | double precision | | | |
| 109 | + mean_time | double precision | | | |
| 110 | + stddev_time | double precision | | | |
| 111 | + rows | bigint | | | |
| 112 | + shared_blks_hit | bigint | | | |
| 113 | + shared_blks_read | bigint | | | |
| 114 | + shared_blks_dirtied | bigint | | | |
| 115 | + shared_blks_written | bigint | | | |
| 116 | + local_blks_hit | bigint | | | |
| 117 | + local_blks_read | bigint | | | |
| 118 | + local_blks_dirtied | bigint | | | |
| 119 | + local_blks_written | bigint | | | |
| 120 | + temp_blks_read | bigint | | | |
| 121 | + temp_blks_written | bigint | | | |
| 122 | + blk_read_time | double precision | | | |
| 123 | + blk_write_time | double precision | | | |
| 124 | + resp_calls | text[] | | | |
| 125 | + cpu_user_time | double precision | | | |
| 126 | + cpu_sys_time | double precision | | | |
| 127 | +
|
| 128 | +For example, to view the IP address of the client application that made the query, run the following command: |
| 129 | + |
| 130 | +.. code-block:: bash |
| 131 | +
|
| 132 | + $ SELECT userid::regrole, datname, substr(query,0, 50) AS query, calls, client_ip |
| 133 | + FROM pg_stat_monitor, pg_database |
| 134 | + WHERE dbid = oid; |
| 135 | +
|
| 136 | + userid | datname | query | calls | client_ip |
| 137 | + ----------+----------+---------------------------------------------------+-------+----------- |
| 138 | + postgres | postgres | select bucket, bucket_start_time, query,calls fro | 1 | 127.0.0.1 |
| 139 | + postgres | postgres | SELECT c.relchecks, c.relkind, c.relhasindex, c.r | 1 | 127.0.0.1 |
| 140 | + postgres | postgres | SELECT userid, total_time, min_time, max_time, | 1 | 127.0.0.1 |
| 141 | +
|
| 142 | +Find more usage examples in `pg_stat_monitor User Guide <https://github.com/percona/pg_stat_monitor/blob/REL0_7_0_STABLE/docs/USER_GUIDE.md>`_. |
| 143 | +
|
| 144 | +.. _change-config: |
| 145 | +
|
| 146 | +Changing the configuration |
| 147 | +================================== |
| 148 | +
|
| 149 | +Run the following query to list available configuration parameters. |
| 150 | +
|
| 151 | +.. code-block:: sh |
| 152 | +
|
| 153 | + $ SELECT name,description FROM pg_stat_monitor_settings; |
| 154 | +
|
| 155 | +**Output** |
| 156 | +
|
| 157 | +.. code-block:: text |
| 158 | +
|
| 159 | + name | description |
| 160 | + -----------------------------------------------+------------------------------------------------------------------- |
| 161 | + pg_stat_monitor.pgsm_max | Sets the maximum number of statements tracked by pg_stat_monitor. |
| 162 | + pg_stat_monitor.pgsm_query_max_len | Sets the maximum length of query. |
| 163 | + pg_stat_monitor.pgsm_enable | Enable/Disable statistics collector. |
| 164 | + pg_stat_monitor.pgsm_track_utility | Selects whether utility commands are tracked. |
| 165 | + pg_stat_monitor.pgsm_normalized_query | Selects whether save query in normalized format. |
| 166 | + pg_stat_monitor.pgsm_max_buckets | Sets the maximum number of buckets. |
| 167 | + pg_stat_monitor.pgsm_bucket_time | Sets the time in seconds per bucket. |
| 168 | + pg_stat_monitor.pgsm_object_cache | Sets the maximum number of object cache |
| 169 | + pg_stat_monitor.pgsm_respose_time_lower_bound | Sets the time in millisecond. |
| 170 | + pg_stat_monitor.pgsm_respose_time_step | Sets the response time steps in millisecond. |
| 171 | + pg_stat_monitor.pgsm_query_shared_buffer | Sets the query shared_buffer size. |
| 172 | +
|
| 173 | +You can change a parameter by setting a new value in the configuration file. Some parameters require server restart to apply a new value. For others, configuration reload is enough. Refer to the `configuration section <https://github.com/percona/pg_stat_monitor/blob/REL0_7_0_STABLE/docs/USER_GUIDE.md#configuration>`_ of the ``pg_stat_monitor`` documentation for the parameters’ description, how you can change their values and if the server restart is required to apply them. |
| 174 | +
|
| 175 | +As an example, let's set the bucket lifetime from default 60 seconds to 100 seconds. Use the :command:`ALTER SYSTEM` command: |
| 176 | +
|
| 177 | +.. code-block:: bash |
| 178 | +
|
| 179 | + $ ALTER SYSTEM set pg_stat_monitor.pgsm_bucket_time = 100; |
| 180 | +
|
| 181 | +Restart the server to apply the change: |
| 182 | +
|
| 183 | +- On Debia and Ubuntu |
| 184 | +
|
| 185 | + .. code-block:: bash |
| 186 | +
|
| 187 | + $ sudo systemctl restart restart postgresql.service |
| 188 | +
|
| 189 | +- On |rhel| and CentOS: |
| 190 | + |
| 191 | + .. code-block:: bash |
| 192 | + |
| 193 | + $ sudo systemctl restart postgresql-13 |
| 194 | +
|
| 195 | +Verify the updated parameter: |
| 196 | +
|
| 197 | +.. code-block:: bash |
| 198 | +
|
| 199 | + $ SELECT name, value |
| 200 | + FROM pg_stat_monitor_settings |
| 201 | + WHERE name = 'pg_stat_monitor.pgsm_bucket_time'; |
| 202 | +
|
| 203 | + name | value |
| 204 | + ----------------------------------+------- |
| 205 | + pg_stat_monitor.pgsm_bucket_time | 100 |
| 206 | +
|
| 207 | +.. seealso:: |
| 208 | +
|
| 209 | + ``pg_stat_monitor`` Documentation |
| 210 | + https://github.com/percona/pg_stat_monitor/blob/REL0_7_0_STABLE/README.md |
| 211 | +
|
| 212 | + Percona Blog: pg_stat_monitor: A New Way Of Looking At PostgreSQL Metrics |
| 213 | + https://www.percona.com/blog/2021/01/19/pg_stat_monitor-a-new-way-of-looking-at-postgresql-metrics/ |
| 214 | +
|
| 215 | +
|
| 216 | +.. include:: .res/replace.txt |
0 commit comments