懒人功能之| PostgreSQL索引自动维护
自驱动数据库功能之一 | PostgreSQL索引自动维护
随着AI Agent的兴起, AI4DB领域之一的self-driving数据库概念也被重新提及.
虽然这么多年过去了, 似乎自驱动数据库只有Oracle做得比较好, 不需要消耗用户太多的管理精力. 还有就是云数据库, 少量人员管理大规模的数据库实例, 也算self-driving数据库范畴吧.
但是self-driving肯定远不止于此. 关于self-driving的讨论可参考:
https://postgres.fm/episodes/self-driving-postgres https://postgres.ai/blog/20250725-self-driving-postgres
本文仅介绍一个有趣的项目 : pg_index_pilot
项目目标概括起来分3类:
AR , 自动reindex膨胀的索引 AIR , 自动发现无用的索引并删除 AIC&O , 自动创建索引以优化数据库SQL性能
通过pg_cron或外部的定时任务, 定期扫描数据库系统表, 发现膨胀超过阈值的索引( https://github.com/ioguix/pgsql-bloat-estimation ), 通过dblink插件异步执行reindex concurrently任务.
项目处于初期阶段, 目前仅实现了部分AR能力, AIR和AIC&O都还没有开始.
详见roadmap:
The Roadmap covers three big areas:
[ ] "AR": Automated Reindexing
[x] RDS and Aurora (see RDS Setup below) [ ] CloudSQL [ ] Supabase [ ] Crunchy Bridge [ ] Azure
[x] original implementation ( pg_index_pilot) – requires initial full reindex[x] non-superuser mode for cloud databases (AWS RDS, Google Cloud SQL, Azure) [x] flexible connection management for dblink [ ] API for stats obtained on a clone (to avoid full reindex on prod primary)
[x] Maxim Boguk's bloat estimation formula – works with any type of index, not only btree [ ] Traditional bloat estimatation (ioguix; btree only) [ ] Exact bloat analysis (pgstattuple; analysis on clones) [x] Tested on managed services [ ] Integration with postgres_ai monitoring [ ] Resource-aware scheduling, predictive maintenance windows (when will load be lowest?) [ ] Coordination with other ops (backups, vacuums, upgrades) [ ] Parallelization and throttling (adaptive) [ ] Predictive bloat modeling [ ] Learning & Feedback Loops: learning from past actions, A/B testing and "what-if" simulation (DBLab) [ ] Impact estimation before scheduling [ ] RCA of fast degraded index health (why it gets bloated fast?) and mitigation (tune autovacuum, avoid xmin horizon getting stuck) [ ] Self-adjusting thresholds
[ ] Unused indexes [ ] Redundant indexes [ ] Invalid indexes (or, per configuration, rebuilding them) [ ] Advanced scoring; suboptimal / rarely used indexes cleanup; self-adjusting thresholds [ ] Forecasting of index usage; seasonal pattern recognition [ ] Impact estimation before removal; "what-if" simulation (DBLab)
[ ] Index recommendations (including multi-column, expression, partial, hybrid, and covering indexes) [ ] Index optimization according to configured goals (latency, size, WAL, write/HOT overhead, read overhead) [ ] Experimentation (hypothetical with HypoPG, real with DBLab) [ ] Query pattern classification [ ] Advanced scoring; cost/benefit analysis [ ] Impact estimation before operations; "what-if" simulation (DBLab)
参考
https://gitlab.com/postgres-ai/pg_index_pilot/-/blob/main/README.md
https://gitlab.com/postgres-ai/pg_index_pilot/-/blob/main/TODO.md
https://gitlab.com/postgres-ai/pg_index_pilot
https://github.com/xataio/agent
https://postgres.fm/episodes/self-driving-postgres
https://postgres.ai/blog/20250725-self-driving-postgres
《这才靠谱数据库MCP Server的样子》
《PostgreSQL 索引推荐 - HypoPG , pg_qualstats》
《解读用户最常问的PostgreSQL垃圾回收、膨胀、多版本管理、存储引擎等疑惑 - 经典》
《PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack》
《PostgreSQL 如何精确计算表膨胀(fsm,数据块layout讲解) - PostgreSQL table exactly bloat monitor use freespace map data》
《PostgreSQL pgstattuple - 检查表的膨胀情况、dead tuples、live tuples、freespace》