gzdaniel commited on
Commit
89e0b3b
·
1 Parent(s): f75614b

Fix: Silence PostgreSQL logs during idempotent graph initialization

Browse files
Files changed (1) hide show
  1. lightrag/kg/postgres_impl.py +19 -14
lightrag/kg/postgres_impl.py CHANGED
@@ -289,25 +289,31 @@ class PostgreSQLDB:
289
  sql: str,
290
  data: dict[str, Any] | None = None,
291
  upsert: bool = False,
 
292
  with_age: bool = False,
293
  graph_name: str | None = None,
294
  ):
295
  try:
296
  async with self.pool.acquire() as connection: # type: ignore
297
  if with_age and graph_name:
298
- await self.configure_age(connection, graph_name) # type: ignore
299
  elif with_age and not graph_name:
300
  raise ValueError("Graph name is required when with_age is True")
301
 
302
  if data is None:
303
- await connection.execute(sql) # type: ignore
304
  else:
305
- await connection.execute(sql, *data.values()) # type: ignore
306
  except (
307
  asyncpg.exceptions.UniqueViolationError,
308
  asyncpg.exceptions.DuplicateTableError,
 
 
309
  ) as e:
310
- if upsert:
 
 
 
311
  print("Key value duplicate, but upsert succeeded.")
312
  else:
313
  logger.error(f"Upsert error: {e}")
@@ -1212,16 +1218,15 @@ class PGGraphStorage(BaseGraphStorage):
1212
  ]
1213
 
1214
  for query in queries:
1215
- try:
1216
- await self.db.execute(
1217
- query,
1218
- upsert=True,
1219
- with_age=True,
1220
- graph_name=self.graph_name,
1221
- )
1222
- # logger.info(f"Successfully executed: {query}")
1223
- except Exception:
1224
- continue
1225
 
1226
  async def finalize(self):
1227
  if self.db is not None:
 
289
  sql: str,
290
  data: dict[str, Any] | None = None,
291
  upsert: bool = False,
292
+ ignore_if_exists: bool = False,
293
  with_age: bool = False,
294
  graph_name: str | None = None,
295
  ):
296
  try:
297
  async with self.pool.acquire() as connection: # type: ignore
298
  if with_age and graph_name:
299
+ await self.configure_age(connection, graph_name)
300
  elif with_age and not graph_name:
301
  raise ValueError("Graph name is required when with_age is True")
302
 
303
  if data is None:
304
+ await connection.execute(sql)
305
  else:
306
+ await connection.execute(sql, *data.values())
307
  except (
308
  asyncpg.exceptions.UniqueViolationError,
309
  asyncpg.exceptions.DuplicateTableError,
310
+ asyncpg.exceptions.DuplicateObjectError, # Catch "already exists" error
311
+ asyncpg.exceptions.InvalidSchemaNameError, # Also catch for AGE extension "already exists"
312
  ) as e:
313
+ if ignore_if_exists:
314
+ # If the flag is set, just ignore these specific errors
315
+ pass
316
+ elif upsert:
317
  print("Key value duplicate, but upsert succeeded.")
318
  else:
319
  logger.error(f"Upsert error: {e}")
 
1218
  ]
1219
 
1220
  for query in queries:
1221
+ # Use the new flag to silently ignore "already exists" errors
1222
+ # at the source, preventing log spam.
1223
+ await self.db.execute(
1224
+ query,
1225
+ upsert=True,
1226
+ ignore_if_exists=True, # Pass the new flag
1227
+ with_age=True,
1228
+ graph_name=self.graph_name,
1229
+ )
 
1230
 
1231
  async def finalize(self):
1232
  if self.db is not None: