PostgreSQL Scripts for Database Administration
ALTER SEQUENCE RESTART WITH in PostgreSQL — Examples
Aug 17, 2025 / · 8 min read · postgresql sql database database-maintenance postgres-sequences postgresql-scripting dba-tips database-tuning pg_class pg_namespace ·Restarting All PostgreSQL Sequences with SQL Script Database administrators often face challenges with PostgreSQL sequences, especially after data has been deleted or when tables have been re-imported. Sequences drive the generation of auto-incrementing values (like primary keys using SERIAL or BIGSERIAL), and …
Read MoreHow to Monitor Slow Running Queries in PostgreSQL
Monitor Slow Running Queries in PostgreSQL with pg_stat_activity An application hangs, the connection pool fills up, and somewhere a statement has been running for minutes. pg_stat_activity shows it for every backend on the server: how long the current query has been going, which role and database it belongs to, and …
Read MoreAnalyze PostgreSQL Cache-Hit Ratio with pg_stat_statements
May 18, 2025 / · 3 min read · PostgreSQL SQL database performance monitor running queries pg_stat_statements ·How to Analyze PostgreSQL Query Cache-Hit Ratio Using pg_stat_statements Purpose Measuring the cache-hit ratio of your PostgreSQL queries is essential for understanding and optimizing how efficiently your database serves data from memory versus disk. This article explains how to use the pg_stat_statements extension to …
Read MorePostgreSQL Lock Monitoring Uncover Query Details and Lock Age
Sep 2, 2024 / · 3 min read · PostgreSQL SQL Database Performance Monitoring pg_stat_activity pg_locks ·PostgreSQL Lock Monitoring: Gain Insights into Locks with Query and Age Purpose This PostgreSQL SQL query equips you to monitor locks held by active database sessions along with their associated queries and age. This detailed information proves invaluable for performance tuning, troubleshooting lock conflicts, and …
Read MoreIdentify & Resolve Blocking Application Locks in PostgreSQL
Sep 1, 2024 / · 4 min read · PostgreSQL SQL Database Performance Troubleshooting pg_locks pg_stat_activity ·PostgreSQL Lock Monitoring: Identify Blocking Locks and Resolve Application Conflicts Purpose In a busy PostgreSQL environment, transactions can sometimes become blocked, waiting for other transactions to release locks. This can slow performance and even cause stalls. This article dissects a powerful PostgreSQL script …
Read MorePostgreSQL Performance Monitoring: Identify Slow Queries
PostgreSQL: Identify Slow Running Queries in Real-Time Purpose This PostgreSQL SQL query provides a real-time snapshot of sessions whose current query (or, for idle sessions, most recent query) started more than 30 seconds ago. It acts as a powerful alternative to a traditional slow query log, helping you proactively …
Read MoreBoost PostgreSQL Efficiency with Cache Hit Ratio Analysis
PostgreSQL Performance Tuning: Analyze Cache Hit Ratio with pg_stat_statements Purpose This PostgreSQL query empowers you to gain critical insights into the efficiency of your query workload by measuring the cache hit ratio from the pg_stat_statements extension. Cache hit ratio is a crucial metric for evaluating how …
Read MoreAnalyze PostgreSQL Query Times with pg_stat_statements
Aug 29, 2024 / · 8 min read · postgresql performance performance tuning pg_stat_statements pg_database pg_roles ·Analyze PostgreSQL Query Times with pg_stat_statements PostgreSQL 13 renamed the timing columns of pg_stat_statements: total_time became total_exec_time, mean_time became mean_exec_time, and the same suffix went onto the minimum, maximum and standard deviation columns. A query copied from an older tutorial fails on a …
Read Morepg_is_in_recovery — Monitor PostgreSQL Standby Status
Aug 28, 2024 / · 9 min read · PostgreSQL SQL Database Recovery Monitoring pg_is_in_backup pg_is_in_recovery pg_backup_start_time ·Monitor PostgreSQL Recovery Status with pg_is_in_recovery Is this server a standby, and is it still applying WAL? One SELECT answers both questions. It calls pg_is_in_recovery() and the functions that report how far a standby has received and replayed the write-ahead log (WAL), and it adds the two legacy …
Read MorePrioritize PostgreSQL Table Analysis for Performance
Apr 25, 2024 / · 2 min read · PostgreSQL database performance optimization query pg_stat_user_tables ·Prioritize PostgreSQL Table Analysis: Unlocking Performance Insights Maintaining optimal PostgreSQL database performance requires regular table analysis. This process lets the query planner make informed decisions based on accurate table statistics. This article dives into a valuable PostgreSQL script revealing tables …
Read More