indexes.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. .. versionchanged:: 5.1
  40. The ``deduplicate_items`` parameter was added.
  41. .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
  42. .. _deduplicate_items: https://www.postgresql.org/docs/current/btree-implementation.html#BTREE-DEDUPLICATION
  43. ``GinIndex``
  44. ============
  45. .. class:: GinIndex(*expressions, fastupdate=None, gin_pending_list_limit=None, **options)
  46. Creates a `gin index <https://www.postgresql.org/docs/current/gin.html>`_.
  47. To use this index on data types not in the `built-in operator classes
  48. <https://www.postgresql.org/docs/current/gin-builtin-opclasses.html>`_,
  49. you need to activate the `btree_gin extension
  50. <https://www.postgresql.org/docs/current/btree-gin.html>`_ on
  51. PostgreSQL. You can install it using the
  52. :class:`~django.contrib.postgres.operations.BtreeGinExtension` migration
  53. operation.
  54. Set the ``fastupdate`` parameter to ``False`` to disable the `GIN Fast
  55. Update Technique`_ that's enabled by default in PostgreSQL.
  56. Provide an integer number of kilobytes to the gin_pending_list_limit_
  57. parameter to tune the maximum size of the GIN pending list which is used
  58. when ``fastupdate`` is enabled.
  59. .. _GIN Fast Update Technique: https://www.postgresql.org/docs/current/gin-implementation.html#GIN-FAST-UPDATE
  60. .. _gin_pending_list_limit: https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-GIN-PENDING-LIST-LIMIT
  61. ``GistIndex``
  62. =============
  63. .. class:: GistIndex(*expressions, buffering=None, fillfactor=None, **options)
  64. Creates a `GiST index
  65. <https://www.postgresql.org/docs/current/gist.html>`_. These indexes are
  66. automatically created on spatial fields with :attr:`spatial_index=True
  67. <django.contrib.gis.db.models.BaseSpatialField.spatial_index>`. They're
  68. also useful on other types, such as
  69. :class:`~django.contrib.postgres.fields.HStoreField` or the :ref:`range
  70. fields <range-fields>`.
  71. To use this index on data types not in the built-in `gist operator classes
  72. <https://www.postgresql.org/docs/current/gist-builtin-opclasses.html>`_,
  73. you need to activate the `btree_gist extension
  74. <https://www.postgresql.org/docs/current/btree-gist.html>`_ on PostgreSQL.
  75. You can install it using the
  76. :class:`~django.contrib.postgres.operations.BtreeGistExtension` migration
  77. operation.
  78. Set the ``buffering`` parameter to ``True`` or ``False`` to manually enable
  79. or disable `buffering build`_ of the index.
  80. Provide an integer value from 10 to 100 to the fillfactor_ parameter to
  81. tune how packed the index pages will be. PostgreSQL's default is 90.
  82. .. _buffering build: https://www.postgresql.org/docs/current/gist-implementation.html#GIST-BUFFERING-BUILD
  83. .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
  84. ``HashIndex``
  85. =============
  86. .. class:: HashIndex(*expressions, fillfactor=None, **options)
  87. Creates a hash index.
  88. Provide an integer value from 10 to 100 to the fillfactor_ parameter to
  89. tune how packed the index pages will be. PostgreSQL's default is 90.
  90. .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
  91. ``SpGistIndex``
  92. ===============
  93. .. class:: SpGistIndex(*expressions, fillfactor=None, **options)
  94. Creates an `SP-GiST index
  95. <https://www.postgresql.org/docs/current/spgist.html>`_.
  96. Provide an integer value from 10 to 100 to the fillfactor_ parameter to
  97. tune how packed the index pages will be. PostgreSQL's default is 90.
  98. .. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
  99. ``OpClass()`` expressions
  100. =========================
  101. .. class:: OpClass(expression, name)
  102. An ``OpClass()`` expression represents the ``expression`` with a custom
  103. `operator class`_ that can be used to define functional indexes, functional
  104. unique constraints, or exclusion constraints. To use it, you need to add
  105. ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`. Set the
  106. ``name`` parameter to the name of the `operator class`_.
  107. For example::
  108. Index(
  109. OpClass(Lower("username"), name="varchar_pattern_ops"),
  110. name="lower_username_idx",
  111. )
  112. creates an index on ``Lower('username')`` using ``varchar_pattern_ops``.
  113. ::
  114. UniqueConstraint(
  115. OpClass(Upper("description"), name="text_pattern_ops"),
  116. name="upper_description_unique",
  117. )
  118. creates a unique constraint on ``Upper('description')`` using
  119. ``text_pattern_ops``.
  120. ::
  121. ExclusionConstraint(
  122. name="exclude_overlapping_ops",
  123. expressions=[
  124. (OpClass("circle", name="circle_ops"), RangeOperators.OVERLAPS),
  125. ],
  126. )
  127. creates an exclusion constraint on ``circle`` using ``circle_ops``.
  128. .. _operator class: https://www.postgresql.org/docs/current/indexes-opclass.html