diff --git a/packages/overlays/matrix-synapse/patches/0001-Fast-auth-links.patch b/packages/overlays/matrix-synapse/patches/0001-Fast-auth-links.patch
new file mode 100644
index 0000000..0d92e4e
--- /dev/null
+++ b/packages/overlays/matrix-synapse/patches/0001-Fast-auth-links.patch
@@ -0,0 +1,100 @@
+From 1b82f35b613e96c56bf18015e33f34328ad73188 Mon Sep 17 00:00:00 2001
+From: Rory& <root@rory.gay>
+Date: Tue, 22 Jul 2025 05:07:01 +0200
+Subject: [PATCH 01/11] Fast auth links
+
+---
+ synapse/storage/database.py | 43 +++++++++++++++++++
+ .../databases/main/event_federation.py | 8 ++--
+ 2 files changed, 47 insertions(+), 4 deletions(-)
+
+diff --git a/synapse/storage/database.py b/synapse/storage/database.py
+index 6188195614..79af139543 100644
+--- a/synapse/storage/database.py
++++ b/synapse/storage/database.py
+@@ -2558,6 +2558,49 @@ class DatabasePool:
+
+ return txn.fetchall()
+
++# requires database_engine.supports_using_any_list to be true
++def make_select_id_if_found_sql_clause(
++ database_engine: BaseDatabaseEngine,
++ column: str,
++ table: str,
++ iterable: Collection[Any],
++ *,
++ negative: bool = False,
++) -> Tuple[str, list]:
++ """Returns an SQL clause that checks the given column is in the iterable.
++
++ On SQLite this expands to `column IN (?, ?, ...)`, whereas on Postgres
++ it expands to `column = ANY(?)`. While both DBs support the `IN` form,
++ using the `ANY` form on postgres means that it views queries with
++ different length iterables as the same, helping the query stats.
++
++ Args:
++ database_engine
++ column: Name of the column
++ table: Name of the table
++ iterable: The values to check the column against.
++ negative: Whether we should check for inequality, i.e. `NOT IN`
++
++ Returns:
++ A tuple of SQL query and the args
++ """
++ # This should hopefully be faster, but also makes postgres query
++ # stats easier to understand.
++ if database_engine.supports_using_any_list:
++ if not negative:
++ clause = f"{column}_lookup AS {column} FROM UNNEST(?::bigint[]) {column}_lookup WHERE EXISTS(SELECT FROM {table} WHERE {column}={column}_lookup)"
++ else:
++ clause = f"{column}_lookup AS {column} FROM UNNEST(?::bigint[]) {column}_lookup WHERE NOT EXISTS(SELECT FROM {table} WHERE {column}={column}_lookup)"
++
++ return clause, [list(iterable)]
++ else:
++ params = ",".join("?" for _ in iterable)
++ if not negative:
++ clause = f"DISTINCT {column} FROM {table} WHERE {column} IN ({params})"
++ else:
++ clause = f"DISTINCT {column} FROM {table} WHERE {column} NOT IN ({params})"
++ return clause, list(iterable)
++
+
+ def make_in_list_sql_clause(
+ database_engine: BaseDatabaseEngine,
+diff --git a/synapse/storage/databases/main/event_federation.py b/synapse/storage/databases/main/event_federation.py
+index 46aa5902d8..026f0114ed 100644
+--- a/synapse/storage/databases/main/event_federation.py
++++ b/synapse/storage/databases/main/event_federation.py
+@@ -52,6 +52,7 @@ from synapse.storage.database import (
+ DatabasePool,
+ LoggingDatabaseConnection,
+ LoggingTransaction,
++ make_select_id_if_found_sql_clause,
+ )
+ from synapse.storage.databases.main.events_worker import EventsWorkerStore
+ from synapse.storage.databases.main.signatures import SignatureWorkerStore
+@@ -362,8 +363,7 @@ class EventFederationWorkerStore(SignatureWorkerStore, EventsWorkerStore, SQLBas
+ sql = """
+ WITH RECURSIVE links(chain_id) AS (
+ SELECT
+- DISTINCT origin_chain_id
+- FROM event_auth_chain_links WHERE %s
++ %s
+ UNION
+ SELECT
+ target_chain_id
+@@ -380,8 +380,8 @@ class EventFederationWorkerStore(SignatureWorkerStore, EventsWorkerStore, SQLBas
+ while chains_to_fetch:
+ batch2 = tuple(itertools.islice(chains_to_fetch, 1000))
+ chains_to_fetch.difference_update(batch2)
+- clause, args = make_in_list_sql_clause(
+- txn.database_engine, "origin_chain_id", batch2
++ clause, args = make_select_id_if_found_sql_clause(
++ txn.database_engine, "origin_chain_id", "event_auth_chain_links", batch2
+ )
+ txn.execute(sql % (clause,), args)
+
+--
+2.49.0
+
|