features.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import operator
  2. from django.db import DataError, InterfaceError
  3. from django.db.backends.base.features import BaseDatabaseFeatures
  4. from django.db.backends.postgresql.psycopg_any import is_psycopg3
  5. from django.utils.functional import cached_property
  6. class DatabaseFeatures(BaseDatabaseFeatures):
  7. minimum_database_version = (15,)
  8. allows_group_by_selected_pks = True
  9. can_return_columns_from_insert = True
  10. can_return_rows_from_bulk_insert = True
  11. can_return_rows_from_update = True
  12. has_real_datatype = True
  13. has_native_boolean_field = True
  14. has_native_uuid_field = True
  15. has_native_duration_field = True
  16. has_native_json_field = True
  17. can_defer_constraint_checks = True
  18. has_select_for_update = True
  19. has_select_for_update_nowait = True
  20. has_select_for_update_of = True
  21. has_select_for_update_skip_locked = True
  22. has_select_for_no_key_update = True
  23. can_release_savepoints = True
  24. supports_comments = True
  25. supports_tablespaces = True
  26. supports_transactions = True
  27. can_introspect_materialized_views = True
  28. can_distinct_on_fields = True
  29. can_rollback_ddl = True
  30. schema_editor_uses_clientside_param_binding = True
  31. supports_combined_alters = True
  32. nulls_order_largest = True
  33. closed_cursor_error_class = InterfaceError
  34. greatest_least_ignores_nulls = True
  35. can_clone_databases = True
  36. supports_temporal_subtraction = True
  37. supports_slicing_ordering_in_compound = True
  38. create_test_procedure_without_params_sql = """
  39. CREATE FUNCTION test_procedure () RETURNS void AS $$
  40. DECLARE
  41. V_I INTEGER;
  42. BEGIN
  43. V_I := 1;
  44. END;
  45. $$ LANGUAGE plpgsql;"""
  46. create_test_procedure_with_int_param_sql = """
  47. CREATE FUNCTION test_procedure (P_I INTEGER) RETURNS void AS $$
  48. DECLARE
  49. V_I INTEGER;
  50. BEGIN
  51. V_I := P_I;
  52. END;
  53. $$ LANGUAGE plpgsql;"""
  54. requires_casted_case_in_updates = True
  55. supports_over_clause = True
  56. supports_frame_exclusion = True
  57. only_supports_unbounded_with_preceding_and_following = True
  58. supports_aggregate_filter_clause = True
  59. supports_aggregate_order_by_clause = True
  60. supported_explain_formats = {"JSON", "TEXT", "XML", "YAML"}
  61. supports_deferrable_unique_constraints = True
  62. has_json_operators = True
  63. json_key_contains_list_matching_requires_list = True
  64. supports_update_conflicts = True
  65. supports_update_conflicts_with_target = True
  66. supports_covering_indexes = True
  67. supports_stored_generated_columns = True
  68. supports_nulls_distinct_unique_constraints = True
  69. supports_no_precision_decimalfield = True
  70. can_rename_index = True
  71. test_collations = {
  72. "deterministic": "C",
  73. "non_default": "sv-x-icu",
  74. "swedish_ci": "sv-x-icu",
  75. "virtual": "sv-x-icu",
  76. }
  77. test_now_utc_template = "STATEMENT_TIMESTAMP() AT TIME ZONE 'UTC'"
  78. insert_test_table_with_defaults = "INSERT INTO {} DEFAULT VALUES"
  79. supports_uuid4_function = True
  80. @cached_property
  81. def supports_uuid7_function(self):
  82. return self.is_postgresql_18
  83. @cached_property
  84. def supports_uuid7_function_shift(self):
  85. return self.is_postgresql_18
  86. @cached_property
  87. def django_test_skips(self):
  88. skips = {
  89. "opclasses are PostgreSQL only.": {
  90. "indexes.tests.SchemaIndexesNotPostgreSQLTests."
  91. "test_create_index_ignores_opclasses",
  92. },
  93. "PostgreSQL requires casting to text.": {
  94. "lookup.tests.LookupTests.test_textfield_exact_null",
  95. },
  96. }
  97. if self.connection.settings_dict["OPTIONS"].get("pool"):
  98. skips.update(
  99. {
  100. "Pool does implicit health checks": {
  101. "backends.base.test_base.ConnectionHealthChecksTests."
  102. "test_health_checks_enabled",
  103. "backends.base.test_base.ConnectionHealthChecksTests."
  104. "test_set_autocommit_health_checks_enabled",
  105. },
  106. }
  107. )
  108. if self.uses_server_side_binding:
  109. skips.update(
  110. {
  111. "The actual query cannot be determined for server side bindings": {
  112. "backends.base.test_base.ExecuteWrapperTests."
  113. "test_wrapper_debug",
  114. }
  115. },
  116. )
  117. return skips
  118. @cached_property
  119. def django_test_expected_failures(self):
  120. expected_failures = set()
  121. if self.uses_server_side_binding:
  122. expected_failures.update(
  123. {
  124. # Parameters passed to expressions in SELECT and GROUP BY
  125. # clauses are not recognized as the same values when using
  126. # server-side binding cursors (#34255).
  127. "aggregation.tests.AggregateTestCase."
  128. "test_group_by_nested_expression_with_params",
  129. }
  130. )
  131. if not is_psycopg3:
  132. expected_failures.update(
  133. {
  134. # operator does not exist: bigint[] = integer[]
  135. "postgres_tests.test_array.TestQuerying.test_gt",
  136. "postgres_tests.test_array.TestQuerying.test_in",
  137. "postgres_tests.test_array.TestQuerying.test_lt",
  138. }
  139. )
  140. return expected_failures
  141. @cached_property
  142. def uses_server_side_binding(self):
  143. options = self.connection.settings_dict["OPTIONS"]
  144. return is_psycopg3 and options.get("server_side_binding") is True
  145. @cached_property
  146. def max_query_params(self):
  147. if self.uses_server_side_binding:
  148. return 2**16 - 1
  149. return None
  150. @cached_property
  151. def prohibits_null_characters_in_text_exception(self):
  152. if is_psycopg3:
  153. return DataError, "PostgreSQL text fields cannot contain NUL (0x00) bytes"
  154. else:
  155. return ValueError, "A string literal cannot contain NUL (0x00) characters."
  156. @cached_property
  157. def introspected_field_types(self):
  158. return {
  159. **super().introspected_field_types,
  160. "PositiveBigIntegerField": "BigIntegerField",
  161. "PositiveIntegerField": "IntegerField",
  162. "PositiveSmallIntegerField": "SmallIntegerField",
  163. }
  164. @cached_property
  165. def is_postgresql_16(self):
  166. return self.connection.pg_version >= 160000
  167. @cached_property
  168. def is_postgresql_17(self):
  169. return self.connection.pg_version >= 170000
  170. @cached_property
  171. def is_postgresql_18(self):
  172. return self.connection.pg_version >= 180000
  173. supports_unlimited_charfield = True
  174. supports_any_value = property(operator.attrgetter("is_postgresql_16"))
  175. supports_virtual_generated_columns = property(
  176. operator.attrgetter("is_postgresql_18")
  177. )
  178. supports_uuid4_function_in_default = property(
  179. operator.attrgetter("supports_uuid4_function")
  180. )