Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

pg_stat_statements

Updated on 2024-10-14 GMT+08:00

Introduction

The pg_stat_statements extension provides a means for tracking planning and execution statistics of all SQL statements executed by a server.

For more information, see pg_stat_statements in the PostgreSQL documentation.

Supported Versions

This extension is available to the latest minor versions of RDS for PostgreSQL 10 and later versions.

You can run the following SQL statement to check whether your DB instance supports this extension:

SELECT * FROM pg_available_extension_versions WHERE name  = 'pg_stat_statements';

If this extension is not supported, upgrade the minor version of your DB instance or upgrade the major version using dump and restore.

To see more extensions supported by RDS for PostgreSQL, go to Supported Extensions.

Extension Installation and Uninstallation

To check whether pg_stat_statements is installed in a database, run the following SQL statement:

select * from pg_extension  where extname = 'pg_stat_statements';

If the command output is empty, the extension is not installed. If the extension information is displayed, the extension is installed.

By default, pg_stat_statements is preloaded in the shared_preload_libraries parameter. Perform the following steps to install or delete the extension:

  • Installing the extension
    SELECT control_extension('create', 'pg_stat_statements');
  • Deleting the extension
    SELECT control_extension('drop', 'pg_stat_statements');

For more information, see Installing and Uninstalling an Extension on the RDS Console and Installing and Uninstalling an Extension Using SQL Commands.

Basic Functions

  1. After the pg_stat_statements extension is installed, configure the parameters below. You can adjust the values based on your workload requirements.
    Table 1 Parameter description

    Parameter

    Reboot Required

    Default Value

    Allowed Values

    Description

    pg_stat_statements.max

    Yes

    5000

    100-5,000,000

    Specifies the maximum number of statements tracked by pg_stat_statements.

    pg_stat_statements.save

    No

    on

    on, off

    Specifies whether to save statement statistics across server shutdowns.

    pg_stat_statements.track

    No

    top

    top, all, none

    Controls which statements are counted by pg_stat_statements.

    pg_stat_statements.track_planning

    No

    off

    on, off

    Controls whether planning duration is tracked by pg_stat_statements.

    pg_stat_statements.track_utility

    No

    on

    on, off

    Controls whether utility commands are tracked by pg_stat_statements.

  2. Query the pg_stat_statements view to obtain statistics.
    select * from pg_stat_statements;
  3. Query the SQL statements with high I/O consumption.
    -- Top 5 SQL statements
    select userid::regrole, dbid, query
    from pg_stat_statements
    order by (blk_read_time+blk_write_time) desc limit 5;
  4. Query the SQL statements with high consumption of shared memory.
    select userid::regrole, dbid, query
    from pg_stat_statements
    order by (shared_blks_hit+shared_blks_dirtied) desc limit 5;
  5. Reset the statistics.
    select pg_stat_statements_reset();

Advanced Functions

You can use pg_stat_statements to troubleshoot high CPU usage. The process is as follows:

  1. Reset the pg_stat_statements counter.
    select pg_stat_statements_reset();

    Leave enough time for pg_stat_statements to collect information.

  2. Obtain the most time-consuming SQL statements.
    select * from pg_stat_statements order by total_exec_time desc limit 10;

    The obtained SQL statements have been occupying the user-mode CPU for a long time. Analyze these SQL statements.

  3. Obtain the SQL statements that read the buffer for the most times.
    select * from pg_stat_statements order by shared_blks_hit + shared_blks_read desc limit 10;

    The obtained SQL statements may cause too many buffer reads due to a lack of indexes, consuming a large number of CPU resources.

  4. Obtain the SQL statements that have been executed for the most times.
    select * from pg_stat_statements order by calls desc limit 10;

    It takes a short time to execute some simple SQL statements separately. However, in some cases (for example, cyclic executions in a transaction or concurrent executions), the CPU usage increases.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback