From 1e1adcb64a710373623148ba52b10db505d47d46 Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 29 Jul 2025 23:03:09 +0800 Subject: [PATCH] Add index on track_id column in doc status table of PostgreSQL --- lightrag/kg/postgres_impl.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lightrag/kg/postgres_impl.py b/lightrag/kg/postgres_impl.py index c08c800b..ded3df2b 100644 --- a/lightrag/kg/postgres_impl.py +++ b/lightrag/kg/postgres_impl.py @@ -568,7 +568,7 @@ class PostgreSQLDB: ) async def _migrate_doc_status_add_track_id(self): - """Add track_id column to LIGHTRAG_DOC_STATUS table if it doesn't exist""" + """Add track_id column to LIGHTRAG_DOC_STATUS table if it doesn't exist and create index""" try: # Check if track_id column exists check_column_sql = """ @@ -593,8 +593,36 @@ class PostgreSQLDB: logger.info( "track_id column already exists in LIGHTRAG_DOC_STATUS table" ) + + # Check if track_id index exists + check_index_sql = """ + SELECT indexname + FROM pg_indexes + WHERE tablename = 'lightrag_doc_status' + AND indexname = 'idx_lightrag_doc_status_track_id' + """ + + index_info = await self.query(check_index_sql) + if not index_info: + logger.info( + "Creating index on track_id column for LIGHTRAG_DOC_STATUS table" + ) + create_index_sql = """ + CREATE INDEX idx_lightrag_doc_status_track_id ON LIGHTRAG_DOC_STATUS (track_id) + """ + await self.execute(create_index_sql) + logger.info( + "Successfully created index on track_id column for LIGHTRAG_DOC_STATUS table" + ) + else: + logger.info( + "Index on track_id column already exists for LIGHTRAG_DOC_STATUS table" + ) + except Exception as e: - logger.warning(f"Failed to add track_id column to LIGHTRAG_DOC_STATUS: {e}") + logger.warning( + f"Failed to add track_id column or index to LIGHTRAG_DOC_STATUS: {e}" + ) async def _migrate_field_lengths(self): """Migrate database field lengths: entity_name, source_id, target_id, and file_path"""