indexes.txt 6.8 KB

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