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

以下是 PostgreSQL 特有的 [索引](/zh-hans/4.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/4.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-intro.html) 。

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

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

## `BTreeIndex`

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

创建一个 B 树索引。

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

## `GinIndex`

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

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

要想在不在 [内置运算符类](https://www.postgresql.org/docs/current/gin-builtin-opclasses.html) 的数据类型上使用这个索引，需要在 PostgreSQL 上激活 [btree\_gin 扩展](https://www.postgresql.org/docs/current/btree-gin.html) 。你可以使用 [`BtreeGinExtension`](/zh-hans/4.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGinExtension) 迁移操作来安装它。

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

Provide an integer number of kilobytes to the [gin\_pending\_list\_limit](https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT)
parameter to tune the maximum size of the GIN pending list which is used
when `fastupdate` is enabled.

## `GistIndex`

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

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

要在不在内置的 [gist 操作类](https://www.postgresql.org/docs/current/gist-builtin-opclasses.html) 的数据类型上使用这个索引，需要在 PostgreSQL 上激活 [btree\_gist 扩展](https://www.postgresql.org/docs/current/btree-gist.html) 。你可以使用 [`BtreeGistExtension`](/zh-hans/4.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGistExtension) 迁移操作来安装它。

将 `buffering` 参数设置为 `True` 或 `False`，手动启用或禁用索引的 [buffering build](https://www.postgresql.org/docs/current/gist-implementation.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。

> **Changed in Django 4.1**
>
> Support for covering SP-GiST indexes on PostgreSQL 14+ was added.

## `OpClass()` 表达式

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

An `OpClass()` expression represents the `expression` with a custom
[operator class](https://www.postgresql.org/docs/current/indexes-opclass.html) that can be used to define functional indexes, functional
unique constraints, or exclusion constraints. To use it, you need to add
`'django.contrib.postgres'` in your [`INSTALLED_APPS`](/zh-hans/4.1/ref/settings/#std-setting-INSTALLED_APPS). Set the
`name` parameter to the name of the [operator class](https://www.postgresql.org/docs/current/indexes-opclass.html).

例子：

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

creates an index on `Lower('username')` using `varchar_pattern_ops`.

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

creates a unique constraint on `Upper('description')` using
`text_pattern_ops`.

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

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

> **Changed in Django 4.0**
>
> 增加了对函数唯一约束的支持。

> **Changed in Django 4.1**
>
> Support for exclusion constraints was added.
