|
95 | 95 | DatabasePool,
|
96 | 96 | LoggingDatabaseConnection,
|
97 | 97 | LoggingTransaction,
|
| 98 | + PostgresEngine, |
98 | 99 | )
|
99 | 100 | from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
|
100 | 101 | from synapse.storage.databases.main.stream import StreamWorkerStore
|
@@ -445,6 +446,14 @@ def _get_unread_counts_by_pos_txn(
|
445 | 446 | (ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
|
446 | 447 | )
|
447 | 448 |
|
| 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 | + |
448 | 457 | # First we pull the counts from the summary table.
|
449 | 458 | #
|
450 | 459 | # We check that `last_receipt_stream_ordering` matches the stream
|
@@ -474,8 +483,8 @@ def _get_unread_counts_by_pos_txn(
|
474 | 483 | ) AS receipts USING (thread_id)
|
475 | 484 | WHERE room_id = ? AND user_id = ?
|
476 | 485 | 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} |
479 | 488 | )
|
480 | 489 | """,
|
481 | 490 | (
|
@@ -516,7 +525,7 @@ def _get_unread_counts_by_pos_txn(
|
516 | 525 | ) AS receipts USING (thread_id)
|
517 | 526 | WHERE user_id = ?
|
518 | 527 | AND room_id = ?
|
519 |
| - AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?) |
| 528 | + AND stream_ordering > {receipt_stream_clause} |
520 | 529 | AND highlight = 1
|
521 | 530 | GROUP BY thread_id
|
522 | 531 | """
|
@@ -601,7 +610,7 @@ def _get_unread_counts_by_pos_txn(
|
601 | 610 | ) AS receipts USING (thread_id)
|
602 | 611 | WHERE user_id = ?
|
603 | 612 | AND room_id = ?
|
604 |
| - AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?) |
| 613 | + AND stream_ordering > {receipt_stream_clause} |
605 | 614 | AND NOT {thread_id_clause}
|
606 | 615 | GROUP BY thread_id
|
607 | 616 | """
|
|
0 commit comments