[Django] #30644: Some Postgres introspection methods don't filter on schema

30 views
Skip to first unread message

Django

unread,
Jul 17, 2019, 4:15:57 AM7/17/19
to django-...@googlegroups.com
#30644: Some Postgres introspection methods don't filter on schema
------------------------------------------+------------------------
Reporter: Georgi Yanchev | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30644>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 17, 2019, 4:16:14 AM7/17/19
to django-...@googlegroups.com
#30644: Some Postgres introspection methods don't filter on schema
--------------------------------+--------------------------------------

Reporter: Georgi Yanchev | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Georgi Yanchev):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:1>

Django

unread,
Jul 17, 2019, 4:16:50 AM7/17/19
to django-...@googlegroups.com
#30644: Some Postgres introspection methods don't filter on schema
--------------------------------+--------------------------------------

Reporter: Georgi Yanchev | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Georgi Yanchev):

* cc: Georgi Yanchev (added)
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:2>

Django

unread,
Jul 17, 2019, 4:20:36 AM7/17/19
to django-...@googlegroups.com
#30644: Some Postgres introspection methods don't filter on schema
-------------------------------------+-------------------------------------

Reporter: Georgi Yanchev | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Georgi Yanchev):

* component: Uncategorized => Database layer (models, ORM)


--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:3>

Django

unread,
Jul 17, 2019, 4:22:48 AM7/17/19
to django-...@googlegroups.com
#30644: Some Postgres introspection methods don't filter on schema
-------------------------------------+-------------------------------------

Reporter: Georgi Yanchev | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Postgres schemas | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Georgi Yanchev):

* keywords: => Postgres schemas


--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:4>

Django

unread,
Jul 17, 2019, 5:44:17 AM7/17/19
to django-...@googlegroups.com
#30644: Postgres introspection don't filter on visible objects.
-------------------------------------+-------------------------------------
Reporter: Georgi Yanchev | Owner: Georgi
Type: | Yanchev
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: Postgres schemas | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => assigned
* version: 2.2 => master
* owner: nobody => Georgi Yanchev
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

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.

[https://github.com/django/django/pull/11511 PR]

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

--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:5>

Django

unread,
Jul 22, 2019, 2:42:10 AM7/22/19
to django-...@googlegroups.com
#30644: Postgres introspection don't filter on visible objects.
-------------------------------------+-------------------------------------
Reporter: Georgi Yanchev | Owner: Georgi
Type: | Yanchev
Cleanup/optimization | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: Postgres schemas | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"c6581a40be3bb4c1e13861f0adbb3fe01f09107f" c6581a40]:
{{{
#!CommitTicketReference repository=""
revision="c6581a40be3bb4c1e13861f0adbb3fe01f09107f"
Fixed #30644 -- Made introspection use pg_table_is_visible() instead of
filtering by public schema on PostgreSQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30644#comment:6>

Reply all
Reply to author
Forward
0 new messages