1. =================================
    
  2. PostgreSQL specific model indexes
    
  3. =================================
    
  4. 
    
  5. .. module:: django.contrib.postgres.indexes
    
  6. 
    
  7. The following are PostgreSQL specific :doc:`indexes </ref/models/indexes>`
    
  8. available from the ``django.contrib.postgres.indexes`` module.
    
  9. 
    
  10. ``BloomIndex``
    
  11. ==============
    
  12. 
    
  13. .. class:: BloomIndex(*expressions, length=None, columns=(), **options)
    
  14. 
    
  15.     Creates a bloom_ index.
    
  16. 
    
  17.     To use this index access you need to activate the bloom_ extension on
    
  18.     PostgreSQL. You can install it using the
    
  19.     :class:`~django.contrib.postgres.operations.BloomExtension` migration
    
  20.     operation.
    
  21. 
    
  22.     Provide an integer number of bits from 1 to 4096 to the ``length``
    
  23.     parameter to specify the length of each index entry. PostgreSQL's default
    
  24.     is 80.
    
  25. 
    
  26.     The ``columns`` argument takes a tuple or list of up to 32 values that are
    
  27.     integer number of bits from 1 to 4095.
    
  28. 
    
  29.     .. _bloom: https://www.postgresql.org/docs/current/bloom.html
    
  30. 
    
  31. ``BrinIndex``
    
  32. =============
    
  33. 
    
  34. .. class:: BrinIndex(*expressions, autosummarize=None, pages_per_range=None, **options)
    
  35. 
    
  36.     Creates a `BRIN index
    
  37.     <https://www.postgresql.org/docs/current/brin-intro.html>`_.
    
  38. 
    
  39.     Set the ``autosummarize`` parameter to ``True`` to enable `automatic
    
  40.     summarization`_ to be performed by autovacuum.
    
  41. 
    
  42.     The ``pages_per_range`` argument takes a positive integer.
    
  43. 
    
  44.     .. _automatic summarization: https://www.postgresql.org/docs/current/brin-intro.html#BRIN-OPERATION
    
  45. 
    
  46. ``BTreeIndex``
    
  47. ==============
    
  48. 
    
  49. .. class:: BTreeIndex(*expressions, fillfactor=None, **options)
    
  50. 
    
  51.     Creates a B-Tree index.
    
  52. 
    
  53.     Provide an integer value from 10 to 100 to the fillfactor_ parameter to
    
  54.     tune how packed the index pages will be. PostgreSQL's default is 90.
    
  55. 
    
  56.     .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
    
  57. 
    
  58. ``GinIndex``
    
  59. ============
    
  60. 
    
  61. .. class:: GinIndex(*expressions, fastupdate=None, gin_pending_list_limit=None, **options)
    
  62. 
    
  63.     Creates a `gin index <https://www.postgresql.org/docs/current/gin.html>`_.
    
  64. 
    
  65.     To use this index on data types not in the `built-in operator classes
    
  66.     <https://www.postgresql.org/docs/current/gin-builtin-opclasses.html>`_,
    
  67.     you need to activate the `btree_gin extension
    
  68.     <https://www.postgresql.org/docs/current/btree-gin.html>`_ on
    
  69.     PostgreSQL. You can install it using the
    
  70.     :class:`~django.contrib.postgres.operations.BtreeGinExtension` migration
    
  71.     operation.
    
  72. 
    
  73.     Set the ``fastupdate`` parameter to ``False`` to disable the `GIN Fast
    
  74.     Update Technique`_ that's enabled by default in PostgreSQL.
    
  75. 
    
  76.     Provide an integer number of kilobytes to the gin_pending_list_limit_
    
  77.     parameter to tune the maximum size of the GIN pending list which is used
    
  78.     when ``fastupdate`` is enabled.
    
  79. 
    
  80.     .. _GIN Fast Update Technique: https://www.postgresql.org/docs/current/gin-implementation.html#GIN-FAST-UPDATE
    
  81.     .. _gin_pending_list_limit: https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT
    
  82. 
    
  83. ``GistIndex``
    
  84. =============
    
  85. 
    
  86. .. class:: GistIndex(*expressions, buffering=None, fillfactor=None, **options)
    
  87. 
    
  88.     Creates a `GiST index
    
  89.     <https://www.postgresql.org/docs/current/gist.html>`_. These indexes are
    
  90.     automatically created on spatial fields with :attr:`spatial_index=True
    
  91.     <django.contrib.gis.db.models.BaseSpatialField.spatial_index>`. They're
    
  92.     also useful on other types, such as
    
  93.     :class:`~django.contrib.postgres.fields.HStoreField` or the :ref:`range
    
  94.     fields <range-fields>`.
    
  95. 
    
  96.     To use this index on data types not in the built-in `gist operator classes
    
  97.     <https://www.postgresql.org/docs/current/gist-builtin-opclasses.html>`_,
    
  98.     you need to activate the `btree_gist extension
    
  99.     <https://www.postgresql.org/docs/current/btree-gist.html>`_ on PostgreSQL.
    
  100.     You can install it using the
    
  101.     :class:`~django.contrib.postgres.operations.BtreeGistExtension` migration
    
  102.     operation.
    
  103. 
    
  104.     Set the ``buffering`` parameter to ``True`` or ``False`` to manually enable
    
  105.     or disable `buffering build`_ of the index.
    
  106. 
    
  107.     Provide an integer value from 10 to 100 to the fillfactor_ parameter to
    
  108.     tune how packed the index pages will be. PostgreSQL's default is 90.
    
  109. 
    
  110.     .. _buffering build: https://www.postgresql.org/docs/current/gist-implementation.html#GIST-BUFFERING-BUILD
    
  111.     .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
    
  112. 
    
  113. ``HashIndex``
    
  114. =============
    
  115. 
    
  116. .. class:: HashIndex(*expressions, fillfactor=None, **options)
    
  117. 
    
  118.     Creates a hash index.
    
  119. 
    
  120.     Provide an integer value from 10 to 100 to the fillfactor_ parameter to
    
  121.     tune how packed the index pages will be. PostgreSQL's default is 90.
    
  122. 
    
  123.     .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
    
  124. 
    
  125. ``SpGistIndex``
    
  126. ===============
    
  127. 
    
  128. .. class:: SpGistIndex(*expressions, fillfactor=None, **options)
    
  129. 
    
  130.     Creates an `SP-GiST index
    
  131.     <https://www.postgresql.org/docs/current/spgist.html>`_.
    
  132. 
    
  133.     Provide an integer value from 10 to 100 to the fillfactor_ parameter to
    
  134.     tune how packed the index pages will be. PostgreSQL's default is 90.
    
  135. 
    
  136.     .. versionchanged:: 4.1
    
  137. 
    
  138.         Support for covering SP-GiST indexes on PostgreSQL 14+ was added.
    
  139. 
    
  140.     .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
    
  141. 
    
  142. ``OpClass()`` expressions
    
  143. =========================
    
  144. 
    
  145. .. class:: OpClass(expression, name)
    
  146. 
    
  147.     An ``OpClass()`` expression represents the ``expression`` with a custom
    
  148.     `operator class`_ that can be used to define functional indexes, functional
    
  149.     unique constraints, or exclusion constraints. To use it, you need to add
    
  150.     ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`. Set the
    
  151.     ``name`` parameter to the name of the `operator class`_.
    
  152. 
    
  153.     For example::
    
  154. 
    
  155.         Index(
    
  156.             OpClass(Lower('username'), name='varchar_pattern_ops'),
    
  157.             name='lower_username_idx',
    
  158.         )
    
  159. 
    
  160.     creates an index on ``Lower('username')`` using ``varchar_pattern_ops``.
    
  161.     ::
    
  162. 
    
  163.         UniqueConstraint(
    
  164.             OpClass(Upper('description'), name='text_pattern_ops'),
    
  165.             name='upper_description_unique',
    
  166.         )
    
  167. 
    
  168.     creates a unique constraint on ``Upper('description')`` using
    
  169.     ``text_pattern_ops``.
    
  170.     ::
    
  171. 
    
  172.         ExclusionConstraint(
    
  173.             name='exclude_overlapping_ops',
    
  174.             expressions=[
    
  175.                 (OpClass('circle', name='circle_ops'), RangeOperators.OVERLAPS),
    
  176.             ],
    
  177.         )
    
  178. 
    
  179.     creates an exclusion constraint on ``circle`` using ``circle_ops``.
    
  180. 
    
  181.     .. versionchanged:: 4.0
    
  182. 
    
  183.         Support for functional unique constraints was added.
    
  184. 
    
  185.     .. versionchanged:: 4.1
    
  186. 
    
  187.         Support for exclusion constraints was added.
    
  188. 
    
  189.     .. _operator class: https://www.postgresql.org/docs/current/indexes-opclass.html