Does YSQL statement_timeout stop all tablet work in a distributed query?
0 reputation · 16 Jul 2023, 19:56 UTC
0 reputation · 16 Jul 2023, 19:56 UTC
YSQL exposes PostgreSQL-compatible statement_timeout and pg_cancel_backend, but their cancellation contract is unclear when a query spans multiple tablet servers. Assuming a YugabyteDB 2.x deployment, the goal is to determine whether a timeout guarantees that every constituent operation stops, or whether some tablet work can continue after the client receives a cancellation error.
The distinction matters because statement_timeout is evaluated at the tablet-server layer after request routing, while a distributed join may involve several execution branches. It is also unclear whether the timeout applies consistently to background scans or child operations not covered by the session setting. The separate default_transaction_timeout boundary should not be assumed to provide identical cancellation semantics.
This behavior must be validated against the deployed YugabyteDB version and a multi-tablet query before being treated as a cancellation guarantee. Can a YSQL timeout leave tablet-server work running after the client is notified? Does pg_stat_activity provide sufficient evidence that cancellation completed across all participating tablets?
In YugabyteDB 2.x, statement_timeout is evaluated independently on each tablet server after a query is routed. This means a timeout does not automatically cancel in-flight work on every tablet involved in a distributed query. If a query spans multiple tablets—such as a distributed join or parallel scan—one branch may stop when the timeout triggers, while other branches continue executing on surviving tablets. The cancellation signal travels per-session, but the tablet-server layer processes it based on its local execution state.
Consequently, pg_stat_activity can show a session state of cancelled on the client side, but this does not per-se confirm that all participating tablets have terminated their local work. Asymmetric branch completion is common: a hash‑join build side on one tablet may stop, while the probe side on another tablet keeps running until its own timeout or natural completion.
Version matters. Behavior changes were introduced in YugabyteDB 2.18+ regarding distributed query cancellation. In earlier 2.x minor releases, the per-tablet timeout semantics were less deterministic, and background scans or non‑parallelizable components may not respect the session‑level statement_timeout at all.
To verify whether a timeout leaves tablet-server work running, you can:
statement_timeout='30s' set at the session level.pg_stat_activity for the session state and note the state change.cancelled or timeout entries across all tablet identifiers involved in the query plan.This two‑step check—client‑side state plus server‑side log evidence—is the minimal verification needed before treating statement_timeout as a full cancellation guarantee.
If your deployment includes background scans, temp‑table builds, or outer‑loop joins that are not fully parallelized, assume statement_timeout may not stop all tablet work. To determine whether the timeout guarantee applies to your specific plan, please confirm the YugabyteDB minor version and whether the query uses parallel branches or background scans.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 16 Jul 2023, 21:39 UTC
In YugabyteDB 2.x, statement_timeout is evaluated independently on each tablet server after request routing. For a distributed query involving multiple tablets, this means the timeout does not automatically cancel in-flight work on all tablets simultaneously; some branches may complete while others are interrupted. The coordinator's pg_stat_activity reflects the overall query state but does not expose per-tablet cancellation status, so error absence on the coordinator does not confirm that all tablet work has stopped. Additionally, default_transaction_timeout enforces at the transaction boundary and may differ from statement_timeout when operations cross tablet server boundaries.