<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: UDDESH RAVINDRA TAKPERE</title>
    <description>The latest articles on DEV Community by UDDESH RAVINDRA TAKPERE (@uddesh_ravindratakpere_f).</description>
    <link>https://dev.to/uddesh_ravindratakpere_f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1538712%2F2dfe7441-7997-4a08-8da2-331331f03925.png</url>
      <title>DEV Community: UDDESH RAVINDRA TAKPERE</title>
      <link>https://dev.to/uddesh_ravindratakpere_f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/uddesh_ravindratakpere_f"/>
    <language>en</language>
    <item>
      <title>PostgreSQL Monitoring: Real-time Query Insights with Prometheus and Grafana</title>
      <dc:creator>UDDESH RAVINDRA TAKPERE</dc:creator>
      <pubDate>Sat, 28 Jun 2025 09:00:57 +0000</pubDate>
      <link>https://dev.to/uddesh_ravindratakpere_f/postgresql-monitoring-real-time-query-insights-with-prometheus-and-grafana-25p9</link>
      <guid>https://dev.to/uddesh_ravindratakpere_f/postgresql-monitoring-real-time-query-insights-with-prometheus-and-grafana-25p9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why Monitor PostgreSQL?&lt;/strong&gt;&lt;br&gt;
PostgreSQL is widely used in production-grade systems, but performance issues often hide in unoptimized queries or excessive connections. With a sharp monitoring setup, we can observe live query activity, track expensive queries, and alert on anomalies, ensuring our database layer is not the bottleneck.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our Tech Stack:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;Prometheus&lt;/li&gt;
&lt;li&gt;Postgres Exporter (Custom queries via queries.yaml)&lt;/li&gt;
&lt;li&gt;Grafana&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What We Did&lt;/strong&gt;&lt;br&gt;
We took the default Postgres Exporter a step further by adding custom SQL queries to extract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Top N Expensive Queries&lt;/li&gt;
&lt;li&gt;Query Execution Counts (per 15m window)&lt;/li&gt;
&lt;li&gt;Average Execution Time&lt;/li&gt;
&lt;li&gt;Active Query Tracking with Username &amp;amp; Database Filters&lt;/li&gt;
&lt;li&gt;Connection State Breakdown&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Grafana Panels We Built :&lt;/strong&gt;&lt;br&gt;
Panel: Top Queries by Total Execution Time&lt;br&gt;
Insight: Highlights queries that consume the most time overall.&lt;/p&gt;

&lt;p&gt;Panel: Top Queries by Call Rate&lt;br&gt;
Insight: See which queries are being called most often in the last 15 minutes.&lt;/p&gt;

&lt;p&gt;Panel: Real-Time Active Queries&lt;br&gt;
Insight: Tracks ongoing query executions with user and database context.&lt;/p&gt;

&lt;p&gt;Panel: Connection State Breakdown&lt;br&gt;
Insight: Monitors how many connections are actively doing work vs. just open.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Custom Exporter Config : *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_query_duration:
  query: |
    SELECT DISTINCT ON (query)
      datname,
      usename,
      query,
      calls,
      total_exec_time / 1000 AS total_time_sec,
      (total_exec_time / calls) / 1000 AS avg_time_sec
    FROM pg_stat_statements
    WHERE query NOT ILIKE '%pg_stat_statements%'
    ORDER BY query, calls DESC
    LIMIT 50;
  metrics:
    - datname: { usage: "LABEL" }
    - usename: { usage: "LABEL" }
    - query: { usage: "LABEL" }
    - calls: { usage: "COUNTER" }
    - total_time_sec: { usage: "COUNTER" }
    - avg_time_sec: { usage: "GAUGE" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time visibility into slow queries&lt;/li&gt;
&lt;li&gt;Developer ownership over query performance&lt;/li&gt;
&lt;li&gt;Faster debugging and production insights&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Monitoring is more than uptime checks. With the right visibility into your PostgreSQL workload, you enable your engineering teams to write better queries and optimize performance, without flying blind.&lt;br&gt;
This setup isn’t just for DBAs. It’s for every DevOps engineer, SRE, and developer who wants performance clarity.&lt;/p&gt;

&lt;p&gt;We're always looking to improve our monitoring setup. If you have suggestions, tweaks, or battle-tested tricks, feel free to share them—feedback from fellow DevOps engineers is always welcome!&lt;/p&gt;

</description>
      <category>database</category>
      <category>monitoring</category>
      <category>grafana</category>
      <category>prometheus</category>
    </item>
  </channel>
</rss>
