diff options
author | reivilibre <oliverw@matrix.org> | 2023-07-03 10:38:57 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-03 11:38:57 +0100 |
commit | 53aa26eddc772a6719bf0da64b0684c333294d05 (patch) | |
tree | f17fd3f057216df8ebaa4e27ba878a21048bc70a /synapse/storage/engines/postgres.py | |
parent | Bump sentry-sdk from 1.25.1 to 1.26.0 (#15867) (diff) | |
download | synapse-53aa26eddc772a6719bf0da64b0684c333294d05.tar.xz |
Add a timeout that aborts any Postgres statement taking more than 1 hour. (#15853)
* Add a timeout to Postgres statements * Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org> --------- Signed-off-by: Olivier Wilkinson (reivilibre) <oliverw@matrix.org>
Diffstat (limited to 'synapse/storage/engines/postgres.py')
-rw-r--r-- | synapse/storage/engines/postgres.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index b350f57ccb..05a72dc554 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -45,6 +45,15 @@ class PostgresEngine( psycopg2.extensions.register_adapter(bytes, _disable_bytes_adapter) self.synchronous_commit: bool = database_config.get("synchronous_commit", True) + # Set the statement timeout to 1 hour by default. + # Any query taking more than 1 hour should probably be considered a bug; + # most of the time this is a sign that work needs to be split up or that + # some degenerate query plan has been created and the client has probably + # timed out/walked off anyway. + # This is in milliseconds. + self.statement_timeout: Optional[int] = database_config.get( + "statement_timeout", 60 * 60 * 1000 + ) self._version: Optional[int] = None # unknown as yet self.isolation_level_map: Mapping[int, int] = { @@ -157,6 +166,10 @@ class PostgresEngine( if not self.synchronous_commit: cursor.execute("SET synchronous_commit TO OFF") + # Abort really long-running statements and turn them into errors. + if self.statement_timeout is not None: + cursor.execute("SET statement_timeout TO ?", (self.statement_timeout,)) + cursor.close() db_conn.commit() |