How to Automatically Kill Long-Running Queries in PostgreSQL

Long-running queries can cause serious performance issues in PostgreSQL. Here’s how to automate their termination to keep your DB healthy. Why Kill Long-Running Queries? In real-world scenarios, long-running queries often: Lock tables or rows Exhaust memory or CPU Cause deadlocks Block migrations or monitoring tools Typical causes: Forgotten WHERE clauses Heavy reporting queries Hung jobs or broken clients How It Works We’ll use a Bash script to: Connect to PostgreSQL Detect queries older than 60 seconds Automatically terminate them Bash Script to Kill Long Queries 1. Define connection and timeout #!/bin/bash PG_CONN="postgres://user:pass@postgres-host/postgres-db" QUERY_TIMEOUT="60" PSQL_RUN="psql $PG_CONN -Atc" 2. Build the query QUERY="SELECT pid FROM pg_stat_activity WHERE now() - query_start > '${QUERY_TIMEOUT} seconds'::interval" 3. Run and kill echo "Checking for queries longer than $QUERY_TIMEOUT seconds..." pids=$(${PSQL_RUN} "$QUERY") for pid in $pids; do echo "Terminating PID $pid..." ${PSQL_RUN} "SELECT pg_terminate_backend($pid)" done Automate with Cron To run every 5 minutes: ...

September 5, 2025 · 1 min · 207 words · DevOps Insights

Kubernetes HPA Explained: Pros, Cons, and Use Cases

The Horizontal Pod Autoscaler (HPA) in Kubernetes automatically scales the number of pods in a deployment or replica set based on observed resource usage, such as CPU or memory. This article breaks down how HPA works, when to use it, its pros and cons, and how to get started with it in your Kubernetes cluster. 1. What is HPA? HPA dynamically adjusts the number of pods in a Kubernetes workload (like a Deployment or StatefulSet) based on metrics from the Kubernetes Metrics Server. ...

September 5, 2025 · 2 min · 311 words · DevOps Insights

Kubernetes Resource Management: LimitRange vs ResourceQuota

Kubernetes Resource Management: LimitRange vs ResourceQuota Managing resources in Kubernetes is critical for ensuring fair usage, stability, and predictable performance in a multi-tenant cluster. Two powerful tools provided by Kubernetes for this purpose are LimitRange and ResourceQuota. This article explains what they are, their differences, and how to use them effectively. What is LimitRange? LimitRange is a Kubernetes policy object that sets default resource limits (CPU/memory) for containers in a namespace. ...

September 5, 2025 · 2 min · 250 words · John Cena

How to Set Up a Private Docker Registry

Need a secure, fast, and self-hosted solution for storing container images? Setting up a private Docker registry is easier than you think. Why Run Your Own Docker Registry? Running your own registry allows you to: Avoid Docker Hub rate limits Store proprietary images securely Speed up CI/CD workflows Control image retention and access Great for air-gapped environments and enterprise deployments. 1. Prerequisites You’ll need: Docker installed Access to a server or VM Optional: domain name and TLS certs (for production) 2. Start a Local Registry (Quick Start) docker run -d -p 5000:5000 --name registry registry:2 This launches a registry on localhost:5000. ...

September 4, 2025 · 2 min · 424 words · DevOps Insights

Best Practices for Deploying Applications to Kubernetes

Deploying applications to Kubernetes? Follow these best practices to avoid downtime, improve scalability, and simplify operations. 1. Use Readiness and Liveness Probes Probes help Kubernetes know when your app is healthy and ready to serve traffic. livenessProbe: httpGet: path: /healthz port: 8080 initialDelaySeconds: 5 periodSeconds: 10 readinessProbe: httpGet: path: /ready port: 8080 initialDelaySeconds: 5 periodSeconds: 10 2. Configure Resource Requests and Limits Avoid over/under-scheduling: ...

September 3, 2025 · 1 min · 195 words · DevOps Insights