r/SQL • • Apr 14 '26

PostgreSQL Anomaly Detection Belongs in Your Database — built SIMD-accelerated isolation forests into Stratum's SQL engine

/r/Clojure/comments/1sktujw/anomaly_detection_belongs_in_your_database_built/
1 Upvotes

2 comments sorted by

2

u/[deleted] Apr 14 '26

[removed] — view removed comment

1

u/flyingfruits Apr 14 '26

Good question. There are a few pieces to this:

Retraining is just SQL: DROP MODEL IF EXISTS fraud_model then CREATE MODEL again with fresh data. Since models are in-memory values with copy-on-write semantics, the old model keeps serving queries until the new one replaces it. No downtime.

Gradual drift is handled by online rotation, you can replace the oldest N trees with trees trained on recent data without rebuilding the whole forest. The Clojure API exposes this today (iforest-rotate), SQL syntax for it is on the roadmap. There's also weighted scoring that gives recent trees higher influence via exponential decay.

Monitoring drift: ANOMALY_CONFIDENCE returns tree agreement (0-1). When confidence drops across the board, it means the trees are disagreeing more, which is a signal that the data distribution has shifted and it's time to retrain. You could schedule a check like:

SELECT AVG(ANOMALY_CONFIDENCE('fraud_model')) FROM transactions
WHERE transaction_date > CURRENT_DATE - 7;

If that number trends down, retrain.

We don't have automatic drift detection that triggers retraining yet. Today it's manual or cron-scheduled. That said, isolation forests are cheap to retrain, 200 trees on 100K rows takes ~15ms on my machine - so aggressive retraining schedules are practical. Some of this is also covered in more detail in the post.

If you have more specific requirements then please lmk, I posted this here primarily to get feedback and make it useful.