pg.ddx.io  pgbouncer/pgbouncer GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
6+ messages / 2 participants
[nested] [flat]

* [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-16 12:46  "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: ritojoao (@ritojoao) @ 2026-11-16 12:46 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

Hi, thanks for pgbouncer. This is my attempt at #1702. Please tell me if
the approach is wrong.

With `host=A,B` and round-robin, `dns_connect()` picks the host with
`pool->rrcounter % count` and advances the counter. The login connection
used to forward a cancel therefore gets the next host, not the host of
the backend being cancelled. The cancel key is then sent to the wrong
server and the query is not cancelled.

The change: in `dns_connect()`, if `db->host` is a list, the pool is not a
peer pool, and the first entry of `waiting_cancel_req_list` has a
`canceled_server->host`, connect to that host and do not touch
`rrcounter`. A side effect is that cancel connections no longer move the
round-robin position.

Tested (2 PostgreSQL 17 servers on 127.0.0.1 and 127.0.0.2, session pool,
`pg_sleep(3)`, 6 iterations per case, own script):
- Before: with `host=127.0.0.1,127.0.0.2`, 3 of 6 cancels failed (the
  query ran to the end). With `load_balance_hosts=disable` and with a
  single host, 6 of 6 were cancelled.
- After: 6 of 6 cancelled in all three cases.
- `make check` in test/ (test_cancel, test_limits,
  test_load_balance_hosts, test_peering): 63 passed. Whole suite: 287
  passed, 8 skipped, 2 failed
  (test_misc.py test_server_check_query_default_negative and
  test_server_check_query_negative, connection timeout through the
  proxy). The same 2 fail on the base commit without my change.

Known limits and not tested:
- If the first queued cancel leaves the queue (client disconnects) before
  the login finishes, the login may serve the next cancel, which can
  belong to another host. I expect this to be rare, but did not test it.
- If the cancel's host is down, the retry uses the same host again. With
  `load_balance_hosts=disable` this skips the `last_connect_failed`
  failover for that cancel. I think this is fine because the key is only
  valid on that host.
- No new pytest test: the test setup starts one backend, and I would need
  two plus a conftest change. I can add that if you want it.
- Peering, a host removed by reload, unix sockets in the list, TLS, IPv6.
- #1217 may be related (same host list, cancel path), but it has no
  repro and I did not test peering, so I do not claim it.

Refs #1702.


inline patch 1: src/objects.c (+17/-1 lines) download inline-1.patchdiff --git a/src/objects.c b/src/objects.c index 666c72d6a013..74d1133b7e58 100644 --- a/src/objects.c +++ b/src/objects.c @@ -1690,9 +1690,25 @@ static void dns_connect(struct PgSocket *server) int sa_len; int res; char *host_copy = NULL; + const char *cancel_host = NULL; + + /* + * A server login that completes while cancel requests are waiting is + * used to forward the first of them. The cancel key is only valid on + * the host of the canceled server, so connect to that host instead of + * the next one from the host list. + */ + if (db->host && strchr(db->host, ',') && !db->peer_id) { + PgSocket *req = first_socket(&server->pool->waiting_cancel_req_list); + + if (req && req->canceled_server && req->canceled_server->host) + cancel_host = req->canceled_server->host; + } /* host list? */ - if (db->host && strchr(db->host, ',')) { + if (cancel_host) { + host = cancel_host; + } else if (db->host && strchr(db->host, ',')) { int count = 1; int n;
^ permalink raw [nested|flat] 6+ messages in thread

* Re: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-17 05:07  "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: cosgroveb (@cosgroveb) @ 2026-11-17 05:07 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

I think our PRs (this and #1703) are largely duplicative but your socket handling is superior. I may see what a best of both PRs combo looks like.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-17 13:05  "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: cosgroveb (@cosgroveb) @ 2026-11-17 13:05 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

Hey @ritojoao I think adding this commit https://github.com/pgbouncer/pgbouncer/pull/1703/changes/cef28024299461b8eff41962ffd1cd1f26b817cd to #1703 represnts the best version of your attempt here and mine. Let me know your thoughts. I've included you in the co-authored-by as well.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-17 13:19  "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: cosgroveb (@cosgroveb) @ 2026-11-17 13:19 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

> Known limits and not tested:
> * If the first queued cancel leaves the queue (client disconnects) before
> the login finishes, the login may serve the next cancel, which can
> belong to another host. I expect this to be rare, but did not test it.

This is correct. #1703 [handles](https://github.com/cosgroveb/pgbouncer/blob/issue-1702-cancel-host-list/src/objects.c#L2306-L2319) [it.](https://github.com/cosgroveb/pgbouncer/blob/issue-1702-cancel-host-list/src/objects.c#L2322-L2337)



^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-17 14:55  "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ritojoao (@ritojoao) @ 2026-11-17 14:55 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

Hi @cosgroveb, thank you very much for the careful look and for the credit. I think combining the two PRs is a good idea, and your #1703 handles the case I only listed as a known limit, which is better than my version.

I would be glad to test the combined commit with my two-server cancel script and the pgbouncer test suite, and I will report the results here and on #1703. If you and the maintainers prefer to continue only in #1703, I am happy to close this PR. Please tell me what is easiest for you.


^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
@ 2026-11-18 19:09  "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ritojoao (@ritojoao) @ 2026-11-18 19:09 UTC (permalink / raw)
  To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>

I posted my test results for the combined version on #1703. Thank you again.


^ permalink  raw  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-11-18 19:09 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 12:46 [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
2026-11-17 05:07 ` "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
2026-11-17 13:05 ` "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
2026-11-17 13:19 ` "cosgroveb (@cosgroveb)" <noreply+cosgroveb@github.com>
2026-11-17 14:55 ` "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>
2026-11-18 19:09 ` "ritojoao (@ritojoao)" <noreply+ritojoao@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox