groups:
  - name: pg_local_cache.availability
    rules:
      - alert: PgLocalCacheExporterDown
        expr: up{job="pg-local-cache"} == 0
        for: 2m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache exporter is unavailable
          description: Prometheus has been unable to scrape pg_local_cache for two minutes.

      - alert: PgLocalCacheMetricsMissing
        expr: up{job="pg-local-cache"} == 1 unless on (instance) pg_local_cache_up{job="pg-local-cache"}
        for: 2m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache custom metrics are missing
          description: postgres_exporter is reachable but local_cache.metrics() is not being exported.

      - alert: PgLocalCacheUnavailable
        expr: pg_local_cache_up{job="pg-local-cache"} == 0
        for: 1m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache reports unavailable
          description: The extension metrics endpoint reports that shared state is not ready.

      - alert: PgLocalCacheWorkersMissing
        expr: pg_local_cache_workers_running < pg_local_cache_workers_configured
        for: 2m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: One or more pg_local_cache workers are missing
          description: The number of running RESP workers is below the configured worker count.

      - alert: PgLocalCacheMappingsIncomplete
        expr: pg_local_cache_workers_with_incomplete_mappings > 0
        for: 1m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache mappings are not current on every worker
          description: At least one RESP worker has not loaded the current complete table-mapping generation.

  - name: pg_local_cache.capacity
    rules:
      - alert: PgLocalCacheMemoryBudgetHigh
        expr: pg_local_cache_estimated_memory_bytes / clamp_min(pg_local_cache_memory_budget_bytes, 1) > 0.85
        for: 30m
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache memory budget has little headroom
          description: The deterministic extension memory estimate exceeds 85% of its startup budget.

      - alert: PgLocalCacheMemoryBudgetCritical
        expr: pg_local_cache_estimated_memory_bytes / clamp_min(pg_local_cache_memory_budget_bytes, 1) > 0.95
        for: 5m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache memory budget is almost exhausted
          description: The deterministic extension memory estimate exceeds 95% of its startup budget.

      - alert: PgLocalCacheNearCapacity
        expr: pg_local_cache_entries / clamp_min(pg_local_cache_cache_capacity, 1) > 0.90
        for: 15m
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache is near entry capacity
          description: More than 90% of cache entry slots have been occupied for 15 minutes.

      - alert: PgLocalCacheAtCapacity
        expr: pg_local_cache_entries / clamp_min(pg_local_cache_cache_capacity, 1) > 0.99
        for: 5m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache is at entry capacity
          description: More than 99% of cache entry slots have been occupied for five minutes.

      - alert: PgLocalCacheClientSaturation
        expr: pg_local_cache_active_clients / clamp_min(pg_local_cache_max_clients, 1) > 0.80
        for: 5m
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache client capacity is under pressure
          description: Active RESP clients have exceeded 80% of the global limit for five minutes.

      - alert: PgLocalCacheClientSaturationCritical
        expr: pg_local_cache_active_clients / clamp_min(pg_local_cache_max_clients, 1) > 0.95
        for: 2m
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache client capacity is almost exhausted
          description: Active RESP clients have exceeded 95% of the global limit for two minutes.

  - name: pg_local_cache.failures
    rules:
      - alert: PgLocalCacheClientLimitRejections
        expr: increase(pg_local_cache_client_limit_rejections_total[5m]) > 0
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache rejected clients at the global limit
          description: At least one RESP connection was rejected by the OOM safety limit in five minutes.

      - alert: PgLocalCacheConnectionRejectionsHigh
        expr: increase(pg_local_cache_rejected_connections_total[5m]) > 10
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache connection rejection rate is high
          description: More than ten RESP connections were rejected in five minutes.

      - alert: PgLocalCacheAuthenticationFailures
        expr: increase(pg_local_cache_authentication_failures_total[5m]) > 5
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache authentication failures detected
          description: More than five RESP authentication attempts failed in five minutes.

      - alert: PgLocalCacheSlowClientDrops
        expr: increase(pg_local_cache_slow_client_drops_total[5m]) > 0
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache dropped slow clients
          description: At least one slow RESP client was disconnected in five minutes.

      - alert: PgLocalCacheDirtyKeyFallback
        expr: increase(pg_local_cache_dirty_key_limit_fallbacks_total[10m]) > 0
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache transaction dirty-key limit was reached
          description: At least one transaction used relation-wide invalidation after reaching its key limit.

      - alert: PgLocalCacheMappingReloadFailure
        expr: increase(pg_local_cache_mapping_reload_failures_total[5m]) > 0
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache mapping reload failed
          description: A mapping reload failure occurred in the last five minutes.

      - alert: PgLocalCacheMappingReloadIncomplete
        expr: increase(pg_local_cache_mapping_reload_incomplete_retries_total[5m]) > 0
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache retried an incomplete mapping set
          description: At least one worker omitted an unsafe mapping during reload in the last five minutes.

  - name: pg_local_cache.container_oom
    rules:
      - alert: PgLocalCacheContainerMemoryPressure
        expr: |
          container_memory_working_set_bytes{name=~".*pg_local_cache.*postgres.*"}
            / clamp_min(container_spec_memory_limit_bytes{name=~".*pg_local_cache.*postgres.*"}, 1) > 0.90
        for: 10m
        labels:
          severity: warning
          component: pg_local_cache
        annotations:
          summary: pg_local_cache PostgreSQL container memory pressure
          description: The PostgreSQL container has used more than 90% of its memory limit for ten minutes.

      - alert: PgLocalCacheContainerOOMKilled
        expr: increase(container_oom_events_total{name=~".*pg_local_cache.*postgres.*"}[10m]) > 0
        labels:
          severity: critical
          component: pg_local_cache
        annotations:
          summary: pg_local_cache PostgreSQL container had an OOM event
          description: cAdvisor reported an OOM event for the PostgreSQL container in the last ten minutes.
