Why are PostgreSQL updates getting slower?

UPDATE latency refers to the time taken by PostgreSQL to perform an UPDATE operation on a table. Slow UPDATE operations can negatively impact the overall performance of the database. Here are some steps to troubleshoot UPDATE latency in PostgreSQL:

  1. Analyze the query plan: The first step in troubleshooting UPDATE latency is to analyze the query plan generated by PostgreSQL. You can use the EXPLAIN command to get the query plan for the UPDATE statement. The query plan will show the steps PostgreSQL takes to execute the UPDATE statement, and you can identify any bottlenecks or performance issues.
  2. Check for long transactions: Long-running transactions can cause UPDATE operations to slow down. You can use the pg_stat_activity view to identify any long-running transactions that might be causing performance issues. Once you identify the transaction, you can use the pg_cancel_backend() or pg_terminate_backend() function to cancel or terminate the transaction.
  3. Check for table bloat: Table bloat can also cause UPDATE operations to slow down. You can use the pgstattuple extension to analyze the table and identify any bloat. Once you identify the problem, you can run the VACUUM command to reclaim the wasted space and improve the performance of the UPDATE statement.
  4. Check for index bloat: Index bloat can also slow down UPDATE operations. You can use the pgstattuple extension to analyze the index and identify any bloat. Once you identify the problem, you can run the REINDEX command to rebuild the index and improve the performance of the UPDATE statement.
  5. Check for locking issues: Locking issues can cause UPDATE operations to wait for other transactions to complete, resulting in slow performance. You can use the pg_locks view to identify any locking issues. Once you identify the problem, you can tune the transaction isolation level or modify the query to reduce the locking contention.
  6. Check for hardware bottlenecks: Slow disk I/O, CPU bottlenecks, or low memory can also cause UPDATE operations to slow down. You can use system monitoring tools like top, iostat, or sar to identify any hardware bottlenecks. Once you identify the problem, you can tune the system or upgrade the hardware to improve the performance of the UPDATE statement.

Monitoring UPDATE latency happening to PostgreSQL 

SELECT query, state, now() – query_start AS elapsed_time
FROM pg_stat_activity
WHERE state = ‘active’ AND query LIKE ‘UPDATE%’
ORDER BY elapsed_time DESC;

This query will show all active UPDATE statements in PostgreSQL, sorted by their elapsed time in descending order. The pg_stat_activity system view provides real-time statistics about the current activity in PostgreSQL. The query column shows the text of the active query, and the state column shows the current state of the query (e.g. “active”, “idle”, “idle in transaction”). The now() – query_start expression calculates the elapsed time of the query in seconds.

Note that this query only shows active UPDATE statements, meaning the ones that are currently running. To monitor the latency of completed UPDATE statements, you can use the pg_stat_user_tables system view, which provides statistics about each user-defined table in the database:

SELECT relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, n_tup_del
FROM pg_stat_user_tables
WHERE schemaname = ‘public’ AND relname = ‘mytable’;

This query shows various statistics about the table mytable, including the number of rows that were updated (n_tup_upd) and the total time spent updating the table. However, note that these statistics are cumulative and are not reset after a server restart or a database vacuum. Therefore, it is important to interpret these statistics in the context of the current workload and usage patterns.

About Shiv Iyer 455 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.