mirror of https://github.com/immich-app/immich.git
feat(server)!: pgvecto.rs 0.2 and pgvector compatibility (#6785)
* basic changes update version check set ef_search for clip * pgvector compatibility Revert "pgvector compatibility" This reverts commit 2b66a52aa4097dd27da58138c5288fd87cb9b24a. pgvector compatibility: minimal edition pgvector startup check * update extension at startup * wording shortened vector extension variable name * nightly docker * fixed version checks * update tests add tests for updating extension remove unnecessary check * simplify `getRuntimeConfig` * wording * reindex on minor version update * 0.2 upgrade testing update prod compose * acquire lock for init * wip vector down on shutdown * use upgrade helper * update image tag * refine restart check check error message * test reindex testing upstream fix formatting fixed reindexing * use enum in signature * fix tests remove unused code * add reindexing tests * update to official 0.2 remove alpha from version name * add warning test if restart required * update test image to 0.2.0 * linting and test cleanup * formatting * update sql * wording * handle setting search path for new and existing databases * handle new db in reindex check * fix post-update reindexing * get dim size * formatting * use vbase * handle different db name * update sql * linting * fix suggested envpull/6960/head
parent
b2775c445a
commit
56b0643890
@ -1,80 +1,157 @@
|
||||
import { ImmichLogger } from '@app/infra/logger';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
import { Version } from '../domain.constant';
|
||||
import { DatabaseExtension, IDatabaseRepository } from '../repositories';
|
||||
import { Version, VersionType } from '../domain.constant';
|
||||
import {
|
||||
DatabaseExtension,
|
||||
DatabaseLock,
|
||||
IDatabaseRepository,
|
||||
VectorExtension,
|
||||
VectorIndex,
|
||||
extName,
|
||||
} from '../repositories';
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseService {
|
||||
private logger = new ImmichLogger(DatabaseService.name);
|
||||
private vectorExt: VectorExtension;
|
||||
minPostgresVersion = 14;
|
||||
minVectorsVersion = new Version(0, 1, 1);
|
||||
maxVectorsVersion = new Version(0, 1, 11);
|
||||
minVectorsVersion = new Version(0, 2, 0);
|
||||
vectorsVersionPin = VersionType.MINOR;
|
||||
minVectorVersion = new Version(0, 5, 0);
|
||||
vectorVersionPin = VersionType.MAJOR;
|
||||
|
||||
constructor(@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository) {}
|
||||
constructor(@Inject(IDatabaseRepository) private databaseRepository: IDatabaseRepository) {
|
||||
this.vectorExt = this.databaseRepository.getPreferredVectorExtension();
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.assertPostgresql();
|
||||
await this.createVectors();
|
||||
await this.assertVectors();
|
||||
await this.databaseRepository.runMigrations();
|
||||
}
|
||||
await this.databaseRepository.withLock(DatabaseLock.Migrations, async () => {
|
||||
await this.createVectorExtension();
|
||||
await this.updateVectorExtension();
|
||||
await this.assertVectorExtension();
|
||||
|
||||
private async assertVectors() {
|
||||
const version = await this.databaseRepository.getExtensionVersion(DatabaseExtension.VECTORS);
|
||||
if (version == null) {
|
||||
throw new Error('Unexpected: The pgvecto.rs extension is not installed.');
|
||||
}
|
||||
try {
|
||||
if (await this.databaseRepository.shouldReindex(VectorIndex.CLIP)) {
|
||||
await this.databaseRepository.reindex(VectorIndex.CLIP);
|
||||
}
|
||||
|
||||
const image = await this.getVectorsImage();
|
||||
const suggestion = image ? `, such as with the docker image '${image}'` : '';
|
||||
if (await this.databaseRepository.shouldReindex(VectorIndex.FACE)) {
|
||||
await this.databaseRepository.reindex(VectorIndex.FACE);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
'Could not run vector reindexing checks. If the extension was updated, please restart the Postgres instance.',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (version.isEqual(new Version(0, 0, 0))) {
|
||||
throw new Error(
|
||||
`The pgvecto.rs extension version is ${version}, which means it is a nightly release.` +
|
||||
`Please run 'DROP EXTENSION IF EXISTS vectors' and switch to a release version${suggestion}.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (version.isNewerThan(this.maxVectorsVersion)) {
|
||||
throw new Error(`
|
||||
The pgvecto.rs extension version is ${version} instead of ${this.maxVectorsVersion}.
|
||||
Please run 'DROP EXTENSION IF EXISTS vectors' and switch to ${this.maxVectorsVersion}${suggestion}.`);
|
||||
}
|
||||
await this.databaseRepository.runMigrations();
|
||||
});
|
||||
}
|
||||
|
||||
if (version.isOlderThan(this.minVectorsVersion)) {
|
||||
private async assertPostgresql() {
|
||||
const { major } = await this.databaseRepository.getPostgresVersion();
|
||||
if (major < this.minPostgresVersion) {
|
||||
throw new Error(`
|
||||
The pgvecto.rs extension version is ${version}, which is older than the minimum supported version ${this.minVectorsVersion}.
|
||||
Please upgrade to this version or later${suggestion}.`);
|
||||
The PostgreSQL version is ${major}, which is older than the minimum supported version ${this.minPostgresVersion}.
|
||||
Please upgrade to this version or later.`);
|
||||
}
|
||||
}
|
||||
|
||||
private async createVectors() {
|
||||
await this.databaseRepository.createExtension(DatabaseExtension.VECTORS).catch(async (error: QueryFailedError) => {
|
||||
const image = await this.getVectorsImage();
|
||||
private async createVectorExtension() {
|
||||
await this.databaseRepository.createExtension(this.vectorExt).catch(async (error: QueryFailedError) => {
|
||||
const otherExt =
|
||||
this.vectorExt === DatabaseExtension.VECTORS ? DatabaseExtension.VECTOR : DatabaseExtension.VECTORS;
|
||||
this.logger.fatal(`
|
||||
Failed to create pgvecto.rs extension.
|
||||
If you have not updated your Postgres instance to a docker image that supports pgvecto.rs (such as '${image}'), please do so.
|
||||
See the v1.91.0 release notes for more info: https://github.com/immich-app/immich/releases/tag/v1.91.0'
|
||||
Failed to activate ${extName[this.vectorExt]} extension.
|
||||
Please ensure the Postgres instance has ${extName[this.vectorExt]} installed.
|
||||
|
||||
If the Postgres instance already has ${extName[this.vectorExt]} installed, Immich may not have the necessary permissions to activate it.
|
||||
In this case, please run 'CREATE EXTENSION IF NOT EXISTS ${this.vectorExt}' manually as a superuser.
|
||||
See https://immich.app/docs/guides/database-queries for how to query the database.
|
||||
|
||||
Alternatively, if your Postgres instance has ${extName[otherExt]}, you may use this instead by setting the environment variable 'VECTOR_EXTENSION=${otherExt}'.
|
||||
Note that switching between the two extensions after a successful startup is not supported.
|
||||
The exception is if your version of Immich prior to upgrading was 1.90.2 or earlier.
|
||||
In this case, you may set either extension now, but you will not be able to switch to the other extension following a successful startup.
|
||||
`);
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
private async getVectorsImage() {
|
||||
const { major } = await this.databaseRepository.getPostgresVersion();
|
||||
if (![14, 15, 16].includes(major)) {
|
||||
return null;
|
||||
private async updateVectorExtension() {
|
||||
const [version, availableVersion] = await Promise.all([
|
||||
this.databaseRepository.getExtensionVersion(this.vectorExt),
|
||||
this.databaseRepository.getAvailableExtensionVersion(this.vectorExt),
|
||||
]);
|
||||
if (version == null) {
|
||||
throw new Error(`Unexpected: ${extName[this.vectorExt]} extension is not installed.`);
|
||||
}
|
||||
|
||||
if (availableVersion == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const maxVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.vectorVersionPin : this.vectorsVersionPin;
|
||||
const isNewer = availableVersion.isNewerThan(version);
|
||||
if (isNewer == null || isNewer > maxVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.logger.log(`Updating ${extName[this.vectorExt]} extension to ${availableVersion}`);
|
||||
const { restartRequired } = await this.databaseRepository.updateVectorExtension(this.vectorExt, availableVersion);
|
||||
if (restartRequired) {
|
||||
this.logger.warn(`
|
||||
The ${extName[this.vectorExt]} extension has been updated to ${availableVersion}.
|
||||
Please restart the Postgres instance to complete the update.`);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.warn(`
|
||||
The ${extName[this.vectorExt]} extension version is ${version}, but ${availableVersion} is available.
|
||||
Immich attempted to update the extension, but failed to do so.
|
||||
This may be because Immich does not have the necessary permissions to update the extension.
|
||||
|
||||
Please run 'ALTER EXTENSION ${this.vectorExt} UPDATE' manually as a superuser.
|
||||
See https://immich.app/docs/guides/database-queries for how to query the database.`);
|
||||
this.logger.error(error);
|
||||
}
|
||||
return `tensorchord/pgvecto-rs:pg${major}-v${this.maxVectorsVersion}`;
|
||||
}
|
||||
|
||||
private async assertPostgresql() {
|
||||
const { major } = await this.databaseRepository.getPostgresVersion();
|
||||
if (major < this.minPostgresVersion) {
|
||||
private async assertVectorExtension() {
|
||||
const version = await this.databaseRepository.getExtensionVersion(this.vectorExt);
|
||||
if (version == null) {
|
||||
throw new Error(`Unexpected: The ${extName[this.vectorExt]} extension is not installed.`);
|
||||
}
|
||||
|
||||
if (version.isEqual(new Version(0, 0, 0))) {
|
||||
throw new Error(`
|
||||
The PostgreSQL version is ${major}, which is older than the minimum supported version ${this.minPostgresVersion}.
|
||||
Please upgrade to this version or later.`);
|
||||
The ${extName[this.vectorExt]} extension version is ${version}, which means it is a nightly release.
|
||||
|
||||
Please run 'DROP EXTENSION IF EXISTS ${this.vectorExt}' and switch to a release version.
|
||||
See https://immich.app/docs/guides/database-queries for how to query the database.`);
|
||||
}
|
||||
|
||||
const minVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.minVectorVersion : this.minVectorsVersion;
|
||||
const maxVersion = this.vectorExt === DatabaseExtension.VECTOR ? this.vectorVersionPin : this.vectorsVersionPin;
|
||||
|
||||
if (version.isOlderThan(minVersion) || version.isNewerThan(minVersion) > maxVersion) {
|
||||
const allowedReleaseType = maxVersion === VersionType.MAJOR ? '' : ` ${VersionType[maxVersion].toLowerCase()}`;
|
||||
const releases =
|
||||
maxVersion === VersionType.EQUAL
|
||||
? minVersion.toString()
|
||||
: `${minVersion} and later${allowedReleaseType} releases`;
|
||||
|
||||
throw new Error(`
|
||||
The ${extName[this.vectorExt]} extension version is ${version}, but Immich only supports ${releases}.
|
||||
|
||||
If the Postgres instance already has a compatible version installed, Immich may not have the necessary permissions to activate it.
|
||||
In this case, please run 'ALTER EXTENSION UPDATE ${this.vectorExt}' manually as a superuser.
|
||||
See https://immich.app/docs/guides/database-queries for how to query the database.
|
||||
|
||||
Otherwise, please update the version of ${extName[this.vectorExt]} in the Postgres instance to a compatible version.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddVectorsToSearchPath1707000751533 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
const res = await queryRunner.query(`SELECT current_database() as db`);
|
||||
const databaseName = res[0]['db'];
|
||||
await queryRunner.query(`ALTER DATABASE ${databaseName} SET search_path TO "$user", public, vectors`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
const databaseName = await queryRunner.query(`SELECT current_database()`);
|
||||
await queryRunner.query(`ALTER DATABASE ${databaseName} SET search_path TO "$user", public`);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue