Duplicate key value violates unique constraint "workflow_statistics_pkey"

I have the same error, is there a way to fix it?
My docker-compose file:

compose
version: "3.8"
services:
  n8n-master:
    image: n8nio/n8n:latest
    depends_on:
      - redis
      - db
    ports:
      - 5678:5678
    environment:
      - DB_POSTGRESDB_DATABASE=postgres
      - DB_POSTGRESDB_USER=postgres
      - DB_POSTGRESDB_PASSWORD=pass
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_TYPE=postgresdb
      - N8N_BASIC_AUTH_PASSWORD=123
      - N8N_BASIC_AUTH_USER=admin
      - EXECUTIONS_MODE=queue
      - EXECUTIONS_DATA_MAX_AGE=72
      - EXECUTIONS_DATA_PRUNE="true"
      - GENERIC_TIMEZONE=America/Sao_Paulo
      - N8N_BASIC_AUTH_ACTIVE="true"
      - N8N_ENCRYPTION_KEY=ae1e030c-2bb1-4175-9028-20c2d87276c5
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - N8N_DISABLE_PRODUCTION_MAIN_PROCESS=true
      - N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN=true

  n8n-worker-1:
    image: n8nio/n8n:latest
    depends_on:
      - redis
      - db
      - n8n-master
    environment:
      - N8N_ENCRYPTION_KEY=ae1e030c-2bb1-4175-9028-20c2d87276c5
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - DB_POSTGRESDB_DATABASE=postgres
      - DB_POSTGRESDB_USER=postgres
      - DB_POSTGRESDB_PASSWORD=pass
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_TYPE=postgresdb
    command: ["n8n", "worker"]

  n8n-worker-2:
    image: n8nio/n8n:latest
    depends_on:
      - redis
      - db
      - n8n-master
    environment:
      - N8N_ENCRYPTION_KEY=ae1e030c-2bb1-4175-9028-20c2d87276c5
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - DB_POSTGRESDB_DATABASE=postgres
      - DB_POSTGRESDB_USER=postgres
      - DB_POSTGRESDB_PASSWORD=pass
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_TYPE=postgresdb
    command: ["n8n", "worker", "--concurrency=5"]

  n8n-webhook-1:
    image: n8nio/n8n:latest
    depends_on:
      - redis
      - db
      - n8n-master
    ports:
      - 5679:5678
    environment:
      - N8N_ENCRYPTION_KEY=ae1e030c-2bb1-4175-9028-20c2d87276c5
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - DB_POSTGRESDB_DATABASE=postgres
      - DB_POSTGRESDB_USER=postgres
      - DB_POSTGRESDB_PASSWORD=pass
      - DB_POSTGRESDB_HOST=db
      - DB_POSTGRESDB_PORT=5432
      - DB_TYPE=postgresdb
    command: ["n8n", "webhook"]

  redis:
    image: redis:latest
    volumes:
      - redis-data:/data

  db:
    image: postgres:latest
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=pass
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  redis-data:
  db-data:

the system starts, but postgree gives this error when starting the process, nothing goes into the queue.

2023-04-30 13:01:11.084 UTC [25] LOG:  checkpoint complete: wrote 18 buffers (0.1%); 0 WAL file(s) added, 0 removed, 0 recycled; write=1.709 s, sync=0.002 s, total=1.715 s; sync files=18, longest=0.001 s, average=0.001 s; distance=21 kB, estimate=21 kB
2023-04-30 13:01:25.881 UTC [228] ERROR:  duplicate key value violates unique constraint "workflow_statistics_pkey"
2023-04-30 13:01:25.881 UTC [228] DETAIL:  Key ("workflowId", name)=(2, data_loaded) already exists.
2023-04-30 13:01:25.881 UTC [228] STATEMENT:  INSERT INTO "public"."workflow_statistics"("count", "latestEvent", "name", "workflowId") VALUES ($1, $2, $3, $4)
1 Like

Hi @roughriver74, this issue is currently being prioritized by @romain-n8n from our product team. I don’t have an ETA yet, but perhaps he already has more details to share here.

Solution in replace insert function with already prepaired query. in n8n/packages/cli/src/events/WorkflowStatistics.ts

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.