diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index 407de776b..9a0c9467e 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -792,7 +792,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); } diff --git a/src/backend/distributed/connection/remote_commands.c b/src/backend/distributed/connection/remote_commands.c index c9860c061..80485523d 100644 --- a/src/backend/distributed/connection/remote_commands.c +++ b/src/backend/distributed/connection/remote_commands.c @@ -1178,6 +1178,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) {