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

Commit c1d2ce2

Browse files
Do not always start a db txn on Postgres (#14840)
1 parent 218a383 commit c1d2ce2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

changelog.d/14840.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent "WARNING: there is already a transaction in progress" lines appearing in PostgreSQL's logs on some occasions.

synapse/storage/prepare_database.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from synapse.config.homeserver import HomeServerConfig
2525
from synapse.storage.database import LoggingDatabaseConnection
26-
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine
26+
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine
2727
from synapse.storage.schema import SCHEMA_COMPAT_VERSION, SCHEMA_VERSION
2828
from synapse.storage.types import Cursor
2929

@@ -108,9 +108,14 @@ def prepare_database(
108108
# so we start one before running anything. This ensures that any upgrades
109109
# are either applied completely, or not at all.
110110
#
111-
# (psycopg2 automatically starts a transaction as soon as we run any statements
112-
# at all, so this is redundant but harmless there.)
113-
cur.execute("BEGIN TRANSACTION")
111+
# psycopg2 does not automatically start transactions when in autocommit mode.
112+
# While it is technically harmless to nest transactions in postgres, doing so
113+
# results in a warning in Postgres' logs per query. And we'd rather like to
114+
# avoid doing that.
115+
if isinstance(database_engine, Sqlite3Engine) or (
116+
isinstance(database_engine, PostgresEngine) and db_conn.autocommit
117+
):
118+
cur.execute("BEGIN TRANSACTION")
114119

115120
logger.info("%r: Checking existing schema version", databases)
116121
version_info = _get_or_create_schema_state(cur, database_engine)

0 commit comments

Comments
 (0)