Override global statement timeout when creating indexes in Postgres (#16085)

This commit is contained in:
Shay 2023-08-17 02:30:02 -07:00 committed by GitHub
parent 8a4fb7a6ba
commit 0377cb4fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

1
changelog.d/16085.misc Normal file
View File

@ -0,0 +1 @@
Override global statement timeout when creating indexes in Postgres.

View File

@ -238,6 +238,7 @@ class BackgroundUpdater:
def __init__(self, hs: "HomeServer", database: "DatabasePool"): def __init__(self, hs: "HomeServer", database: "DatabasePool"):
self._clock = hs.get_clock() self._clock = hs.get_clock()
self.db_pool = database self.db_pool = database
self.hs = hs
self._database_name = database.name() self._database_name = database.name()
@ -758,6 +759,11 @@ class BackgroundUpdater:
logger.debug("[SQL] %s", sql) logger.debug("[SQL] %s", sql)
c.execute(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 = ( sql = (
"CREATE %(unique)s INDEX CONCURRENTLY %(name)s" "CREATE %(unique)s INDEX CONCURRENTLY %(name)s"
" ON %(table)s" " ON %(table)s"
@ -778,6 +784,12 @@ class BackgroundUpdater:
logger.debug("[SQL] %s", sql) logger.debug("[SQL] %s", sql)
c.execute(sql) c.execute(sql)
finally: 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 conn.set_session(autocommit=False) # type: ignore
def create_index_sqlite(conn: Connection) -> None: def create_index_sqlite(conn: Connection) -> None: