Opened 5 years ago

Closed 5 years ago

#30644 closed Cleanup/optimization (fixed)

Postgres introspection don't filter on visible objects.

Reported by: Georgi Yanchev Owned by: Georgi Yanchev
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: Postgres schemas
Cc: Georgi Yanchev Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Several instances of my Django application share the same Postgres database (each in own schema). To specify which schema to use I have in my Django settings:

DATABASES[default']['OPTIONS'] = {
            'options': '-c search_path={}'.format(get_env('POSTGRES_SCHEMA', 'public')),
        }

The problem is that migrations fail, because methods like get_constraints return constraints from other schemas: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L145

When I look deeper into that code, it looks that there is a mess with schemas filtering.

  1. The first SQL query in get_constraints will use hardcoded schema name public: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L175
  2. The second SQL in the same method doesn't filter by schema name, so it will return entries from all schemas: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L214
  3. get_table_list properly filters by using pg_catalog.pg_table_is_visible.
  4. get_sequences and get_relations will return entries from all schemas.

Change History (6)

comment:1 by Georgi Yanchev, 5 years ago

Type: UncategorizedBug

comment:2 by Georgi Yanchev, 5 years ago

Cc: Georgi Yanchev added
Has patch: set

comment:3 by Georgi Yanchev, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:4 by Georgi Yanchev, 5 years ago

Keywords: Postgres schemas added

comment:5 by Mariusz Felisiak, 5 years ago

Owner: changed from nobody to Georgi Yanchev
Status: newassigned
Summary: Some Postgres introspection methods don't filter on schemaPostgres introspection don't filter on visible objects.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Version: 2.2master

Thanks for this report. I agree that we can use pg_table_is_visible and remove filtering by public schema in get_sequences() and get_constraints(). Django doesn't support schema so it's rather a cleanup to me.

PR

Comment: get_sequences() will return sequences only from public schema.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In c6581a40:

Fixed #30644 -- Made introspection use pg_table_is_visible() instead of filtering by public schema on PostgreSQL.

Note: See TracTickets for help on using tickets.
Back to Top