Repository: zitadel/zitadel

Issue #4784: first schedule events issues
Created: 2022-11-30T09:35:25Z
The current implementation of the first schedule events have multiple problems.
The events are not always written at the first system start because many operations happen in parallel: https://discord.com/channels/927474939156643850/1019895518383583323/threads/1047145613176873010
Because the events are written on the system aggregate its not possible to reset projections for single instances.
We should design a better solution for this behaviour.

Duplicate issue #4895: [Bug]: Postgresql duplicate key value violates unique constraint "previous_sequence_unique"
Created: 2022-12-16T16:31:49Z
Preflight Checklist

 I could not find a solution in the documentation, the existing issues or discussions
 I have joined the ZITADEL chat

Environment
Self-hosted
Describe the bug
I tried to setup zitadel with postgresql and failed. It might be just that I checked the wrong documentations. my docker-composed is based of this post here #4304
constraint violation
when starting with either version 2.16.0 or 2.14.7 migrations start fine but at some point fail
g_1             | 2022-12-16 16:09:24.059 UTC [738] ERROR:  duplicate key value violates unique constraint "previous_sequence_unique"
pg_1             | 2022-12-16 16:09:24.059 UTC [738] DETAIL:  Key (previous_aggregate_sequence, instance_id)=(52, ) already exists.
pg_1             | 2022-12-16 16:09:24.059 UTC [738] STATEMENT:  WITH previous_data (aggregate_type_sequence, aggregate_sequence, resource_owner) AS (SELECT agg_type.seq, agg.seq, agg.ro FROM ( SELECT MAX(event_sequence) seq, 1 join_me FROM eventstore.events WHERE aggregate_type = $2 AND (CASE WHEN $9::TEXT IS NULL THEN instance_id is null else instance_id = $9::TEXT END)) AS agg_type LEFT JOIN ( SELECT event_sequence seq, resource_owner ro, 1 join_me FROM eventstore.events WHERE aggregate_type = $2 AND aggregate_id = $3 AND (CASE WHEN $9::TEXT IS NULL THEN instance_id is null else instance_id = $9::TEXT END) ORDER BY event_sequence DESC LIMIT 1) AS agg USING(join_me)) INSERT INTO eventstore.events ( event_type, aggregate_type, aggregate_id, aggregate_version, creation_date, event_data, editor_user, editor_service, resource_owner, instance_id, event_sequence, previous_aggregate_sequence, previous_aggregate_type_sequence) SELECT $1::VARCHAR AS event_type, $2::VARCHAR AS aggregate_type, $3::VARCHAR AS aggregate_id, $4::VARCHAR AS aggregate_version, NOW() AS creation_date, $5::JSONB AS event_data, $6::VARCHAR AS editor_user, $7::VARCHAR AS editor_service, COALESCE((resource_owner), $8::VARCHAR) AS resource_owner, $9::VARCHAR AS instance_id, NEXTVAL(CONCAT('eventstore.', (CASE WHEN $9 <> '' THEN CONCAT('i_', $9) ELSE 'system' END), '_seq')), aggregate_sequence AS previous_aggregate_sequence, aggregate_type_sequence AS previous_aggregate_type_sequence FROM previous_data RETURNING id, event_sequence, previous_aggregate_sequence, previous_aggregate_type_sequence, creation_date, resource_owner, instance_id

 \"previous_sequence_unique\" (SQLSTATE 23505))" projection=projections.users6
zitadel-start_1  | time="2022-12-16T16:09:24Z" level=info msg="query failed" aggregate=system aggregateId=SYSTEM aggregateType=system caller="/home/runner/work/zitadel/zitadel/internal/eventstore/repository/sql/crdb.go:141" error="ERROR: duplicate key value violates unique constraint \"previous_sequence_unique\" (SQLSTATE 23505)" eventType=system.projections.scheduler.succeeded instanceID=
zitadel-start_1  | time="2022-12-16T16:09:24Z" level=warning msg="unable to push first schedule succeeded" caller="/home/runner/work/zitadel/zitadel/internal/eventstore/handler/handler_projection.go:266" error="ID=SQL-SBP37 Message=unable to create event Parent=(ERROR: duplicate key value violates unique constraint \"previous_sequence_unique\" (SQLSTATE 23505))" projection=projections.login_names2

To reproduce
don't worry, pasted passwords are not used anywhere and where generated random for this example.
create a .env file
POSTGRES_USER=zitadel
POSTGRES_PASS=fFEjzOSKHKD6eU1RKbHB
POSTGRES_ADMIN_PASS=6OFEl6SXyLLKoIsnQMFA
VERSION=v2.16.0
MASTERKEY=U7ODyQwa5lkNEmdMLHIdYTdhMhad1CYs

create a docker-compose.yml
version: '3.8'

services:
  zitadel-init:
    networks:
      - 'zitadel'
    image: 'ghcr.io/zitadel/zitadel:${VERSION}'
    command: ["init", "zitadel"]
    environment:
      - ZITADEL_DATABASE_POSTGRES_HOST=pg
      - ZITADEL_DATABASE_POSTGRES_PORT=5432
      - ZITADEL_DATABASE_POSTGRES_DATABASE=zitadel
      - ZITADEL_DATABASE_POSTGRES_USER_USERNAME=${POSTGRES_USER}
      - ZITADEL_DATABASE_POSTGRES_USER_PASSWORD=${POSTGRES_PASS}
      - ZITADEL_DATABASE_POSTGRES_USER_SSL_MODE=disable
      - ZITADEL_EXTERNALSECURE=false
    depends_on:
      pg:
        condition: 'service_healthy'

  zitadel-start:
    restart: 'always'
    networks:
      - 'zitadel'
    image: 'ghcr.io/zitadel/zitadel:${VERSION}'
    command: ["start-from-setup", "--tlsMode", "external", "--masterkey", "${MASTERKEY}"]
    environment:
      - ZITADEL_DATABASE_POSTGRES_HOST=pg
      - ZITADEL_DATABASE_POSTGRES_PORT=5432
      - ZITADEL_DATABASE_POSTGRES_DATABASE=zitadel
      - ZITADEL_DATABASE_POSTGRES_USER_USERNAME=${POSTGRES_USER}
      - ZITADEL_DATABASE_POSTGRES_USER_PASSWORD=${POSTGRES_PASS}
      - ZITADEL_DATABASE_POSTGRES_USER_SSL_MODE=disable
      - ZITADEL_EXTERNALSECURE=false
    depends_on:
      zitadel-init:
        condition: 'service_completed_successfully'
      pg:
        condition: 'service_healthy'
    ports:
      - '8080:8080'

  pg:
    image: postgres:15-alpine
    restart: always
    networks:
      - 'zitadel'
    environment:
      - POSTGRES_USER=postgres
      - PGUSER=postgres
      - POSTGRES_PASSWORD=${POSTGRES_ADMIN_PASS}
    ports:
      - '5432:5432'
    healthcheck:
      test: ["CMD-SHELL", "pg_isready", "-d", "zitadel"]
      interval: 10s
      timeout: 60s
      retries: 5
      start_period: 5s
    volumes:
      - pg-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

networks:
  zitadel:

volumes:
  pg-data:
Screenshots
No response
Expected behavior
No response
Version
2.16.0
Operating System
No response
Relevant Configuration
No response
Additional Context
No response

Closing comment (2022-12-16T16:46:09Z): just joined the chat as well, seems to be a duplicate of #4784