Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Override global statement timeout when creating indexes in Postgres (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Shay committed Aug 17, 2023
1 parent 8a4fb7a commit 0377cb4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/16085.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Override global statement timeout when creating indexes in Postgres.
12 changes: 12 additions & 0 deletions synapse/storage/background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class BackgroundUpdater:
def __init__(self, hs: "HomeServer", database: "DatabasePool"):
self._clock = hs.get_clock()
self.db_pool = database
self.hs = hs

self._database_name = database.name()

Expand Down Expand Up @@ -758,6 +759,11 @@ def create_index_psql(conn: Connection) -> None:
logger.debug("[SQL] %s", sql)
c.execute(sql)

# override the global statement timeout to avoid accidentally squashing
# a long-running index creation process
timeout_sql = "SET SESSION statement_timeout = 0"
c.execute(timeout_sql)

sql = (
"CREATE %(unique)s INDEX CONCURRENTLY %(name)s"
" ON %(table)s"
Expand All @@ -778,6 +784,12 @@ def create_index_psql(conn: Connection) -> None:
logger.debug("[SQL] %s", sql)
c.execute(sql)
finally:
# mypy ignore - `statement_timeout` is defined on PostgresEngine
# reset the global timeout to the default
default_timeout = self.db_pool.engine.statement_timeout # type: ignore[attr-defined]
undo_timeout_sql = f"SET statement_timeout = {default_timeout}"
conn.cursor().execute(undo_timeout_sql)

conn.set_session(autocommit=False) # type: ignore

def create_index_sqlite(conn: Connection) -> None:
Expand Down

0 comments on commit 0377cb4

Please sign in to comment.