mirror of
https://github.com/langchain-ai/langchain-postgres.git
synced 2026-07-16 01:33:18 -04:00
Async Retriever Error with SQLAlchemy - "cannot insert multiple commands into a prepared statement" #39
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @alexanderflorean on GitHub (Aug 7, 2024).
Description
When running the retriever in
langchain-postgresas an asynchronous instance, I encounter the following error, related to the SQL statement execution. The message suggests that the problem arises from attempting to insert multiple SQL commands into a prepared statement, which seems to not be supported.Error Message
My thoughts
The error appears to be caused by the SQL statement that combines two commands: acquiring an advisory lock and creating an extension, executed as a single statement, even though it is separated by ";" in the sql text statement. This is seems to not be supported in asynchronous execution with
asyncpgas it treats this as a syntax error in a prepared statement.Proposed Solution
I found that if the SQL commands are defined as separate statements, we avoids combining multiple commands into a single execution call.
The affected code, in
vectorstore.py:236:Here is the revised function implementation that fixed the issue for me:
By separating the statements into individual execution calls, the error is avoided, and the functionality remains intact. Although I haven't tried using it on a synchronous instance.
Question
Would love to get some feedback as I do not often post on issues. Shoud I create a pr for this fix to the issue?
@alexanderflorean commented on GitHub (Aug 7, 2024):
Also, I found after posting this issue, a similar issue #86, where it was recommended to use the
psycopg3engine whereas I'm usingasyncpg.However, switching to
psycopg3, I recived the following error:sqlalchemy.exc.InterfaceError: (psycopg.InterfaceError) Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
(Background on this error at: https://sqlalche.me/e/20/rvf5)
I tried to fix that error, using the following, as stated in the error:
This did not fix the issue, when using the
psycopg3engine.@eyurtsev commented on GitHub (Aug 9, 2024):
Could you provide the stack trace?
@eyurtsev commented on GitHub (Sep 13, 2024):
Closing as issue appears stale