Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transactions not being committed #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions langchain_postgres/chat_message_histories.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def get_messages(self) -> List[BaseMessage]:
with self._connection.cursor() as cursor:
cursor.execute(query, {"session_id": self._session_id})
items = [record[0] for record in cursor.fetchall()]
self._connection.commit()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a transaction need to be committed here? There's no mutation that's done in get_messages

Copy link
Author

@LeiTi34 LeiTi34 Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the transaction is still created and never commits, which can block other operations.

See the first code example in the docs.
There conn.close() closes the transaction.

Another option would be to use Autocommit transactions.

Copy link
Author

@LeiTi34 LeiTi34 Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While doing some research I stumbled on this article, which describes some implications of having not committed transactions.

Basically, it avoids VACUUM marking rows as eligible for reuse, as long as the connection is open.

That means, that if someone builds an application that is rarely restarted using this library, it can grow the size of the DB drastically.


messages = messages_from_dict(items)
return messages
Expand All @@ -336,6 +337,7 @@ async def aget_messages(self) -> List[BaseMessage]:
async with self._aconnection.cursor() as cursor:
await cursor.execute(query, {"session_id": self._session_id})
items = [record[0] for record in await cursor.fetchall()]
await self._aconnection.commit()

messages = messages_from_dict(items)
return messages
Expand Down