name: pg_local_cache

services:
  postgres:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        POSTGRES_MAJOR: ${POSTGRES_MAJOR:-16}
        POSTGRES_VARIANT: ${POSTGRES_VARIANT:-bookworm}
    image: pg_local_cache:1.2.1
    restart: unless-stopped
    stop_grace_period: 60s
    shm_size: 1gb
    mem_limit: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_LIMIT:-2g}
    mem_reservation: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_RESERVATION:-1g}
    environment:
      PGDATA: /var/lib/postgresql/data
      POSTGRES_DB: app
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
      PG_LOCAL_CACHE_DATABASE: app
      PG_LOCAL_CACHE_ROLE: local_cache_worker
      PG_LOCAL_CACHE_BIND_ADDRESS: 0.0.0.0
      PG_LOCAL_CACHE_PORT: "6380"
      PG_LOCAL_CACHE_WORKERS: "8"
      PG_LOCAL_CACHE_CACHE_ENTRIES: "65536"
      PG_LOCAL_CACHE_RELATION_STATES: "1024"
      PG_LOCAL_CACHE_MAX_CLIENTS: "512"
      PG_LOCAL_CACHE_MAX_CLIENTS_PER_WORKER: "64"
      PG_LOCAL_CACHE_MEMORY_BUDGET_MB: "1024"
      PG_LOCAL_CACHE_MAX_WORKER_PROCESSES: "16"
      PG_LOCAL_CACHE_IDLE_TIMEOUT_MS: "300000"
      PG_LOCAL_CACHE_STATEMENT_TIMEOUT_MS: "2000"
      PG_LOCAL_CACHE_LOCK_TIMEOUT_MS: "250"
      PG_LOCAL_CACHE_SINGLEFLIGHT_WAIT_MS: "25"
      PG_LOCAL_CACHE_MAX_PIPELINE_COMMANDS: "256"
      PG_LOCAL_CACHE_MAX_DIRTY_KEYS: "4096"
      PG_LOCAL_CACHE_AUTH_TOKEN_FILE: /run/secrets/pg_local_cache_auth_token
    ports:
      - "127.0.0.1:${POSTGRES_HOST_PORT:-5432}:5432"
      - "127.0.0.1:${PG_LOCAL_CACHE_HOST_PORT:-6380}:6380"
    secrets:
      - source: postgres_password
        target: postgres_password
        mode: 0600
      - source: pg_local_cache_auth_token
        target: pg_local_cache_auth_token
        mode: 0600
    volumes:
      - postgres_data:/var/lib/postgresql/data
    tmpfs:
      - /run/pg_local_cache:rw,noexec,nosuid,nodev,size=1m,mode=0700
    ulimits:
      nofile:
        soft: 65536
        hard: 65536
    healthcheck:
      test: ["CMD", "/usr/local/bin/pg_local_cache_healthcheck"]
      interval: 10s
      timeout: 5s
      start_period: 30s
      retries: 6
    logging:
      driver: json-file
      options:
        max-size: 10m
        max-file: "5"

secrets:
  postgres_password:
    file: ./secrets/postgres_password
  pg_local_cache_auth_token:
    file: ./secrets/pg_local_cache_auth_token

volumes:
  postgres_data:
