Add composite indexes for workspace+id columns for PostgreSQL
Browse files- lightrag/kg/postgres_impl.py +23 -0
lightrag/kg/postgres_impl.py
CHANGED
@@ -510,6 +510,29 @@ class PostgreSQLDB:
|
|
510 |
f"PostgreSQL, Failed to create index on table {k}, Got: {e}"
|
511 |
)
|
512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
513 |
# After all tables are created, attempt to migrate timestamp fields
|
514 |
try:
|
515 |
await self._migrate_timestamp_columns()
|
|
|
510 |
f"PostgreSQL, Failed to create index on table {k}, Got: {e}"
|
511 |
)
|
512 |
|
513 |
+
# Create composite index for (workspace, id) columns in each table
|
514 |
+
try:
|
515 |
+
composite_index_name = f"idx_{k.lower()}_workspace_id"
|
516 |
+
check_composite_index_sql = f"""
|
517 |
+
SELECT 1 FROM pg_indexes
|
518 |
+
WHERE indexname = '{composite_index_name}'
|
519 |
+
AND tablename = '{k.lower()}'
|
520 |
+
"""
|
521 |
+
composite_index_exists = await self.query(check_composite_index_sql)
|
522 |
+
|
523 |
+
if not composite_index_exists:
|
524 |
+
create_composite_index_sql = (
|
525 |
+
f"CREATE INDEX {composite_index_name} ON {k}(workspace, id)"
|
526 |
+
)
|
527 |
+
logger.info(
|
528 |
+
f"PostgreSQL, Creating composite index {composite_index_name} on table {k}"
|
529 |
+
)
|
530 |
+
await self.execute(create_composite_index_sql)
|
531 |
+
except Exception as e:
|
532 |
+
logger.error(
|
533 |
+
f"PostgreSQL, Failed to create composite index on table {k}, Got: {e}"
|
534 |
+
)
|
535 |
+
|
536 |
# After all tables are created, attempt to migrate timestamp fields
|
537 |
try:
|
538 |
await self._migrate_timestamp_columns()
|