This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Override global statement timeout when creating indexes in Postgres #16085
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d51db6
disable global statement timeout when creating index
H-Shay 81a36d7
newsfragement
H-Shay 6de8215
fix syntax
H-Shay c18bc5c
use `session` rather than `local` for statement_timeout
H-Shay 64a4228
move return to default to finally block
H-Shay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Override global statement timeout when creating indexes in Postgres. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
||
|
@@ -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" | ||
|
@@ -772,6 +778,11 @@ def create_index_psql(conn: Connection) -> None: | |
logger.debug("[SQL] %s", sql) | ||
c.execute(sql) | ||
|
||
# mypy ignore - `statement_timeout` is defined on PostgresEngine | ||
default_timeout = self.db_pool.engine.statement_timeout # type: ignore[attr-defined] | ||
undo_timeout_sql = f"SET statement_timeout = {default_timeout}" | ||
c.execute(undo_timeout_sql) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be in |
||
|
||
if replaces_index is not None: | ||
# We drop the old index as the new index has now been created. | ||
sql = f"DROP INDEX IF EXISTS {replaces_index}" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to set this back to the default, as we're not in a transaction when creating indices I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the autocommit being on the reason that this isn't in a transaction? The statement timeout only applies for a given transaction - if we are not in a transaction then I wonder if it will have any effect. I will do some digging in the docs but they were pretty scarce on information when I looked into this the first time. Do we have any Postgres/psychopg wizards around who might have some arcane insight into this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
https://www.postgresql.org/docs/current/sql-set.html says that
LOCAL
doesn't work outside a transaction, and so we'd need to useSESSION