pg_profile is a tool that can collect resource usage statistics in the database to help users analyze and optimize the database.
Background information
pg_profile is based on PostgreSQL’s built-in statistics views (pg_stat_statements) and community extension (pg_stat_kcache), fully written in PL/pgSQL, with no external libraries or software required. pg_profile depends on the dblink, pg_stat_statements, and pg_stat_kcache extensions. Among them, pg_stat_kcache is optional. Enabling pg_stat_kcache provides more information collection features. Prerequisites
- Add pg_stat_statements to the shared_preload_libraries parameter.
- (Optional) Add pg_stat_kcache to the shared_preload_libraries parameter.
Precautions
- Storing sampling information will consume disk space. pg_profile has an automatic cleanup mechanism; pay attention to data preservation.
- Sampling is time-consuming and does not require frequent execution; the recommended sampling frequency is 1 to 2 times per hour.
Create and delete extension
tip
A super account is required to create the extension.
Extension usage
The pg_profile extension requires coordination between two databases during use: one database is sampled, and the other stores the collected statistical information. These two databases can be created in different PostgreSQL instances or within the same PostgreSQL instance. Below, the server database refers to the sampled database, and the profile database refers to the database storing statistical information, explaining the usage process of the pg_profile extension.
During sampling, the profile database sends a request to the server database, which samples its information upon receiving the request and returns the sampling results to profile. Profile stores the received results in tables within the profile database.
1. Prepare the Server database.
- Create a Server database.
- Connect to the Server database and create an extension.
2. Prepare the profile database.
- Create a profile database.
- Connect to the profile database and create an extension.
3. Create a Server connection.
Return result example:
tip
After creating the server connection, a profile database connection is automatically created, which is the connection named local in the above return result example.
4. Modify the profile database connection information and configure the Password.
5. Perform initial data collection.
6. Collect data again after conducting stress testing on the server database.
tip
- The following commands must be executed in the client command line window. Ensure the client has PostgreSQL client installed.
Connect to the instance where the profile database is located and collect data again.
7. Generate a report from the collected results.
At two methods can be used to export the collected results to the local machine.
- Method 1: Execute the following command in the profile database using psql:
- Method 2: Execute the following command in the local client command line:
Others