name: pg_local_cache_sql_only

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: 512mb
    mem_limit: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_LIMIT:-1g}
    mem_reservation: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_RESERVATION:-512m}
    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: 127.0.0.1
      PG_LOCAL_CACHE_PORT: "0"
      PG_LOCAL_CACHE_WORKERS: "1"
      PG_LOCAL_CACHE_CACHE_ENTRIES: "16384"
      PG_LOCAL_CACHE_RELATION_STATES: "1024"
      PG_LOCAL_CACHE_MAX_CLIENTS: "256"
      PG_LOCAL_CACHE_MAX_CLIENTS_PER_WORKER: "64"
      PG_LOCAL_CACHE_MEMORY_BUDGET_MB: "256"
      PG_LOCAL_CACHE_MAX_WORKER_PROCESSES: "8"
      PG_LOCAL_CACHE_SINGLEFLIGHT_WAIT_MS: "25"
      PG_LOCAL_CACHE_MAX_DIRTY_KEYS: "4096"
    ports:
      - "127.0.0.1:${POSTGRES_HOST_PORT:-5432}:5432"
    secrets:
      - source: postgres_password
        target: postgres_password
        mode: 0600
    volumes:
      - postgres_sql_only_data:/var/lib/postgresql/data
    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"

  postgres_stock:
    image: postgres:${POSTGRES_MAJOR:-16}-${POSTGRES_VARIANT:-bookworm}
    restart: unless-stopped
    stop_grace_period: 60s
    shm_size: 512mb
    mem_limit: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_LIMIT:-1g}
    mem_reservation: ${PG_LOCAL_CACHE_CONTAINER_MEMORY_RESERVATION:-512m}
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
    secrets:
      - source: postgres_password
        target: postgres_password
        mode: 0600
    volumes:
      - postgres_stock_sql_only_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready --username postgres --dbname $${POSTGRES_DB}"]
      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

volumes:
  postgres_sql_only_data:
  postgres_stock_sql_only_data:
