|
23 | 23 |
|
24 | 24 | from synapse.config.homeserver import HomeServerConfig
|
25 | 25 | from synapse.storage.database import LoggingDatabaseConnection
|
26 |
| -from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine |
| 26 | +from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine, Sqlite3Engine |
27 | 27 | from synapse.storage.schema import SCHEMA_COMPAT_VERSION, SCHEMA_VERSION
|
28 | 28 | from synapse.storage.types import Cursor
|
29 | 29 |
|
@@ -108,9 +108,14 @@ def prepare_database(
|
108 | 108 | # so we start one before running anything. This ensures that any upgrades
|
109 | 109 | # are either applied completely, or not at all.
|
110 | 110 | #
|
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") |
114 | 119 |
|
115 | 120 | logger.info("%r: Checking existing schema version", databases)
|
116 | 121 | version_info = _get_or_create_schema_state(cur, database_engine)
|
|
0 commit comments