# GiST（PostgreSQL）

GiST（Generalized Search Tree，广义搜索树）是 [PostgreSQL](https://www.edgepedia.org/zh/postgresql) 中的一种平衡的树结构索引访问方法，它充当一个基础模板，在它之上可以实现任意索引方案；B 树、R 树以及许多其他索引方案都可以建立在它之上。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> 它由威斯康星大学麦迪逊分校的约瑟夫·M. 海勒斯坦（Joseph M. Hellerstein）和杰弗里·F. 诺顿（Jeffrey F. Naughton），以及加州大学伯克利分校的阿维·普费弗（Avi Pfeffer）在 1995 年的一篇 VLDB 论文中提出，作为一种单一数据结构，提供数据库系统所需的基本搜索树逻辑，把 B+ 树和 R 树等结构统一到同一段代码中。<sup>[2](https://courses.cs.duke.edu/spring04/cps216/papers/hellerstein-etal-1995.pdf)</sup> 在 PostgreSQL 中，GiST 实现主要由特奥多尔·西加耶夫（Teodor Sigaev）和奥列格·巴尔图诺夫（Oleg Bartunov）维护。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 全称 | Generalized Search Tree（广义搜索树）；一种平衡的树结构访问方法，可作为其他索引方案（包括 B 树和 R 树）的模板<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> |
| 起源 | 由海勒斯坦、诺顿和普费弗在 1995 年 VLDB（苏黎世）上提出<sup>[2](https://courses.cs.duke.edu/spring04/cps216/papers/hellerstein-etal-1995.pdf)</sup> |
| 操作符类 API | 当前 PostgreSQL 中五个必需方法和七个可选方法；正确性取决于 `same`、`consistent` 和 `union`，效率取决于 `penalty` 和 `picksplit`<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> |
| 内置操作符类 | `box_ops`、`circle_ops`、`point_ops`、`poly_ops`、`range_ops`、`inet_ops`、`tsquery_ops`、`tsvector_ops` 等<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> |
| 最近邻搜索 | 通过 `<->` 等排序操作符支持，例如 `ORDER BY location <-> point '(101,456)' LIMIT 10`<sup>[3](https://www.postgresql.org/docs/16/indexes-types.html)</sup> |
| 索引构建方法 | 简单插入、缓冲构建（自 9.2 版起），以及当每个操作符类都提供 `sortsupport` 函数时使用的排序构建<sup>[4](https://www.postgresql.org/files/documentation/pdf/9.4/postgresql-9.4-US.pdf)</sup><sup> • </sup><sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup> |
| 仅索引扫描 | 对某些 GiST 操作符类支持，对另一些不支持；索引必须存储或重建原始数据值<sup>[6](https://www.postgresql.org/docs/16/indexes-index-only-scans.html)</sup> |

## 目的与设计

GiST 是谓词的层次结构。与其他二级索引树一样，它在叶子中存储（键，RID）对，其中记录标识符指向数据页上的行；内部节点包含（谓词，子页指针）对，并且对于关联子页中包含或可到达的任意键，每个谓词都求值为真。键空间不必有序，这允许多维数据，并且同一树层级上的边界谓词可以重叠。<sup>[7](https://dsf.berkeley.edu/papers/sigmod97-gist.pdf)</sup>

实际动机是可扩展性。例如，在图像集合上的 B 树索引只能回答诸如“图像x是否等于图像y”“图像x是否小于图像y”或“图像x是否大于图像y”之类的问题。使用基于 GiST 的索引，开发者可以创造方式来提出领域特定问题，或许是“找出所有马的照片”或“找出所有曝光过度的图像”。<sup>[8](https://www.postgresql.org/docs/17/gist.html)</sup> 传统上，实现新的索引访问方法需要理解数据库内部机制，例如锁管理器和预写式日志；GiST 接口具有很高的抽象层次，因此实现者只需提供数据类型的语义，而 GiST 层负责并发、日志记录和搜索树结构。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> 一个已说明的优点是，自定义数据类型可以由该数据类型领域的专家而不是数据库专家赋予适当的访问方法。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

## 操作符类接口

索引操作符类定义某一特定数据类型如何与索引一起使用：它把操作符分配给策略角色，并指定索引方法调用的支持函数。<sup>[9](https://www.postgresql.org/docs/18/sql-createopclass.html)</sup> 一个 GiST 操作符类必须提供五个方法，并可提供七个可选方法。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

- **`consistent`** 在给定索引条目和查询值时，判断该条目是否与查询一致，即对于该条目所表示的任意行，谓词是否可能为真。对于叶子条目，这就是被索引的条件本身；对于内部节点，它判断是否必须扫描该子树。当结果为真时，该函数还返回一个 `recheck` 标志：false 表示谓词已被精确测试，true 表示该行只是候选匹配，系统将针对实际行值计算该操作符。这一约定使 GiST 能够同时支持无损和有损索引结构。<sup>[8](https://www.postgresql.org/docs/17/gist.html)</sup>
- **`union`** 计算一组键的并集，**`same`** 测试两个索引条目是否相同。与 `consistent` 一起，这些函数确保索引的正确性。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>
- **`penalty`** 和 **`picksplit`** 决定效率。当需要拆分索引页时，`picksplit` 决定哪些条目留在旧页、哪些移到新页；其结果通过修改传入的结构交付，而不是通过返回值交付。设计合适的 `penalty` 和 `picksplit` 实现，正是构建高性能 GiST 索引的挑战所在。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup><sup> • </sup><sup>[10](https://www.postgresql.org/docs/19/gist.html)</sup>
- 可选的 **`compress`** 和 **`decompress`** 方法让索引存储与其所索引数据不同类型的内部树数据；`CREATE OPERATOR CLASS` 的 `STORAGE` 选项可以声明该存储类型。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup><sup> • </sup><sup>[9](https://www.postgresql.org/docs/18/sql-createopclass.html)</sup>
- 可选的 **`distance`** 方法是有序扫描（最近邻搜索）所必需的；**`fetch`** 支持仅索引扫描，但省略 `compress` 时除外；**`options`** 定义用户可见参数；**`sortsupport`** 加速索引构建；**`stratnum`** 把比较类型转换为操作符类使用的策略号。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

当 `compress` 方法对叶子条目应用有损压缩时，该操作符类不支持仅索引扫描，并且必须省略 `fetch` 函数。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> 该 API 随时间发展：PostgreSQL 9.4 文档描述了七个必需方法和一个可选方法，而当前文档描述了五个必需方法和七个可选方法。<sup>[4](https://www.postgresql.org/files/documentation/pdf/9.4/postgresql-9.4-US.pdf)</sup><sup> • </sup><sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

## 内置与贡献操作符类

核心发行版包含针对若干二维几何类型（`box_ops`、`circle_ops`、`point_ops`、`poly_ops`）、范围和多重范围类型（`range_ops`）、网络地址（`inet_ops`）以及文本搜索（`tsquery_ops`、`tsvector_ops`）的 GiST 操作符类。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup> 由于历史原因，`inet_ops` 不是 `inet` 和 `cidr` 类型的默认类；要使用它，必须在 `CREATE INDEX` 中给出类名，例如 `CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops)`。<sup>[1](https://www.postgresql.org/docs/current/gist.html)</sup>

GiST 索引可以使用的具体操作符随操作符类而变化。几何类支持使用 `<<`、`&<`、`&&`、`@>` 和 `<@` 等操作符的索引查询，而定义了距离操作符的类通过操作符类表的“排序操作符”列支持最近邻搜索。<sup>[3](https://www.postgresql.org/docs/16/indexes-types.html)</sup> 贡献模块增加了更多类：`btree_gist`（为若干数据类型提供 B 树等价行为，包括对具有自然距离度量的类型（如 `int4`、`float8` 和 `timestamp`）使用 `<->` 进行最近邻搜索）、`cube`（多维立方体）、`hstore`（键值对）、`intarray`（用于 int4 数组的 RD 树）、`ltree`（树状结构）、`pg_trgm`（使用三元组匹配的文本相似度）和 `seg`（浮点范围）。<sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup><sup> • </sup><sup>[11](https://www.postgresql.org/docs/current/btree-gist.html)</sup>

## 有损性与仅索引扫描

有些 GiST 操作符类是有损的。对于文本搜索，每个文档在索引中由一个固定长度签名表示，因此涉及权重的查询需要对表行进行复检，而有损性会因为不必要地获取最终为假匹配的记录而导致性能下降。<sup>[12](https://www.postgresql.org/docs/16/textsearch-indexes.html)</sup> 更一般地，GiST 和 SP-GiST 索引对某些操作符类支持仅索引扫描，对另一些则不支持；底层要求是索引必须物理存储或能够重建每个条目的原始数据值。<sup>[6](https://www.postgresql.org/docs/16/indexes-index-only-scans.html)</sup>

## 索引构建

构建 GiST 索引的最简单方法是逐条插入所有条目，对于大型索引这很慢，因为分散的元组需要大量随机 I/O。PostgreSQL 支持两种替代的初始构建方法：排序模式和缓冲模式。<sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup> 缓冲方法在 9.2 版中引入，它不直接插入元组；它把缓冲区附加到内部节点，使插入更多地按广度优先顺序发生，对于非有序数据集可以显著减少随机 I/O，不过它会更频繁地调用 `penalty` 函数，并需要最多达到结果索引大小的临时磁盘空间。<sup>[4](https://www.postgresql.org/files/documentation/pdf/9.4/postgresql-9.4-US.pdf)</sup><sup> • </sup><sup>[13](https://github.com/postgres/postgres/blob/master/src/backend/access/gist/README)</sup> 如果无法排序，当索引大小达到 `effective_cache_size` 时，构建默认切换到缓冲；`CREATE INDEX` 的 `buffering` 参数可以手动强制或阻止缓冲。<sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup> 排序方法自底向上构建索引，类似 B 树构建，当所有被索引列的操作符类都定义了 `sortsupport` 函数时使用。<sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup><sup> • </sup><sup>[13](https://github.com/postgres/postgres/blob/master/src/backend/access/gist/README)</sup> PostgreSQL 18 增加了对值排序以加速范围类型 GiST 索引构建的能力。<sup>[14](https://www.postgresql.org/docs/18/release-18.html)</sup>

缓冲构建算法基于拉尔斯·阿尔格、克劳斯·欣里希斯、扬·法伦霍尔德和杰弗里·斯科特·维特的论文《动态 R 树上的高效批量操作》开发，并发支持基于[马塞尔·科纳克](https://www.edgepedia.org/zh/marcel-kornacker)的《下一代数据库系统的访问方法》开发，原始算法经过调整以符合 PostgreSQL 惯例，例如每次搜索调用返回一个元组并支持变长键。<sup>[13](https://github.com/postgres/postgres/blob/master/src/backend/access/gist/README)</sup>

## 空间索引与应用

PostGIS 指示用户使用 `CREATE INDEX ... USING GIST (geom)` 创建空间索引；当绑定到 GiST 访问方法时，几何类型的默认索引是 R 树，而省略 `USING GIST` 会生成 B 树，后者是非空间的，不会帮助空间查询。<sup>[15](https://postgis.net/documentation/faq/spatial-indexes/)</sup> 像 `ST_Intersects`、`ST_Contains`、`ST_Within` 和 `ST_DWithin` 这类能感知索引的 PostGIS 函数可以把空间索引用作预过滤器。<sup>[15](https://postgis.net/documentation/faq/spatial-indexes/)</sup> 因为索引存储的是边界框而不是完整几何，GiST 扫描可以低成本地拒绝一行，但可能返回假阳性，执行器必须针对实际几何重新检查。<sup>[16](https://www.postgis-python.com/advanced-gist-indexing-optimization/)</sup>

除几何之外，GiST 索引还服务于全文搜索（`tsvector` 和 `tsquery`）、基于三元组的文本相似度、多维立方体和范围查询，原始论文还展示了针对集合值数据表现得像 B+ 树、R 树和 RD 树的实现。<sup>[5](https://www.postgresql.org/docs/18/gist.html)</sup><sup> • </sup><sup>[2](https://courses.cs.duke.edu/spring04/cps216/papers/hellerstein-etal-1995.pdf)</sup> 后来的研究扩展了这一思想本身：MGiST 和 MSP-GiST 是 GiST 和 SP-GiST 的多条目对应物，旨在让单个对象在插入期间被分区为多个条目，面向轨迹索引。<sup>[17](https://arxiv.org/abs/2406.05327)</sup>

## References

1. [PostgreSQL Documentation: 65.2. GiST Indexes](https://www.postgresql.org/docs/current/gist.html)
2. [Hellerstein, Naughton, Pfeffer (1995). Generalized Search Trees for Database Systems, VLDB 1995](https://courses.cs.duke.edu/spring04/cps216/papers/hellerstein-etal-1995.pdf)
3. [PostgreSQL Documentation: 11.2. Index Types](https://www.postgresql.org/docs/16/indexes-types.html)
4. [PostgreSQL 9.4 Documentation, Chapter 56. GiST Indexes](https://www.postgresql.org/files/documentation/pdf/9.4/postgresql-9.4-US.pdf)
5. [PostgreSQL Documentation: 65.2. GiST Indexes (18)](https://www.postgresql.org/docs/18/gist.html)
6. [PostgreSQL Documentation: 11.9. Index-Only Scans and Covering Indexes](https://www.postgresql.org/docs/16/indexes-index-only-scans.html)
7. [Kornacker (1997). Concurrency and Recovery in Generalized Search Trees](https://dsf.berkeley.edu/papers/sigmod97-gist.pdf)
8. [PostgreSQL Documentation: 64.2. GiST Indexes (17)](https://www.postgresql.org/docs/17/gist.html)
9. [PostgreSQL Documentation: CREATE OPERATOR CLASS](https://www.postgresql.org/docs/18/sql-createopclass.html)
10. [PostgreSQL Documentation: 65.2. GiST Indexes (19)](https://www.postgresql.org/docs/19/gist.html)
11. [PostgreSQL Documentation: F.8. btree_gist](https://www.postgresql.org/docs/current/btree-gist.html)
12. [PostgreSQL Documentation: 12.9. Preferred Index Types for Text Search](https://www.postgresql.org/docs/16/textsearch-indexes.html)
13. [postgres/src/backend/access/gist/README](https://github.com/postgres/postgres/blob/master/src/backend/access/gist/README)
14. [PostgreSQL Documentation: E.6. Release 18](https://www.postgresql.org/docs/18/release-18.html)
15. [PostGIS Documentation: How do I use spatial indexes?](https://postgis.net/documentation/faq/spatial-indexes/)
16. [Advanced GiST Indexing & Optimization, postgis-python.com](https://www.postgis-python.com/advanced-gist-indexing-optimization/)
17. [Multi-Entry Generalized Search Trees for Indexing Trajectories, arXiv:2406.05327](https://arxiv.org/abs/2406.05327)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Databases and data systems › Database theory and data modeling › Indexing and physical data organization

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/gist-postgresql

English original: https://www.edgepedia.org/gist-postgresql
