{"title":"PostgreSQL specific model indexes","version":"5.1","locale":"en","docname":"ref/contrib/postgres/indexes","url":"/en/5.1/ref/contrib/postgres/indexes/","canonical":"https://djangodocs.dev/en/5.1/ref/contrib/postgres/indexes/","summary":"The following are PostgreSQL specific indexes available from the django.contrib.postgres.indexes module. BloomIndex Link to this heading # class BloomIndex ( *…","html":"<span id=\"postgresql-specific-model-indexes\"></span><h1>PostgreSQL specific model indexes<a class=\"heading-anchor\" href=\"#module-django.contrib.postgres.indexes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>The following are PostgreSQL specific <a class=\"reference internal\" href=\"/en/5.1/ref/models/indexes/\"><span class=\"doc\">indexes</span></a>\navailable from the <code class=\"docutils literal notranslate\">django.contrib.postgres.indexes</code> module.</p>\n<section id=\"bloomindex\">\n<h2><code class=\"docutils literal notranslate\">BloomIndex</code><a class=\"heading-anchor\" href=\"#bloomindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.BloomIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BloomIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">length<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">columns<span class=\"o\">=</span><span class=\"default_value\">()</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.BloomIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/bloom.html\">bloom</a> index.</p>\n<p>To use this index access you need to activate the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/bloom.html\">bloom</a> extension on\nPostgreSQL. You can install it using the\n<a class=\"reference internal\" href=\"/en/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BloomExtension\" title=\"django.contrib.postgres.operations.BloomExtension\"><code class=\"xref py py-class docutils literal notranslate\">BloomExtension</code></a> migration\noperation.</p>\n<p>Provide an integer number of bits from 1 to 4096 to the <code class=\"docutils literal notranslate\">length</code>\nparameter to specify the length of each index entry. PostgreSQL’s default\nis 80.</p>\n<p>The <code class=\"docutils literal notranslate\">columns</code> argument takes a tuple or list of up to 32 values that are\ninteger number of bits from 1 to 4095.</p>\n</dd></dl>\n\n</section>\n<section id=\"brinindex\">\n<h2><code class=\"docutils literal notranslate\">BrinIndex</code><a class=\"heading-anchor\" href=\"#brinindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.BrinIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BrinIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">autosummarize<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">pages_per_range<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.BrinIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/brin.html\">BRIN index</a>.</p>\n<p>Set the <code class=\"docutils literal notranslate\">autosummarize</code> parameter to <code class=\"docutils literal notranslate\">True</code> to enable <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/brin.html#BRIN-OPERATION\">automatic\nsummarization</a> to be performed by autovacuum.</p>\n<p>The <code class=\"docutils literal notranslate\">pages_per_range</code> argument takes a positive integer.</p>\n</dd></dl>\n\n</section>\n<section id=\"btreeindex\">\n<h2><code class=\"docutils literal notranslate\">BTreeIndex</code><a class=\"heading-anchor\" href=\"#btreeindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.BTreeIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BTreeIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">fillfactor<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">deduplicate_items<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.BTreeIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a B-Tree index.</p>\n<p>Provide an integer value from 10 to 100 to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS\">fillfactor</a> parameter to\ntune how packed the index pages will be. PostgreSQL’s default is 90.</p>\n<p>Provide a boolean value to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/btree.html#BTREE-DEDUPLICATION\">deduplicate_items</a> parameter to control\nwhether deduplication is enabled. PostgreSQL enables deduplication by\ndefault.</p>\n<aside class=\"version-note version-changed\" data-version=\"5.1\">\n<p class=\"version-note-title\">Changed in Django 5.1</p><p>The <code class=\"docutils literal notranslate\">deduplicate_items</code> parameter was added.</p>\n</aside>\n</dd></dl>\n\n</section>\n<section id=\"ginindex\">\n<h2><code class=\"docutils literal notranslate\">GinIndex</code><a class=\"heading-anchor\" href=\"#ginindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.GinIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GinIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">fastupdate<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">gin_pending_list_limit<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.GinIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gin.html\">gin index</a>.</p>\n<p>To use this index on data types not in the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gin.html#GIN-BUILTIN-OPCLASSES\">built-in operator classes</a>,\nyou need to activate the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/btree-gin.html\">btree_gin extension</a> on\nPostgreSQL. You can install it using the\n<a class=\"reference internal\" href=\"/en/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGinExtension\" title=\"django.contrib.postgres.operations.BtreeGinExtension\"><code class=\"xref py py-class docutils literal notranslate\">BtreeGinExtension</code></a> migration\noperation.</p>\n<p>Set the <code class=\"docutils literal notranslate\">fastupdate</code> parameter to <code class=\"docutils literal notranslate\">False</code> to disable the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gin.html#GIN-FAST-UPDATE\">GIN Fast\nUpdate Technique</a> that’s enabled by default in PostgreSQL.</p>\n<p>Provide an integer number of kilobytes to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT\">gin_pending_list_limit</a>\nparameter to tune the maximum size of the GIN pending list which is used\nwhen <code class=\"docutils literal notranslate\">fastupdate</code> is enabled.</p>\n</dd></dl>\n\n</section>\n<section id=\"gistindex\">\n<h2><code class=\"docutils literal notranslate\">GistIndex</code><a class=\"heading-anchor\" href=\"#gistindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.GistIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GistIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">buffering<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">fillfactor<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.GistIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gist.html\">GiST index</a>. These indexes are\nautomatically created on spatial fields with <a class=\"reference internal\" href=\"/en/5.1/ref/contrib/gis/model-api/#django.contrib.gis.db.models.BaseSpatialField.spatial_index\" title=\"django.contrib.gis.db.models.BaseSpatialField.spatial_index\"><code class=\"xref py py-attr docutils literal notranslate\">spatial_index=True</code></a>. They’re\nalso useful on other types, such as\n<a class=\"reference internal\" href=\"/en/5.1/ref/contrib/postgres/fields/#django.contrib.postgres.fields.HStoreField\" title=\"django.contrib.postgres.fields.HStoreField\"><code class=\"xref py py-class docutils literal notranslate\">HStoreField</code></a> or the <a class=\"reference internal\" href=\"/en/5.1/ref/contrib/postgres/fields/#range-fields\"><span class=\"std std-ref\">range\nfields</span></a>.</p>\n<p>To use this index on data types not in the built-in <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gist.html#GIST-BUILTIN-OPCLASSES\">gist operator classes</a>,\nyou need to activate the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/btree-gist.html\">btree_gist extension</a> on PostgreSQL.\nYou can install it using the\n<a class=\"reference internal\" href=\"/en/5.1/ref/contrib/postgres/operations/#django.contrib.postgres.operations.BtreeGistExtension\" title=\"django.contrib.postgres.operations.BtreeGistExtension\"><code class=\"xref py py-class docutils literal notranslate\">BtreeGistExtension</code></a> migration\noperation.</p>\n<p>Set the <code class=\"docutils literal notranslate\">buffering</code> parameter to <code class=\"docutils literal notranslate\">True</code> or <code class=\"docutils literal notranslate\">False</code> to manually enable\nor disable <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/gist.html#GIST-BUFFERING-BUILD\">buffering build</a> of the index.</p>\n<p>Provide an integer value from 10 to 100 to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS\">fillfactor</a> parameter to\ntune how packed the index pages will be. PostgreSQL’s default is 90.</p>\n</dd></dl>\n\n</section>\n<section id=\"hashindex\">\n<h2><code class=\"docutils literal notranslate\">HashIndex</code><a class=\"heading-anchor\" href=\"#hashindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.HashIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">HashIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">fillfactor<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.HashIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a hash index.</p>\n<p>Provide an integer value from 10 to 100 to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS\">fillfactor</a> parameter to\ntune how packed the index pages will be. PostgreSQL’s default is 90.</p>\n</dd></dl>\n\n</section>\n<section id=\"spgistindex\">\n<h2><code class=\"docutils literal notranslate\">SpGistIndex</code><a class=\"heading-anchor\" href=\"#spgistindex\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.SpGistIndex\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SpGistIndex</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>expressions</em>, <em class=\"sig-param\">fillfactor<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.SpGistIndex\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates an <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/spgist.html\">SP-GiST index</a>.</p>\n<p>Provide an integer value from 10 to 100 to the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS\">fillfactor</a> parameter to\ntune how packed the index pages will be. PostgreSQL’s default is 90.</p>\n</dd></dl>\n\n</section>\n<section id=\"opclass-expressions\">\n<h2><code class=\"docutils literal notranslate\">OpClass()</code> expressions<a class=\"heading-anchor\" href=\"#opclass-expressions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.postgres.indexes.OpClass\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">OpClass</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">expression</em>, <em class=\"sig-param\">name</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.postgres.indexes.OpClass\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>An <code class=\"docutils literal notranslate\">OpClass()</code> expression represents the <code class=\"docutils literal notranslate\">expression</code> with a custom\n<a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/indexes-opclass.html\">operator class</a> that can be used to define functional indexes, functional\nunique constraints, or exclusion constraints. To use it, you need to add\n<code class=\"docutils literal notranslate\">'django.contrib.postgres'</code> in your <a class=\"reference internal\" href=\"/en/5.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>. Set the\n<code class=\"docutils literal notranslate\">name</code> parameter to the name of the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/indexes-opclass.html\">operator class</a>.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>Index<span class=\"p\">(</span>\n    OpClass<span class=\"p\">(</span>Lower<span class=\"p\">(</span><span class=\"s2\">&quot;username&quot;</span><span class=\"p\">),</span> name<span class=\"o\">=</span><span class=\"s2\">&quot;varchar_pattern_ops&quot;</span><span class=\"p\">),</span>\n    name<span class=\"o\">=</span><span class=\"s2\">&quot;lower_username_idx&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>creates an index on <code class=\"docutils literal notranslate\">Lower('username')</code> using <code class=\"docutils literal notranslate\">varchar_pattern_ops</code>.</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>UniqueConstraint<span class=\"p\">(</span>\n    OpClass<span class=\"p\">(</span>Upper<span class=\"p\">(</span><span class=\"s2\">&quot;description&quot;</span><span class=\"p\">),</span> name<span class=\"o\">=</span><span class=\"s2\">&quot;text_pattern_ops&quot;</span><span class=\"p\">),</span>\n    name<span class=\"o\">=</span><span class=\"s2\">&quot;upper_description_unique&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>creates a unique constraint on <code class=\"docutils literal notranslate\">Upper('description')</code> using\n<code class=\"docutils literal notranslate\">text_pattern_ops</code>.</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>ExclusionConstraint<span class=\"p\">(</span>\n    name<span class=\"o\">=</span><span class=\"s2\">&quot;exclude_overlapping_ops&quot;</span><span class=\"p\">,</span>\n    expressions<span class=\"o\">=</span><span class=\"p\">[</span>\n        <span class=\"p\">(</span>OpClass<span class=\"p\">(</span><span class=\"s2\">&quot;circle&quot;</span><span class=\"p\">,</span> name<span class=\"o\">=</span><span class=\"s2\">&quot;circle_ops&quot;</span><span class=\"p\">),</span> RangeOperators<span class=\"o\">.</span>OVERLAPS<span class=\"p\">),</span>\n    <span class=\"p\">],</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>creates an exclusion constraint on <code class=\"docutils literal notranslate\">circle</code> using <code class=\"docutils literal notranslate\">circle_ops</code>.</p>\n</dd></dl>\n\n</section>","rootId":"module-django.contrib.postgres.indexes","toc":[{"title":"BloomIndex","anchor":"bloomindex","children":[]},{"title":"BrinIndex","anchor":"brinindex","children":[]},{"title":"BTreeIndex","anchor":"btreeindex","children":[]},{"title":"GinIndex","anchor":"ginindex","children":[]},{"title":"GistIndex","anchor":"gistindex","children":[]},{"title":"HashIndex","anchor":"hashindex","children":[]},{"title":"SpGistIndex","anchor":"spgistindex","children":[]},{"title":"OpClass() expressions","anchor":"opclass-expressions","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/5.1/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/5.1/ref/contrib/"},{"docname":"ref/contrib/postgres/index","title":"django.contrib.postgres","url":"/en/5.1/ref/contrib/postgres/"}],"prev":{"docname":"ref/contrib/postgres/functions","title":"PostgreSQL specific database functions","url":"/en/5.1/ref/contrib/postgres/functions/"},"next":{"docname":"ref/contrib/postgres/lookups","title":"PostgreSQL specific lookups","url":"/en/5.1/ref/contrib/postgres/lookups/"},"formats":{"html":"/en/5.1/ref/contrib/postgres/indexes/","markdown":"/en/5.1/ref/contrib/postgres/indexes.md","json":"/en/5.1/ref/contrib/postgres/indexes.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/ref/contrib/postgres/indexes.txt","official":"https://docs.djangoproject.com/en/5.1/ref/contrib/postgres/indexes/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}