immich-app / immich

High performance self-hosted photo and video management solution.
https://immich.app
GNU Affero General Public License v3.0
45.19k stars 2.19k forks source link

Primary key conflict on fresh install of Immich #6759

Closed mmomjian closed 7 months ago

mmomjian commented 7 months ago

The bug

On a fresh run of Immich, I am getting a "duplicate key value violates unique constraint" error from Postgres at the very beginning of the logs for the immich container. Is anyone else able to replicate this same error? The issue does not persist after restarting an already initialized container - it is only present on the first run of a brand new instance, with a brand new UPLOAD_LOCATION and fresh (already existing database. Immich does not have superuser permission on the Postgres instance. Immich otherwise runs totally fine. Output below.

The OS that Immich Server is running on

Debian 12

Version of Immich Server

v1.93.3

Version of Immich Mobile App

n/a

Platform with the issue

Your docker-compose.yml content

n/a

Your .env content

n/a

Reproduction steps

1. Create a new instance of Immich connected to a fresh Postgres database.
2. Run `docker logs immich`

Additional information

/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219
            throw new QueryFailedError_1.QueryFailedError(query, parameters, err);
                  ^

QueryFailedError: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
    at PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:219:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9)
    at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17)
    at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35)
    at async DatabaseRepository.runMigrations (/usr/src/app/dist/infra/repositories/database.repository.js:42:9)
    at async DatabaseService.init (/usr/src/app/dist/domain/database/database.service.js:33:9)
    at async AppService.init (/usr/src/app/dist/immich/app.service.js:56:9)
    at async AppModule.onModuleInit (/usr/src/app/dist/immich/app.module.js:32:9)
    at async callModuleInitHook (/usr/src/app/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9) {
  query: '\n' +
    '        create table if not exists users\n' +
    '        (\n' +
    '            id          uuid      default uuid_generate_v4() not null\n' +
    '                constraint "PK_a3ffb1c0c8416b9fc6f907b7433"\n' +
    '                    primary key,\n' +
    '            email       varchar                              not null,\n' +
    '            password    varchar                              not null,\n' +
    '            salt        varchar                              not null,\n' +
    '            "createdAt" timestamp default now()              not null\n' +
    '        );\n' +
    '      ',
  parameters: undefined,
  driverError: error: duplicate key value violates unique constraint "pg_type_typname_nsp_index"
      at /usr/src/app/node_modules/pg/lib/client.js:526:17
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:184:25)
      at async CreateUserTable1645130759468.up (/usr/src/app/dist/infra/migrations/1645130759468-CreateUserTable.js:6:9)
      at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:225:17)
      at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:263:35)
jrasm91 commented 7 months ago

Both the immich-server and immich-microservices run database migrations when they start up. If you start them up at exactly the same time they sometimes run into this issue.

mmomjian commented 7 months ago

Yes, they are starting at the same time. Thanks for the insight. In my experience this happens nearly 100% of the time. Does this pose any threat to the health of the Immich instance that is created with both migrations taking place at the same time? I imported about 50k images into my instance and it is running fine but I will start over if needed.

jrasm91 commented 7 months ago

No, because they each run the migrations sequentially and inside of transactions so it's never an issue, except that one aborts if the other is doing the same thing at the same time.

mmomjian commented 7 months ago

That makes sense. Thank you. Would you be interested in me adding a "tips" section to the docker compose docs page outlining that this is a harmless message?

jrasm91 commented 7 months ago

Yeah, you can add a tip and/or a FAQ for this error, until we find a more reliable way to run the migrations without the concurrency issue.