Opened 6 years ago
Closed 6 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.
- The first SQL query in
get_constraintswill use hardcoded schema namepublic: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L175 - 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
get_table_listproperly filters by usingpg_catalog.pg_table_is_visible.get_sequencesandget_relationswill return entries from all schemas.
Change History (6)
comment:1 by , 6 years ago
| Type: | Uncategorized → Bug |
|---|
comment:2 by , 6 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:3 by , 6 years ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|
comment:4 by , 6 years ago
| Keywords: | Postgres schemas added |
|---|
comment:5 by , 6 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
| Summary: | Some Postgres introspection methods don't filter on schema → Postgres introspection don't filter on visible objects. |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
| Version: | 2.2 → master |
Thanks for this report. I agree that we can use
pg_table_is_visibleand remove filtering bypublicschema inget_sequences()andget_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.