Audit MySQL Long-Running Uncommitted Transactions
Proactively detect long-running uncommitted MySQL transactions and row lock contention before thread pool exhaustion occurs.
Find pgvector columns with no HNSW or IVFFlat index, build an HNSW index with the operator class matching your distance operator, then verify pg_index.
A vector or halfvec column with no approximate nearest neighbour index makes every similarity query a sequential scan with an exact distance computed for each row. On a few thousand development rows that is milliseconds. At a few million it is seconds per query. Nothing in PostgreSQL warns you. There is no pg_stat counter for it and no log line. The query keeps returning the right answer, slower every week.
The part that catches people twice is the operator class. An HNSW index is built for one distance function. vector_cosine_ops serves <=>, vector_l2_ops serves <->, vector_ip_ops serves <#>, with halfvec_cosine_ops, halfvec_l2_ops and halfvec_ip_ops for half precision columns. Build the index for the wrong one and the planner simply never uses it, so you pay for the build, pay the memory and still get the sequential scan. This guard takes the distance your queries use as an input and derives the operator class from it and from the column type.
This blueprint was created by zkasuran.
dry_run is true and auto_remediate is false, so a plain run reports and never builds an index.auto_remediate is true and dry_run is false. It then waits for a human to resume the Pause with approved set true. An unanswered gate fails the run at PT2H.CREATE INDEX USING hnsw reads every row, builds a graph bounded by maintenance_work_mem and holds a lock that blocks writes on the table until it finishes. On a large table that is minutes to hours, so the window is a human decision, not a schedule's.IF NOT EXISTS makes a repeat run a no-op.flag_opclass_mismatch on, a column whose index is built for a different distance is reported and gains a second index for the right one. The existing index is never dropped.min_table_rows keeps small tables out. A sequential scan over a few hundred rows beats a graph that has to be maintained.pg_index is the real gate. It counts only indisvalid indexes, so a build that failed part way and left an invalid index fails the run instead of reporting success.detect (io.kestra.plugin.jdbc.postgresql.Query, read-only) joins pg_attribute to pg_type to find every vector and halfvec column on a table, materialized view or partitioned table outside the system schemas, then looks in pg_index for a valid hnsw or ivfflat index whose first key column is that column. It derives the wanted operator class from distance_metric and the column type, then returns one row per column with no matching index, with the estimated row count, the distance operator, the indexes that do exist and the exact CREATE INDEX ... USING hnsw statement built with format('%I').summarize (io.kestra.plugin.core.output.OutputValues) builds the readable target list and the JSON the loop iterates, so the log, the alert and the loop read the same set.log_detect (io.kestra.plugin.core.log.Log) logs the count, the targets and the mode.guard (io.kestra.plugin.core.flow.If) branches on the count. With none found, clean logs and the run ends. Otherwise findings_alert posts the finding on every detection.mode (io.kestra.plugin.core.flow.If) enters the remediation branch only when auto_remediate is true and dry_run is false. Otherwise report_only lists what would be built and stops.approval_gate (io.kestra.plugin.core.flow.Pause) holds the run for a human. remediate_indexes (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) then runs one CREATE INDEX USING hnsw per target, in turn, so two builds never compete for maintenance_work_mem.reverify re-runs the detection and verify_result fails the run through io.kestra.plugin.core.execution.Fail if any targeted column still has no valid HNSW index. The message names any invalid index left behind.vector and halfvec column that turns a similarity query into a sequential scan, largest table first, with the indexes it does have.halfvec_*_ops rather than the vector_*_ops that would be rejected.\d and is never used.pg_index shows a valid index on every target.The catalog query answers it once. Kestra runs it every week and after every backfill, so a column that crossed the threshold overnight is caught before the latency shows up in a dashboard. The build sits behind auto_remediate, dry_run and a Pause, which is what a write locking index build needs. One task per build keeps a failure isolated, the re-read of pg_index decides whether the run passed. The execution history records which index was built on which column and when.
On Kestra 2.0.5 OSS against pgvector/pgvector:pg16 (pgvector 0.8.2), with a local HTTP sink standing in for Slack. Seeded in one database:
docs.embedding vector(3), 20,000 rows, no index;docs.title_embedding vector(3), an existing hnsw index with vector_cosine_ops;docs.body_embedding vector(3), an existing ivfflat index with vector_l2_ops;notes.summary halfvec(3), 5,000 rows, no index;plain_items, no vector column.| Run | Inputs | Result |
|---|---|---|
| 1 | defaults, healthy state | 0 findings, clean logged |
| 2 | defaults, degraded state | 2 findings, docs.embedding needing vector_cosine_ops and notes.summary needing halfvec_cosine_ops. The two indexed columns and plain_items are absent. Alert sent |
| 3 | auto_remediate: true, dry_run: true |
Same 2 findings, nothing built, report_only |
| 4 | auto_remediate: true, dry_run: false, approved |
2 HNSW indexes built and verified, both indisvalid in pg_index, re-read reports 0 |
| 5 | auto_remediate: true, dry_run: false, resumed without approval |
nothing built, not_approved |
| 6 | flag_opclass_mismatch: true |
docs.body_embedding reported, its ivfflat vector_l2_ops index cannot serve <=>. docs.title_embedding stays clean |
| 7 | unreachable database | run failed and the errors block posted to Slack |
The plan is the evidence. EXPLAIN SELECT id FROM docs ORDER BY embedding <=> '[0.1,0.2,0.3]' LIMIT 5 was a Seq Scan on docs under a sort before the run. It became an Index Scan using docs_embedding_hnsw_cosine_idx on docs after it.
CREATE EXTENSION vector). The detection reads pg_attribute, pg_type, pg_index, pg_opclass and pg_am, which any role can read.CREATE INDEX does.maintenance_work_mem for the session or the database before a large build. pgvector builds the graph in memory and falls back to a much slower on disk build when it does not fit.docker run -d --name pgvec -p 5432:5432 -e POSTGRES_PASSWORD=pg pgvector/pgvector:pg16;CREATE EXTENSION vector, then a table with a vector column, some rows and no index.POSTGRES_USERNAME and POSTGRES_PASSWORD: credentials used by every query.SLACK_WEBHOOK_URL: used by findings_alert and the errors block.SECRET_.| Input | Default | Purpose |
|---|---|---|
postgres_url |
jdbc:postgresql://localhost:5432/postgres |
Database to check |
use_secret_auth |
true |
Read credentials from secrets. Set false only in a disposable test |
postgres_username |
postgres |
Inline username when use_secret_auth is false |
postgres_password |
empty | Inline password when use_secret_auth is false |
only_schema |
empty | Restrict to one schema |
distance_metric |
cosine |
The distance the queries use, which fixes the operator class |
min_table_rows |
1000 |
Smallest table that earns an index |
flag_opclass_mismatch |
false |
Also report an index built for another distance |
dry_run |
true |
Must be false for an index to be built |
auto_remediate |
false |
Must be true for an index to be built |
POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL secrets.postgres_url.distance_metric to the operator your searches use. Grep the application for <=>, <-> or <#> if you are not sure.flag_opclass_mismatch: true to see whether an index you already have can serve that operator at all.auto_remediate: true and dry_run: false in a maintenance window, raise maintenance_work_mem first, then resume the Pause with approved set true.weekly_vector_index_check trigger, which always runs in safe mode.CREATE INDEX CONCURRENTLY when you cannot take a write lock. It is slower and leaves an invalid index behind on failure, which the reverify step already catches.WITH (m = 16, ef_construction = 64) in fix_sql for recall against build time.maintenance_work_mem per database with ALTER DATABASE ... SET maintenance_work_mem so every build gets the memory it needs.ivfflat instead for a table that is loaded once and rarely updated, where a smaller index matters more than recall on inserts.distance_metric if parts of the application use different operators on the same column.