diff --git a/src/objects.c b/src/objects.c
index 666c72d6a013..a39253342bea 100644
--- a/src/objects.c
+++ b/src/objects.c
@@ -1780,6 +1780,26 @@ static void dns_connect(struct PgSocket *server)
 	free(host_copy);
 }
 
+static void connect_cancel_server(PgSocket *server, const PgSocket *canceled_server)
+{
+	const PgAddr *addr = &canceled_server->remote_addr;
+	int salen;
+
+	if (pga_family(addr) == AF_INET) {
+		salen = sizeof(addr->sin);
+	} else if (pga_family(addr) == AF_INET6) {
+		salen = sizeof(addr->sin6);
+	} else {
+		dns_connect(server);
+		return;
+	}
+
+	if (canceled_server->host)
+		server->host = xstrdup(canceled_server->host);
+
+	connect_server(server, &addr->sa, salen);
+}
+
 PgSocket *compare_connections_by_time(PgSocket *lhs, PgSocket *rhs)
 {
 	if (!lhs)
@@ -2005,6 +2025,16 @@ void launch_new_connection(PgPool *pool, bool evict_if_needed)
 	if (pool->user_credentials)
 		pool->user_credentials->global_user->connection_count++;
 
+	if (!pool->db->peer_id && !statlist_empty(&pool->waiting_cancel_req_list)) {
+		PgSocket *req = first_socket(&pool->waiting_cancel_req_list);
+
+		if (req->canceled_server) {
+			/* A cancel key is valid only on the server that supplied it. */
+			connect_cancel_server(server, req->canceled_server);
+			return;
+		}
+	}
+
 	dns_connect(server);
 }
 
diff --git a/test/test.ini b/test/test.ini
index 5899174d5ade..982a2d32ef66 100644
--- a/test/test.ini
+++ b/test/test.ini
@@ -40,6 +40,7 @@ authdb = port=6666 host=127.0.0.1 dbname=p1 auth_user=pswcheck
 
 hostlist1 = port=6666 host=127.0.0.1,::1 dbname=p0 user=bouncer
 hostlist2 = port=6666 host=127.0.0.1,127.0.0.1 dbname=p0 user=bouncer
+cancel_hostlist = port=6666 host=127.0.0.1,127.0.0.2 dbname=p0 user=bouncer pool_mode=session
 hostlist_good_first = port=6666 host=127.0.0.1,127.0.0.3 dbname=p0 user=bouncer load_balance_hosts=disable
 hostlist_bad_first = port=6666 host=unresolvable-hostname,127.0.0.1 dbname=p0 user=bouncer load_balance_hosts=disable
 load_balance_hosts_update = port=6666 host=127.0.0.1,127.0.0.3 dbname=p0 user=bouncer load_balance_hosts=disable
diff --git a/test/test_cancel.py b/test/test_cancel.py
index 16014007b4b4..090f8b4d7f4e 100644
--- a/test/test_cancel.py
+++ b/test/test_cancel.py
@@ -4,6 +4,27 @@
 import psycopg
 import pytest
 
+from .utils import USE_UNIX_SOCKETS, Postgres, wait_until
+
+
+@pytest.fixture
+def second_postgres(pg, tmp_path):
+    other = Postgres(tmp_path / "second_pgdata")
+    other.initdb()
+    other.host = "127.0.0.2"
+    other.port = pg.port
+    with other.conf_path.open("a") as pgconf:
+        pgconf.write("listen_addresses = '127.0.0.2'\n")
+        if USE_UNIX_SOCKETS:
+            pgconf.write("unix_socket_directories = ''\n")
+    other.nossl_access("all", "trust")
+    other.commit_hba()
+    other.start()
+    other.sql("CREATE DATABASE p0")
+    other.sql("CREATE USER bouncer")
+    yield other
+    other.cleanup()
+
 
 def test_cancel(bouncer):
     with bouncer.cur(dbname="p3") as cur, ThreadPoolExecutor(max_workers=2) as pool:
@@ -17,6 +38,27 @@ def test_cancel(bouncer):
             query.result()
 
 
+def test_cancel_uses_query_backend_host(bouncer, pg, second_postgres):
+    with (
+        bouncer.cur(dbname="cancel_hostlist") as cur,
+        ThreadPoolExecutor(max_workers=2) as pool,
+    ):
+        query = pool.submit(cur.execute, "SELECT pg_sleep(2)")
+
+        for _ in wait_until("query did not reach the first host"):
+            sleeping = pg.sql_value("""
+                SELECT count(*) FROM pg_stat_activity
+                WHERE usename = 'bouncer' AND datname = 'p0'
+                  AND state = 'active' AND wait_event = 'PgSleep'
+            """)
+            if sleeping == 1:
+                break
+
+        pool.submit(cur.connection.cancel).result()
+        with pytest.raises(psycopg.errors.QueryCanceled, match="due to user request"):
+            query.result()
+
+
 # Test for waiting connections handling for cancel requests.
 #
 # The bug fixed by GH PR #542 was: When the connection pool is full,
