以下是 PostgreSQL 特有的 索引 可以从 django.contrib.postgres.indexes 模块中获得。
BloomIndex¶创建一个 bloom 索引。
要使用这个索引访问,你需要激活 PostgreSQL 上的 bloom 扩展。你可以使用 BloomExtension 迁移操作来安装它。
为 length 参数提供一个从 1 到 4096 的整数位,用于指定每个索引条目的长度。PostgreSQL 的默认值是 80。
columns 参数取一个元组或最多 32 个值的列表,这些值是 1 到 4095 的整数位。
BrinIndex¶BTreeIndex¶创建一个 B 树索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
向 deduplicate_items 参数提供一个布尔值,以控制是否启用去重。PostgreSQL 默认启用去重。
增加了 deduplicate_items 参数。
GinIndex¶创建一个 gin 索引 。
To use this index on data types not in the built-in operator classes,
you need to activate the btree_gin extension on
PostgreSQL. You can install it using the
BtreeGinExtension migration
operation.
将 fastupdate 参数设置为 False,以禁用 PostgreSQL 中默认启用的 GIN 快速更新技术 。
将一个整数值以千字节为单位提供给 gin_pending_list_limit 参数,以调整当启用 fastupdate 时用于限制 GIN 待处理列表的最大大小。
GistIndex¶创建一个 GiST 索引 。这些索引在空间字段上会自动创建 spatial_index=True。它们在其他类型上也很有用,比如 HStoreField 或者 range fields。
To use this index on data types not in the built-in gist operator classes,
you need to activate the btree_gist extension on PostgreSQL.
You can install it using the
BtreeGistExtension migration
operation.
将 buffering 参数设置为 True 或 False,手动启用或禁用索引的 buffering build 。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
HashIndex¶创建一个哈希索引。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
SpGistIndex¶创建 SP-GIST 索引 。
为 fillfactor 参数提供一个从 10 到 100 的整数值,以调整索引页的打包程度。PostgreSQL 的默认值是 90。
OpClass() 表达式¶一个 OpClass() 表达式表示带有自定义 operator class 的 expression,它可用于定义功能索引、功能唯一约束或排除约束。要使用它,您需要在 INSTALLED_APPS 中添加 'django.contrib.postgres'。将 name 参数设置为 operator class 的名称。
例子:
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 上创建一个排除约束。
8月 13, 2025