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

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

## `BloomIndex`

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

> **New in Django 3.1**

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

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

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

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

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `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` 参数取一个正整数。

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `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。

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `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/3.2/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) 。

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

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `GistIndex`

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

创建一个 [GiST 索引](https://www.postgresql.org/docs/current/gist.html) 。这些索引在空间字段上会自动创建 [`spatial_index=True`](/zh-hans/3.2/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/3.2/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。

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `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。

> **只在 PostgreSQL 10 及以后的版本中使用此索引。**
>
> 哈希索引在 PostgreSQL 中已经存在很长时间了，但在旧版本中，哈希索引存在一些数据完整性问题。

> **Changed in Django 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `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 3.2**
>
> 增加了位置参数 \*expressions，以支持函数索引。

## `OpClass()` 表达式

> **New in Django 3.2**

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

一个 `OpClass()` 表达式代表了具有自定义 [operator class](https://www.postgresql.org/docs/current/indexes-opclass.html) 的  `expression` ，可以用来定义函数索引。要使用它，你需要在你的 [`INSTALLED_APPS`](/zh-hans/3.2/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')` 创建索引。
