Skip to content

Commit a0e94f7

Browse files
committed
Add RETURNING alternative for old versions of SQLite
This was the original reason the PR was reverted, see #18346 > The `RETURNING` syntax has been supported by SQLite since version 3.35.0 (2021-03-12). > > *-- https://www.sqlite.org/lang_returning.html* Synapse supports... > The oldest supported version of SQLite is the version [provided](https://packages.debian.org/bullseye/libsqlite3-0) by [Debian oldstable](https://wiki.debian.org/DebianOldStable). > > *-- https://element-hq.github.io/synapse/latest/deprecation_policy.html* which currently is https://packages.debian.org/bullseye/sqlite3 -> `3.34.1-3+deb11u1` We have `self.db_pool.engine.supports_returning` to detect whether we can use `RETURNING`.
1 parent eb89758 commit a0e94f7

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

synapse/storage/databases/main/events_bg_updates.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,8 @@ async def _event_stats_populate_counts_bg_update(
25762576
querying that entire index.
25772577
"""
25782578
# The last event `stream_ordering` we processed (starting place of this next
2579-
# batch).
2579+
# batch). Since we're processing things in `stream_ordering` ascending order,
2580+
# this should be the maximum `stream_ordering` we processed.
25802581
last_event_stream_ordering = progress.get(
25812582
"last_event_stream_ordering", -(1 << 31)
25822583
)
@@ -2770,8 +2771,7 @@ def _populate_txn(
27702771
"""
27712772

27722773
# Increment the counts based on the events present in this batch.
2773-
txn.execute(
2774-
"""
2774+
update_event_stats_sql = """
27752775
WITH event_batch AS (
27762776
SELECT *
27772777
FROM events
@@ -2798,12 +2798,37 @@ def _populate_txn(
27982798
total_event_count = total_event_count + (SELECT total_event_count FROM batch_stats),
27992799
unencrypted_message_count = unencrypted_message_count + (SELECT unencrypted_message_count FROM batch_stats),
28002800
e2ee_event_count = e2ee_event_count + (SELECT e2ee_event_count FROM batch_stats)
2801-
RETURNING
2802-
(SELECT total_event_count FROM batch_stats) AS total_event_count,
2803-
(SELECT max_stream_ordering FROM batch_stats) AS max_stream_ordering
2804-
""",
2805-
(last_event_stream_ordering, stop_event_stream_ordering, batch_size),
2806-
)
2801+
"""
2802+
if self.db_pool.engine.supports_returning:
2803+
txn.execute(
2804+
f"""
2805+
{update_event_stats_sql}
2806+
RETURNING
2807+
(SELECT total_event_count FROM batch_stats) AS total_event_count,
2808+
(SELECT max_stream_ordering FROM batch_stats) AS max_stream_ordering
2809+
""",
2810+
(
2811+
last_event_stream_ordering,
2812+
stop_event_stream_ordering,
2813+
batch_size,
2814+
),
2815+
)
2816+
else:
2817+
txn.execute(update_event_stats_sql)
2818+
txn.execute(
2819+
"""
2820+
SELECT COUNT(*), MAX(stream_ordering)
2821+
FROM events
2822+
WHERE stream_ordering > ? AND stream_ordering <= ?
2823+
ORDER BY stream_ordering ASC
2824+
LIMIT ?
2825+
""",
2826+
(
2827+
last_event_stream_ordering,
2828+
stop_event_stream_ordering,
2829+
batch_size,
2830+
),
2831+
)
28072832

28082833
# Get the results of the update
28092834
(total_event_count, max_stream_ordering) = cast(

0 commit comments

Comments
 (0)