hypopg
假设索引,用于创建一个虚拟索引检验执行计划
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
hypopg | 1.4.2 | FEAT | PostgreSQL | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 2790 | hypopg | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 相关扩展 | index_advisor pg_qualstats powa pg_hint_plan auto_explain pg_stat_statements btree_gin pg_show_plans |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PGDG | 1.4.2 | 1817161514 | hypopg | - |
| RPM | PGDG | 1.4.2 | 1817161514 | hypopg_$v | - |
| DEB | PGDG | 1.4.2 | 1817161514 | postgresql-$v-hypopg | - |
安装
您可以直接安装 hypopg 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:
pig repo add pgdg -u # 添加 PGDG 仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install hypopg; # 当前活跃 PG 版本安装
pig ext install -y hypopg -v 18 # PG 18
pig ext install -y hypopg -v 17 # PG 17
pig ext install -y hypopg -v 16 # PG 16
pig ext install -y hypopg -v 15 # PG 15
pig ext install -y hypopg -v 14 # PG 14
dnf install -y hypopg_18 # PG 18
dnf install -y hypopg_17 # PG 17
dnf install -y hypopg_16 # PG 16
dnf install -y hypopg_15 # PG 15
dnf install -y hypopg_14 # PG 14
apt install -y postgresql-18-hypopg # PG 18
apt install -y postgresql-17-hypopg # PG 17
apt install -y postgresql-16-hypopg # PG 16
apt install -y postgresql-15-hypopg # PG 15
apt install -y postgresql-14-hypopg # PG 14
创建扩展:
CREATE EXTENSION hypopg;
用法
HypoPG 允许创建仅存在于当前会话中的假想(虚拟)索引,这些索引可被 EXPLAIN(不带 ANALYZE)在查询规划时考虑。这使得无需实际创建索引即可测试索引对查询的影响。
函数
| 函数 | 描述 |
|---|---|
hypopg_create_index(query text) | 使用 CREATE INDEX 语法创建假想索引 |
hypopg_list_indexes() | 列出当前会话中的所有假想索引 |
hypopg_drop_index(oid) | 通过 OID 删除特定假想索引 |
hypopg_reset() | 删除所有假想索引 |
hypopg() | 以类似 pg_index 的格式返回假想索引 |
工作流程
创建测试表并查看基线执行计划:
CREATE TABLE hypo AS SELECT id, 'line ' || id AS val FROM generate_series(1, 10000) id;
ANALYZE hypo;
EXPLAIN SELECT * FROM hypo WHERE id = 1;
-- Seq Scan on hypo (cost=0.00..170.00 rows=1 width=15)
创建假想索引:
SELECT * FROM hypopg_create_index('CREATE INDEX ON hypo (id)');
-- indexrelid | indexname
-- ------------+----------------------
-- 13543 | <13543>btree_hypo_id
查看使用假想索引后的执行计划:
EXPLAIN SELECT * FROM hypo WHERE id = 1;
-- Index Scan using <13543>btree_hypo_id on hypo (cost=0.04..8.06 rows=1 width=15)
列出和管理假想索引:
SELECT * FROM hypopg_list_indexes();
SELECT * FROM hypopg_drop_index(13543);
SELECT * FROM hypopg_reset();
限制
- 只有不带
ANALYZE的EXPLAIN才会考虑假想索引 - 假想索引仅存在于当前后端会话中
- 其他并发连接不受影响
- 索引名称和部分 CREATE INDEX 选项会被忽略