From 80a989352e13aff151663e54efaf45e91bb401eb Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 15 Sep 2022 08:47:08 -0400 Subject: [PATCH] Support PostgreSQL. --- .../databases/main/event_push_actions.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py index 660ebd81259c..16d1558925a4 100644 --- a/synapse/storage/databases/main/event_push_actions.py +++ b/synapse/storage/databases/main/event_push_actions.py @@ -95,6 +95,7 @@ DatabasePool, LoggingDatabaseConnection, LoggingTransaction, + PostgresEngine, ) from synapse.storage.databases.main.receipts import ReceiptsWorkerStore from synapse.storage.databases.main.stream import StreamWorkerStore @@ -445,6 +446,14 @@ def _get_unread_counts_by_pos_txn( (ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE), ) + # PostgreSQL and SQLite differ in comparing scalar numerics. + if isinstance(self.database_engine, PostgresEngine): + # GREATEST ignores NULLs. + receipt_stream_clause = "GREATEST(receipt_stream_ordering, ?)" + else: + # MAX returns NULL if any are NULL, so COALESCE to 0 first. + receipt_stream_clause = "MAX(COALESCE(receipt_stream_ordering, 0), ?)" + # First we pull the counts from the summary table. # # We check that `last_receipt_stream_ordering` matches the stream @@ -474,8 +483,8 @@ def _get_unread_counts_by_pos_txn( ) AS receipts USING (thread_id) WHERE room_id = ? AND user_id = ? AND ( - (last_receipt_stream_ordering IS NULL AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?)) - OR last_receipt_stream_ordering = MAX(COALESCE(receipt_stream_ordering, 0), ?) + (last_receipt_stream_ordering IS NULL AND stream_ordering > {receipt_stream_clause}) + OR last_receipt_stream_ordering = {receipt_stream_clause} ) """, ( @@ -516,7 +525,7 @@ def _get_unread_counts_by_pos_txn( ) AS receipts USING (thread_id) WHERE user_id = ? AND room_id = ? - AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?) + AND stream_ordering > {receipt_stream_clause} AND highlight = 1 GROUP BY thread_id """ @@ -601,7 +610,7 @@ def _get_unread_counts_by_pos_txn( ) AS receipts USING (thread_id) WHERE user_id = ? AND room_id = ? - AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?) + AND stream_ordering > {receipt_stream_clause} AND NOT {thread_id_clause} GROUP BY thread_id """