ForgeOpsTracker.QueryPlanner (forge_ops_tracker v0.13.0)

Copy Markdown View Source

Opt-in EXPLAIN for slow PostgreSQL queries (explain_slow_queries, off by default).

consider/5 runs in the process that ran the query, right after it finished, and only decides: enabled, PostgreSQL, at or over explain_threshold_ms, and a plain SELECT (ForgeOpsTracker.QueryPlans.explainable?/1). A query that passes is cast to this supervised GenServer along with the function that will EXPLAIN it, so nothing more happens on the caller's path. Here it's rate limited (each distinct masked statement once per 10 minutes, 10 per minute overall, for the whole VM), and then run in its own Task, one at a time.

That Task is started by this GenServer, not by the process that ran the query, so it never inherits that process's connection or transaction (not even through $callers): the EXPLAIN function checks out its own connection from the repo's pool. It runs EXPLAIN (FORMAT JSON), never EXPLAIN ANALYZE, with the original statement and params inside a READ ONLY transaction with a 2 second statement_timeout, and rolls back (see ForgeOpsTracker.Integrations.Ecto.explain_with_repo/3). Only the masked statement and the masked plan are sent, through ForgeOpsTracker.DeliveryQueue. Any failure is logged and dropped; nothing here ever raises into the app.

Summary

Functions

Returns a specification to start this module under a supervisor.

Offers one finished query for an EXPLAIN. explain is a zero-arity function that runs EXPLAIN (FORMAT JSON) on a separate connection and returns the plan; it only ever runs in this planner's own Task. masked is the statement as already masked for the span, when there was one. Returns whether it was handed to the planner. Never raises.

True in the process running an EXPLAIN, so the Ecto handler can ignore the EXPLAIN's own statements.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

consider(statement, db_system, duration_ms, explain, masked \\ nil)

@spec consider(String.t(), String.t() | nil, number(), (-> term()), String.t() | nil) ::
  boolean()

Offers one finished query for an EXPLAIN. explain is a zero-arity function that runs EXPLAIN (FORMAT JSON) on a separate connection and returns the plan; it only ever runs in this planner's own Task. masked is the statement as already masked for the span, when there was one. Returns whether it was handed to the planner. Never raises.

explaining?()

@spec explaining?() :: boolean()

True in the process running an EXPLAIN, so the Ecto handler can ignore the EXPLAIN's own statements.