diff --git a/src/backend/distributed/connection/connection_management.c b/src/backend/distributed/connection/connection_management.c index 6d1be15e4..ca0a7dfc6 100644 --- a/src/backend/distributed/connection/connection_management.c +++ b/src/backend/distributed/connection/connection_management.c @@ -25,6 +25,7 @@ #include "distributed/hash_helpers.h" #include "distributed/placement_connection.h" #include "distributed/run_from_same_connection.h" +#include "distributed/cancel_utils.h" #include "distributed/remote_commands.h" #include "distributed/version_compat.h" #include "mb/pg_wchar.h" @@ -738,7 +739,7 @@ FinishConnectionListEstablishment(List *multiConnectionList) CHECK_FOR_INTERRUPTS(); - if (InterruptHoldoffCount > 0 && (QueryCancelPending || ProcDiePending)) + if (IsHoldOffCancellationReceived()) { /* * because we can't break from 2 loops easily we need to not forget to diff --git a/src/backend/distributed/connection/remote_commands.c b/src/backend/distributed/connection/remote_commands.c index 2a110bcc7..875c448a4 100644 --- a/src/backend/distributed/connection/remote_commands.c +++ b/src/backend/distributed/connection/remote_commands.c @@ -17,6 +17,7 @@ #include "distributed/errormessage.h" #include "distributed/log_utils.h" #include "distributed/remote_commands.h" +#include "distributed/cancel_utils.h" #include "lib/stringinfo.h" #include "miscadmin.h" #include "storage/latch.h" @@ -757,7 +758,7 @@ FinishConnectionIO(MultiConnection *connection, bool raiseInterrupts) * interrupts held, return instead, and mark the transaction as * failed. */ - if (InterruptHoldoffCount > 0 && (QueryCancelPending || ProcDiePending)) + if (IsHoldOffCancellationReceived()) { connection->remoteTransaction.transactionFailed = true; break; @@ -863,8 +864,7 @@ WaitForAllConnections(List *connectionList, bool raiseInterrupts) CHECK_FOR_INTERRUPTS(); } - if (InterruptHoldoffCount > 0 && (QueryCancelPending || - ProcDiePending)) + if (IsHoldOffCancellationReceived()) { /* * Break out of event loop immediately in case of cancellation. diff --git a/src/backend/distributed/executor/adaptive_executor.c b/src/backend/distributed/executor/adaptive_executor.c index e8df2f31a..fe1bdb9aa 100644 --- a/src/backend/distributed/executor/adaptive_executor.c +++ b/src/backend/distributed/executor/adaptive_executor.c @@ -143,6 +143,7 @@ #include "distributed/placement_access.h" #include "distributed/placement_connection.h" #include "distributed/relation_access_tracking.h" +#include "distributed/cancel_utils.h" #include "distributed/remote_commands.h" #include "distributed/resource_lock.h" #include "distributed/subplan_execution.h" @@ -1810,7 +1811,7 @@ SequentialRunDistributedExecution(DistributedExecution *execution) CHECK_FOR_INTERRUPTS(); - if (InterruptHoldoffCount > 0 && (QueryCancelPending || ProcDiePending)) + if (IsHoldOffCancellationReceived()) { break; } @@ -1918,8 +1919,7 @@ RunDistributedExecution(DistributedExecution *execution) CHECK_FOR_INTERRUPTS(); } - if (InterruptHoldoffCount > 0 && (QueryCancelPending || - ProcDiePending)) + if (IsHoldOffCancellationReceived()) { /* * Break out of event loop immediately in case of cancellation. diff --git a/src/backend/distributed/utils/cancel_utils.c b/src/backend/distributed/utils/cancel_utils.c new file mode 100644 index 000000000..17383c034 --- /dev/null +++ b/src/backend/distributed/utils/cancel_utils.c @@ -0,0 +1,25 @@ +/* + * cancel_utils.c + * + * Utilities related to query cancellation + * + * Copyright (c) Citus Data, Inc. + */ + + +#include "postgres.h" +#include "miscadmin.h" +#include "distributed/cancel_utils.h" + + +/* + * IsHoldOffCancellationReceived returns true if a cancel signal + * was sent and HOLD_INTERRUPTS was called prior to this. The motivation + * here is that since our queries can take a long time, in some places + * we do not want to wait even if HOLD_INTERRUPTS was called. + */ +bool +IsHoldOffCancellationReceived() +{ + return InterruptHoldoffCount > 0 && (QueryCancelPending || ProcDiePending); +} diff --git a/src/include/distributed/cancel_utils.h b/src/include/distributed/cancel_utils.h new file mode 100644 index 000000000..5ff6ffc39 --- /dev/null +++ b/src/include/distributed/cancel_utils.h @@ -0,0 +1,17 @@ +/*------------------------------------------------------------------------- + * + * cencel_utils.h + * Utilities related to query cancellation + * + * Copyright (c) Citus Data, Inc. + * + *------------------------------------------------------------------------- + */ + +#ifndef CANCEL_UTILS_H +#define CANCEL_UTILS_H + + +extern bool IsHoldOffCancellationReceived(void); + +#endif /* CANCEL_UTILS_H */