{
  "dashboard": {
    "title": "Database Performance Metrics",
    "uid": "database-performance",
    "timezone": "browser",
    "panels": [
      {
        "id": 1,
        "title": "Connection Pool Usage",
        "type": "stat",
        "gridPos": {"h": 8, "w": 6, "x": 0, "y": 0},
        "targets": [
          {
            "expr": "(pg_stat_activity_count / pg_settings_max_connections) * 100",
            "refId": "A"
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "percent",
            "thresholds": {
              "mode": "absolute",
              "steps": [
                {"color": "green", "value": null},
                {"color": "yellow", "value": 70},
                {"color": "red", "value": 85}
              ]
            }
          }
        }
      },
      {
        "id": 2,
        "title": "Active Connections",
        "type": "stat",
        "gridPos": {"h": 8, "w": 6, "x": 6, "y": 0},
        "targets": [
          {
            "expr": "pg_stat_activity_count",
            "refId": "A"
          }
        ]
      },
      {
        "id": 3,
        "title": "Query Latency (ms)",
        "type": "stat",
        "gridPos": {"h": 8, "w": 6, "x": 12, "y": 0},
        "targets": [
          {
            "expr": "histogram_quantile(0.95, rate(db_query_duration_seconds_bucket[5m])) * 1000",
            "refId": "A"
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "ms",
            "thresholds": {
              "mode": "absolute",
              "steps": [
                {"color": "green", "value": null},
                {"color": "yellow", "value": 3000},
                {"color": "red", "value": 5000}
              ]
            }
          }
        }
      },
      {
        "id": 4,
        "title": "Transaction Duration (ms)",
        "type": "stat",
        "gridPos": {"h": 8, "w": 6, "x": 18, "y": 0},
        "targets": [
          {
            "expr": "histogram_quantile(0.99, rate(db_transaction_duration_seconds_bucket[5m])) * 1000",
            "refId": "A"
          }
        ]
      },
      {
        "id": 5,
        "title": "Query Execution Rate",
        "type": "graph",
        "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8},
        "targets": [
          {
            "expr": "sum(rate(db_queries_total[1m]))",
            "refId": "A",
            "legendFormat": "Queries/sec"
          }
        ]
      },
      {
        "id": 6,
        "title": "Query Error Rate",
        "type": "graph",
        "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8},
        "targets": [
          {
            "expr": "sum(rate(db_queries_failed_total[5m])) / sum(rate(db_queries_total[5m]))",
            "refId": "A",
            "legendFormat": "Error Rate"
          }
        ]
      },
      {
        "id": 7,
        "title": "Database Disk Usage",
        "type": "gauge",
        "gridPos": {"h": 8, "w": 6, "x": 0, "y": 16},
        "targets": [
          {
            "expr": "pg_database_size_bytes / (pg_settings_work_mem * 1024) * 100",
            "refId": "A"
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "percent",
            "thresholds": {
              "mode": "absolute",
              "steps": [
                {"color": "green", "value": null},
                {"color": "yellow", "value": 70},
                {"color": "red", "value": 90}
              ]
            }
          }
        }
      },
      {
        "id": 8,
        "title": "Cache Hit Ratio",
        "type": "gauge",
        "gridPos": {"h": 8, "w": 6, "x": 6, "y": 16},
        "targets": [
          {
            "expr": "pg_stat_database_blks_hit / (pg_stat_database_blks_hit + pg_stat_database_blks_read) * 100",
            "refId": "A"
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "percent",
            "min": 0,
            "max": 100,
            "thresholds": {
              "mode": "absolute",
              "steps": [
                {"color": "red", "value": null},
                {"color": "yellow", "value": 80},
                {"color": "green", "value": 95}
              ]
            }
          }
        }
      },
      {
        "id": 9,
        "title": "Replication Lag (seconds)",
        "type": "stat",
        "gridPos": {"h": 8, "w": 6, "x": 12, "y": 16},
        "targets": [
          {
            "expr": "pg_replication_lag_seconds",
            "refId": "A"
          }
        ],
        "fieldConfig": {
          "defaults": {
            "unit": "s",
            "thresholds": {
              "mode": "absolute",
              "steps": [
                {"color": "green", "value": null},
                {"color": "yellow", "value": 5},
                {"color": "red", "value": 30}
              ]
            }
          }
        }
      },
      {
        "id": 10,
        "title": "Slow Query Log (>1s)",
        "type": "table",
        "gridPos": {"h": 8, "w": 6, "x": 18, "y": 16},
        "targets": [
          {
            "expr": "pg_slow_queries",
            "format": "table",
            "instant": true,
            "refId": "A"
          }
        ]
      }
    ],
    "refresh": "30s",
    "schemaVersion": 27,
    "style": "dark",
    "time": {
      "from": "now-1h",
      "to": "now"
    }
  }
}
