indexes.txt 6.5 KB

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