{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": { "type": "grafana", "uid": "-- Grafana --" },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "description": "PostgreSQL dashboard for Oshun (pg_exporter schema): up/down, connections by state, commits vs rollbacks, cache hit ratio, query throughput, replication lag, locks, database size.",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "liveNow": false,
  "panels": [
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "pg_up reports 1 when the exporter can reach PostgreSQL.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "mappings": [
            {
              "options": {
                "0": { "color": "red", "index": 0, "text": "DOWN" },
                "1": { "color": "green", "index": 1, "text": "UP" }
              },
              "type": "value"
            }
          ],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "red", "value": null },
              { "color": "green", "value": 1 }
            ]
          },
          "unit": "none"
        }
      },
      "gridPos": { "h": 4, "w": 6, "x": 0, "y": 0 },
      "id": 1,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false },
        "textMode": "value_and_name"
      },
      "pluginVersion": "10.2.0",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "min(pg_up)",
          "legendFormat": "pg_up",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "PostgreSQL Up",
      "type": "stat"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Total active backends across all databases.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "orange", "value": 150 },
              { "color": "red", "value": 220 }
            ]
          },
          "unit": "short"
        }
      },
      "gridPos": { "h": 4, "w": 6, "x": 6, "y": 0 },
      "id": 2,
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false },
        "textMode": "value_and_name"
      },
      "pluginVersion": "10.2.0",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(pg_stat_database_numbackends{datname=~\"$datname\"})",
          "legendFormat": "backends",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Connections",
      "type": "stat"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Commit TPS across the selected databases (last 5m rate).",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "blue", "value": null }] },
          "unit": "ops"
        }
      },
      "gridPos": { "h": 4, "w": 6, "x": 12, "y": 0 },
      "id": 3,
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false },
        "textMode": "value_and_name"
      },
      "pluginVersion": "10.2.0",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_xact_commit{datname=~\"$datname\"}[5m]))",
          "legendFormat": "tps",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Commits / sec",
      "type": "stat"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Buffer cache hit ratio (blks_hit / (blks_hit + blks_read)). A healthy OLTP workload stays above 0.95.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "mappings": [],
          "max": 1,
          "min": 0,
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "red", "value": null },
              { "color": "orange", "value": 0.9 },
              { "color": "green", "value": 0.98 }
            ]
          },
          "unit": "percentunit"
        }
      },
      "gridPos": { "h": 4, "w": 6, "x": 18, "y": 0 },
      "id": 4,
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "justifyMode": "auto",
        "orientation": "auto",
        "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false },
        "textMode": "value_and_name"
      },
      "pluginVersion": "10.2.0",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_blks_hit{datname=~\"$datname\"}[5m])) / clamp_min(sum(rate(pg_stat_database_blks_hit{datname=~\"$datname\"}[5m])) + sum(rate(pg_stat_database_blks_read{datname=~\"$datname\"}[5m])), 1)",
          "legendFormat": "cache hit",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Cache Hit Ratio",
      "type": "stat"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Backends per database. Long-running high counts indicate a connection leak.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 20,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true,
            "stacking": { "group": "A", "mode": "normal" }
          },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }] },
          "unit": "short"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 4 },
      "id": 5,
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum by (datname) (pg_stat_database_numbackends{datname=~\"$datname\"})",
          "legendFormat": "{{datname}}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Connections by Database",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Transaction commit vs rollback rate. A sustained rollback spike often means application errors.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 15,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true
          },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }] },
          "unit": "ops"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 4 },
      "id": 6,
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum by (datname) (rate(pg_stat_database_xact_commit{datname=~\"$datname\"}[5m]))",
          "legendFormat": "{{datname}} commit",
          "range": true,
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum by (datname) (rate(pg_stat_database_xact_rollback{datname=~\"$datname\"}[5m]))",
          "legendFormat": "{{datname}} rollback",
          "range": true,
          "refId": "B"
        }
      ],
      "title": "Commits vs Rollbacks",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Rows returned, fetched, inserted, updated, deleted.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 10,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true
          },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }] },
          "unit": "ops"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 12 },
      "id": 7,
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_tup_returned{datname=~\"$datname\"}[5m]))",
          "legendFormat": "returned",
          "range": true,
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_tup_fetched{datname=~\"$datname\"}[5m]))",
          "legendFormat": "fetched",
          "range": true,
          "refId": "B"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_tup_inserted{datname=~\"$datname\"}[5m]))",
          "legendFormat": "inserted",
          "range": true,
          "refId": "C"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_tup_updated{datname=~\"$datname\"}[5m]))",
          "legendFormat": "updated",
          "range": true,
          "refId": "D"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum(rate(pg_stat_database_tup_deleted{datname=~\"$datname\"}[5m]))",
          "legendFormat": "deleted",
          "range": true,
          "refId": "E"
        }
      ],
      "title": "Row Operation Rate",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Streaming replication lag in bytes (primary's current LSN minus replica's replay LSN).",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 15,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "orange", "value": 16777216 },
              { "color": "red", "value": 268435456 }
            ]
          },
          "unit": "bytes"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 12 },
      "id": 8,
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "max by (application_name) (pg_stat_replication_lag_bytes)",
          "legendFormat": "{{application_name}}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Replication Lag (bytes)",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Locks held by mode. Access-exclusive locks should be rare and short-lived.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 15,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true,
            "stacking": { "group": "A", "mode": "normal" }
          },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }] },
          "unit": "short"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 20 },
      "id": 9,
      "options": {
        "legend": {
          "calcs": ["mean", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "sum by (mode) (pg_locks_count)",
          "legendFormat": "{{mode}}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Locks by Mode",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${datasource}" },
      "description": "Database on-disk size. Use to plan storage.",
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": {
            "drawStyle": "line",
            "fillOpacity": 15,
            "gradientMode": "opacity",
            "lineInterpolation": "smooth",
            "lineWidth": 2,
            "pointSize": 5,
            "showPoints": "never",
            "spanNulls": true
          },
          "mappings": [],
          "thresholds": { "mode": "absolute", "steps": [{ "color": "green", "value": null }] },
          "unit": "bytes"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 20 },
      "id": 10,
      "options": {
        "legend": {
          "calcs": ["lastNotNull", "max"],
          "displayMode": "table",
          "placement": "right",
          "showLegend": true
        },
        "tooltip": { "mode": "multi", "sort": "desc" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${datasource}" },
          "editorMode": "code",
          "expr": "pg_database_size_bytes{datname=~\"$datname\"}",
          "legendFormat": "{{datname}}",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Database Size",
      "type": "timeseries"
    }
  ],
  "refresh": "30s",
  "schemaVersion": 39,
  "tags": ["oshun", "infrastructure", "postgres", "database"],
  "templating": {
    "list": [
      {
        "current": { "selected": false, "text": "Prometheus", "value": "prometheus" },
        "hide": 0,
        "includeAll": false,
        "label": "Datasource",
        "multi": false,
        "name": "datasource",
        "options": [],
        "query": "prometheus",
        "refresh": 1,
        "regex": "",
        "skipUrlSync": false,
        "type": "datasource"
      },
      {
        "allValue": ".+",
        "current": { "selected": true, "text": ["All"], "value": ["$__all"] },
        "datasource": { "type": "prometheus", "uid": "${datasource}" },
        "definition": "label_values(pg_stat_database_numbackends, datname)",
        "hide": 0,
        "includeAll": true,
        "label": "Database",
        "multi": true,
        "name": "datname",
        "options": [],
        "query": {
          "query": "label_values(pg_stat_database_numbackends, datname)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 2,
        "regex": "^(?!template|postgres$).*$",
        "skipUrlSync": false,
        "sort": 1,
        "type": "query"
      }
    ]
  },
  "time": { "from": "now-3h", "to": "now" },
  "timepicker": {},
  "timezone": "",
  "title": "Infrastructure — PostgreSQL",
  "uid": "infra-postgres",
  "version": 1,
  "weekStart": ""
}
