---
title: "PostgreSQL 特有模型索引"
version: 5.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/5.1/ref/contrib/postgres/indexes/
canonical: https://djangodocs.dev/zh-hans/5.1/ref/contrib/postgres/indexes/
---
# PostgreSQL 特有模型索引

以下是 PostgreSQL 特有的 [索引](/zh-hans/5.1/ref/models/indexes/) 可以从 `django.contrib.postgres.indexes` 模块中获得。

## `BloomIndex`

#### `class BloomIndex(*expressions, length=None, columns=(), **options)`

创建一个 [bloom](https://www.postgresql.org/docs/current/bloom.html) 索引。

要使用这个索引访问，你需要激活 PostgreSQL 上的 [bloom](https://www.postgresql.org/docs/current/bloom.html) 扩展。你可以使用 [`BloomExtension`](/zh-hans/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BloomExtension) 迁移操作来安装它。

为 `length` 参数提供一个从 1 到 4096 的整数位，用于指定每个索引条目的长度。PostgreSQL 的默认值是 80。

`columns` 参数取一个元组或最多 32 个值的列表，这些值是 1 到 4095 的整数位。

## `BrinIndex`

#### `class BrinIndex(*expressions, autosummarize=None, pages_per_range=None, **options)`

创建一个 [BRIN 索引](https://www.postgresql.org/docs/current/brin.html)。

将 `autosummarize` 参数设置为 `True`，启用 autovacuum 进行 [自动汇总](https://www.postgresql.org/docs/current/brin.html#BRIN-OPERATION) 。

`pages_per_range` 参数取一个正整数。

## `BTreeIndex`

#### `class BTreeIndex(*expressions, fillfactor=None, deduplicate_items=None, **options)`

创建一个 B 树索引。

为 [fillfactor](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS) 参数提供一个从  10 到 100 的整数值，以调整索引页的打包程度。PostgreSQL 的默认值是 90。

向 [deduplicate\_items](https://www.postgresql.org/docs/current/btree.html#BTREE-DEDUPLICATION) 参数提供一个布尔值，以控制是否启用去重。PostgreSQL 默认启用去重。

> **Changed in Django 5.1**
>
> 增加了 `deduplicate_items` 参数。

## `GinIndex`

#### `class GinIndex(*expressions, fastupdate=None, gin_pending_list_limit=None, **options)`

创建一个 [gin 索引](https://www.postgresql.org/docs/current/gin.html) 。

To use this index on data types not in the [built-in operator classes](https://www.postgresql.org/docs/current/gin.html#GIN-BUILTIN-OPCLASSES),
you need to activate the [btree\_gin extension](https://www.postgresql.org/docs/current/btree-gin.html) on
PostgreSQL. You can install it using the
[`BtreeGinExtension`](/zh-hans/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGinExtension) migration
operation.

将 `fastupdate` 参数设置为 `False`，以禁用 PostgreSQL 中默认启用的 [GIN 快速更新技术](https://www.postgresql.org/docs/current/gin.html#GIN-FAST-UPDATE) 。

将一个整数值以千字节为单位提供给 [gin\_pending\_list\_limit](https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT) 参数，以调整当启用 `fastupdate` 时用于限制 GIN 待处理列表的最大大小。

## `GistIndex`

#### `class GistIndex(*expressions, buffering=None, fillfactor=None, **options)`

创建一个 [GiST 索引](https://www.postgresql.org/docs/current/gist.html) 。这些索引在空间字段上会自动创建 [`spatial_index=True`](/zh-hans/5.1/ref/contrib/gis/model-api/#django.contrib.gis.db.models.BaseSpatialField.spatial_index)。它们在其他类型上也很有用，比如 `HStoreField` 或者 range fields。

To use this index on data types not in the built-in [gist operator classes](https://www.postgresql.org/docs/current/gist.html#GIST-BUILTIN-OPCLASSES),
you need to activate the [btree\_gist extension](https://www.postgresql.org/docs/current/btree-gist.html) on PostgreSQL.
You can install it using the
[`BtreeGistExtension`](/zh-hans/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGistExtension) migration
operation.

将 `buffering` 参数设置为 `True` 或 `False`，手动启用或禁用索引的 [buffering build](https://www.postgresql.org/docs/current/gist.html#GIST-BUFFERING-BUILD) 。

为 [fillfactor](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS) 参数提供一个从  10 到 100 的整数值，以调整索引页的打包程度。PostgreSQL 的默认值是 90。

## `HashIndex`

#### `class HashIndex(*expressions, fillfactor=None, **options)`

创建一个哈希索引。

为 [fillfactor](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS) 参数提供一个从  10 到 100 的整数值，以调整索引页的打包程度。PostgreSQL 的默认值是 90。

## `SpGistIndex`

#### `class SpGistIndex(*expressions, fillfactor=None, **options)`

创建 [SP-GIST 索引](https://www.postgresql.org/docs/current/spgist.html) 。

为 [fillfactor](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS) 参数提供一个从  10 到 100 的整数值，以调整索引页的打包程度。PostgreSQL 的默认值是 90。

## `OpClass()` 表达式

#### `class OpClass(expression, name)`

一个 `OpClass()` 表达式表示带有自定义 [operator class](https://www.postgresql.org/docs/current/indexes-opclass.html) 的 `expression`，它可用于定义功能索引、功能唯一约束或排除约束。要使用它，您需要在 [`INSTALLED_APPS`](/zh-hans/5.1/ref/settings/#std-setting-INSTALLED_APPS) 中添加 `'django.contrib.postgres'`。将 `name` 参数设置为 [operator class](https://www.postgresql.org/docs/current/indexes-opclass.html) 的名称。

例子：

```
Index(
    OpClass(Lower("username"), name="varchar_pattern_ops"),
    name="lower_username_idx",
)
```

使用 `varchar_pattern_ops` 在 `Lower('username')` 上创建一个索引。:

```
UniqueConstraint(
    OpClass(Upper("description"), name="text_pattern_ops"),
    name="upper_description_unique",
)
```

使用 `text_pattern_ops` 在 `Upper('description')` 上创建一个唯一约束。:

```
ExclusionConstraint(
    name="exclude_overlapping_ops",
    expressions=[
        (OpClass("circle", name="circle_ops"), RangeOperators.OVERLAPS),
    ],
)
```

使用 `circle_ops` 在 `circle` 上创建一个排除约束。
