Skip to content

Commit

Permalink
Merge pull request #384 from tonioo/master
Browse files Browse the repository at this point in the history
Fix query performance when retrieving constraints (avoid full scan).
  • Loading branch information
bernardopires committed Sep 23, 2016
2 parents baf6046 + e431105 commit eceab7a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tenant_schemas/postgresql_backend/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ class DatabaseSchemaIntrospection(DatabaseIntrospection):
AND n.nspname = %(schema)s
"""

_get_constaints_query = """
_get_constraints_query = """
SELECT
kc.constraint_name,
kc.column_name,
c.constraint_type,
array(SELECT table_name::text || '.' || column_name::text
FROM information_schema.constraint_column_usage
WHERE constraint_name = kc.constraint_name)
WHERE constraint_name = kc.constraint_name
AND table_schema = kc.table_schema)
FROM information_schema.key_column_usage AS kc
JOIN information_schema.table_constraints AS c ON
kc.table_schema = c.table_schema AND
Expand Down Expand Up @@ -240,7 +241,7 @@ def get_constraints(self, cursor, table_name):

# Loop over the key table, collecting things as constraints
# This will get PKs, FKs, and uniques, but not CHECK
cursor.execute(self._get_constaints_query, {
cursor.execute(self._get_constraints_query, {
'schema': self.connection.schema_name,
'table': table_name,
})
Expand Down

0 comments on commit eceab7a

Please sign in to comment.