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

Commit 2ed39f6

Browse files
committed
Support PostgreSQL.
1 parent 3c52b6a commit 2ed39f6

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

synapse/storage/databases/main/event_push_actions.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
DatabasePool,
9696
LoggingDatabaseConnection,
9797
LoggingTransaction,
98+
PostgresEngine,
9899
)
99100
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
100101
from synapse.storage.databases.main.stream import StreamWorkerStore
@@ -445,6 +446,14 @@ def _get_unread_counts_by_pos_txn(
445446
(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
446447
)
447448

449+
# PostgreSQL and SQLite differ in comparing scalar numerics.
450+
if isinstance(self.database_engine, PostgresEngine):
451+
# GREATEST ignores NULLs.
452+
receipt_stream_clause = "GREATEST(receipt_stream_ordering, ?)"
453+
else:
454+
# MAX returns NULL if any are NULL, so COALESCE to 0 first.
455+
receipt_stream_clause = "MAX(COALESCE(receipt_stream_ordering, 0), ?)"
456+
448457
# First we pull the counts from the summary table.
449458
#
450459
# We check that `last_receipt_stream_ordering` matches the stream
@@ -474,8 +483,8 @@ def _get_unread_counts_by_pos_txn(
474483
) AS receipts USING (thread_id)
475484
WHERE room_id = ? AND user_id = ?
476485
AND (
477-
(last_receipt_stream_ordering IS NULL AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?))
478-
OR last_receipt_stream_ordering = MAX(COALESCE(receipt_stream_ordering, 0), ?)
486+
(last_receipt_stream_ordering IS NULL AND stream_ordering > {receipt_stream_clause})
487+
OR last_receipt_stream_ordering = {receipt_stream_clause}
479488
)
480489
""",
481490
(
@@ -516,7 +525,7 @@ def _get_unread_counts_by_pos_txn(
516525
) AS receipts USING (thread_id)
517526
WHERE user_id = ?
518527
AND room_id = ?
519-
AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?)
528+
AND stream_ordering > {receipt_stream_clause}
520529
AND highlight = 1
521530
GROUP BY thread_id
522531
"""
@@ -601,7 +610,7 @@ def _get_unread_counts_by_pos_txn(
601610
) AS receipts USING (thread_id)
602611
WHERE user_id = ?
603612
AND room_id = ?
604-
AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?)
613+
AND stream_ordering > {receipt_stream_clause}
605614
AND NOT {thread_id_clause}
606615
GROUP BY thread_id
607616
"""

0 commit comments

Comments
 (0)