This collector is enabled by default.
What It Monitors
The locks collector tracks the number of locks held in each database, categorized by lock mode. It helps identify:- Lock contention issues
- Deadlock situations
- Databases with high locking activity
Metrics Exposed
pg_locks_count
Number of locks held in each database, grouped by lock mode. Type: GaugeLabels:
datname- Database namemode- Lock mode (see Lock Modes section)
Lock Modes
PostgreSQL supports nine lock modes, ordered from least to most restrictive:- accesssharelock - Acquired by
SELECTqueries - rowsharelock - Acquired by
SELECT FOR UPDATE - rowexclusivelock - Acquired by
INSERT,UPDATE,DELETE - shareupdateexclusivelock - Acquired by
VACUUM,CREATE INDEX CONCURRENTLY - sharelock - Acquired by
CREATE INDEX - sharerowexclusivelock - Rare lock mode
- exclusivelock - Blocks all concurrent access
- accessexclusivelock - Acquired by
DROP TABLE,TRUNCATE,ALTER TABLE - sireadlock - Used by serializable transactions
SQL Query Used
The collector executes a comprehensive query that reports all lock modes for all databases:Configuration
Enable/Disable the Collector
Use Cases
Detect Lock Contention
Alert when exclusive locks are held for extended periods:Monitor Normal Lock Activity
Track typical lock usage patterns:Identify Problematic Databases
Find databases with the highest lock counts:Alert on Lock Spikes
Detect sudden increases in locking:Understanding Lock Patterns
Normal Activity
- Many
accesssharelock(SELECT queries) - Moderate
rowexclusivelock(INSERT/UPDATE/DELETE) - Few exclusive locks
Potential Issues
- High
accessexclusivelock- Table maintenance blocking queries - Many
sharerowexclusivelock- Potential deadlock situations - Sustained high lock counts - Query optimization needed
Permissions Required
The locks collector requires:CONNECTprivilege on the database- Read access to the
pg_lockssystem view - For PostgreSQL 10+:
pg_monitororpg_read_all_statsrole - For PostgreSQL <10: Superuser or specific function access
Performance Considerations
The locks collector:- Queries the
pg_lockssystem view - Uses a CROSS JOIN with all lock modes to ensure complete coverage
- Has minimal performance impact
- Query execution time grows with the number of databases and active locks
- Safe to run frequently (every scrape interval)
Related Collectors
- Database Statistics - Tracks deadlock counts
- Long Running Transactions Collector - Identifies transactions holding locks (if enabled)
Troubleshooting
If you see unexpectedly high lock counts:- Check for long-running transactions:
SELECT * FROM pg_stat_activity - Identify blocking queries: Use
pg_blocking_pids()function - Review application query patterns
- Consider using connection pooling to reduce lock duration