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

[24.0] Optimize/fix sqlite hid update statement #19106

Merged
Merged
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
28 changes: 18 additions & 10 deletions lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
update,
VARCHAR,
)
from sqlalchemy.exc import OperationalError
from sqlalchemy.exc import (
CompileError,
OperationalError,
)
from sqlalchemy.ext import hybrid
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.orderinglist import ordering_list
Expand Down Expand Up @@ -3120,16 +3123,21 @@ def _next_hid(self, n=1):
table = self.__table__
history_id = cached_id(self)
update_stmt = update(table).where(table.c.id == history_id).values(hid_counter=table.c.hid_counter + n)
update_returning_stmnt = update_stmt.returning(table.c.hid_counter)

with engine.begin() as conn:
if engine.name in ["postgres", "postgresql"]:
stmt = update_stmt.returning(table.c.hid_counter)
updated_hid = conn.execute(stmt).scalar()
hid = updated_hid - n
else:
select_stmt = select(table.c.hid_counter).where(table.c.id == history_id).with_for_update()
hid = conn.execute(select_stmt).scalar()
conn.execute(update_stmt)
if engine.name != "sqlite":
with engine.begin() as conn:
hid = conn.execute(update_returning_stmnt).scalar() - n
else:
try:
hid = session.execute(update_returning_stmnt).scalar() - n
except (CompileError, OperationalError):
# The RETURNING clause was added to SQLite in version 3.35.0.
# Not using FOR UPDATE either, since that was added in 3.45.0,
# and anyway does a whole-table lock
select_stmt = select(table.c.hid_counter).where(table.c.id == history_id)
hid = session.execute(select_stmt).scalar()
session.execute(update_stmt)

session.expire(self, ["hid_counter"])
return hid
Expand Down
Loading