From 8aca59fa32b33c424c0375fa64cb48d2d50dab96 Mon Sep 17 00:00:00 2001 From: Nils Dijk Date: Fri, 23 Oct 2020 15:35:43 +0200 Subject: [PATCH 1/2] don't send unnecessary cancels --- src/backend/distributed/connection/remote_commands.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/backend/distributed/connection/remote_commands.c b/src/backend/distributed/connection/remote_commands.c index da484a13b..6e3fa3081 100644 --- a/src/backend/distributed/connection/remote_commands.c +++ b/src/backend/distributed/connection/remote_commands.c @@ -1042,6 +1042,12 @@ SendCancelationRequest(MultiConnection *connection) { char errorBuffer[ERROR_BUFFER_SIZE] = { 0 }; + if (!PQisBusy(connection->pgConn)) + { + /* no statement in progress, nothing to cancel */ + return false; + } + PGcancel *cancelObject = PQgetCancel(connection->pgConn); if (cancelObject == NULL) { From e06cf9a8223d42bfe83a7da0d700f3977da79f07 Mon Sep 17 00:00:00 2001 From: Nils Dijk Date: Fri, 23 Oct 2020 15:57:27 +0200 Subject: [PATCH 2/2] on shutdown, consume any response before sending shutdown packet --- .../distributed/connection/connection_management.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index 2e6bce06b..1354f8ebd 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -616,7 +616,17 @@ ShutdownConnection(MultiConnection *connection) if (PQstatus(connection->pgConn) == CONNECTION_OK && PQtransactionStatus(connection->pgConn) == PQTRANS_ACTIVE) { - SendCancelationRequest(connection); + bool sentCancel = SendCancelationRequest(connection); + if (sentCancel) + { + /* + * If we have sent a cancelation we need to wait and consume the response to + * make sure the cancelation is processed. In case of network delay + * cancelation might hit other backend/query. Poolers might introduce out of + * order delivery. + */ + ClearResultsDiscardWarnings(connection, false); + } } CitusPQFinish(connection); }