[Django] #33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with multiple fields.

13 views
Skip to first unread message

Django

unread,
Jun 9, 2022, 6:30:48 AM6/9/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
-------------------------------------+-------------------------------------
Reporter: sumebrius | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Tested with Postgresql backend, Django versions 3.2 & 4.0

Defining an Index in the Meta of a model ignores the
DEFAULT_INDEX_TABLESPACE setting if multiple fields are used for the
index.
The setting is honoured if only a single field defines the index.

Example case, with a postgresql DB defined in settings, as well as:
{{{
DEFAULT_TABLESPACE = 'data_ts'
DEFAULT_INDEX_TABLESPACE = 'index_ts'
}}}

The below model definition:
{{{
class MyModel(models.Model):
foo = models.CharField(max_length=10)
bar = models.CharField(max_length=10)

class Meta:
indexes = [
models.Index(fields=['foo']),
models.Index(fields=['foo', 'bar']),
]
}}}
generates this output from management command {{{sqlmigrate}}} run on the
produced migration:
{{{
BEGIN;
--
-- Create model MyModel
--
CREATE TABLE "cbcap_mymodel" ("id" bigserial NOT NULL PRIMARY KEY USING
INDEX TABLESPACE "index_ts", "foo" varchar(10) NOT NULL, "bar" varchar(10)
NOT NULL) TABLESPACE "data_ts";
--
-- Create index cbcap_mymod_foo_f01529_idx on field(s) foo of model
mymodel
--
CREATE INDEX "cbcap_mymod_foo_f01529_idx" ON "cbcap_mymodel" ("foo")
TABLESPACE "index_ts";
--
-- Create index cbcap_mymod_foo_44bcd6_idx on field(s) foo, bar of model
mymodel
--
CREATE INDEX "cbcap_mymod_foo_44bcd6_idx" ON "cbcap_mymodel" ("foo",
"bar") TABLESPACE "data_ts";
COMMIT;
}}}
Note the 'data_ts' tablespace is used for the second index

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

Django

unread,
Jun 9, 2022, 10:50:10 AM6/9/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+------------------------------------
Reporter: Bruce Cutler | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

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

* component: Database layer (models, ORM) => Migrations
* stage: Unreviewed => Accepted


Comment:

Thank you for your report.

There's effectively a discrepancy between
[https://docs.djangoproject.com/en/4.0/topics/db/tablespaces/#declaring-
tablespaces-for-indexes what's documented] and how
[https://github.com/django/django/blob/e96320c91724830034033a9cb8afd9cf8c11e2fd/django/db/backends/base/schema.py#L1305-L1313
it's implemented for indexes including multiple fields or solely
expressions].

Would you be interested in submitting a patch that makes
`_get_index_tablespace_sql` consider `DEFAULT_INDEX_TABLESPACE` before
`model._meta.db_tablespace` if the former is defined?

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

Django

unread,
Jun 12, 2022, 11:38:09 PM6/12/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+----------------------------------------
Reporter: Bruce Cutler | Owner: Bruce Cutler
Type: Bug | Status: assigned

Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Bruce Cutler):

* owner: nobody => Bruce Cutler
* status: new => assigned


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

Django

unread,
Jun 13, 2022, 12:03:34 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+----------------------------------------
Reporter: Bruce Cutler | Owner: Bruce Cutler
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1
* needs_tests: 0 => 1


Comment:

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

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

Django

unread,
Jun 13, 2022, 12:09:59 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+----------------------------------------
Reporter: Bruce Cutler | Owner: Bruce Cutler
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------

Comment (by Bruce Cutler):

[[https://github.com/django/django/pull/15770|PR on github]] raised for
this patch.

No tests were created for the patch, as 3 existing tests already fail
prior to the changes (and pass since) when DEFAULT_INDEX_TABLESPACE is
specified in settings:
{{{
======================================================================
FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['shortcut', 'isbn'])
...
AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("shortcut", "isbn")'

======================================================================
FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['title', 'author'])
...
AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("title", "author")'

======================================================================
FAIL: test_func_with_tablespace (model_indexes.tests.IndexesTests)
...
AssertionError: '"idx_def_tbsp"' not found in 'CREATE INDEX
"functional_no_tbls" ON "model_indexes_book" ((LOWER("shortcut")) DESC)'
}}}

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

Django

unread,
Jun 13, 2022, 12:13:56 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+----------------------------------------
Reporter: Bruce Cutler | Owner: Bruce Cutler
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Bruce Cutler):

* needs_tests: 1 => 0


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

Django

unread,
Jun 13, 2022, 1:07:55 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
------------------------------+----------------------------------------
Reporter: Bruce Cutler | Owner: Bruce Cutler
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+----------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:4 Bruce Cutler]:


> [[https://github.com/django/django/pull/15770|PR on github]] raised for
this patch.
>
> No tests were created for the patch, as 3 existing tests already fail
prior to the changes (and pass since) when DEFAULT_INDEX_TABLESPACE is
specified in settings:
> {{{
> ======================================================================
> FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['shortcut', 'isbn'])
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("shortcut", "isbn")'
>
> ======================================================================
> FAIL: test_db_tablespace (model_indexes.tests.IndexesTests) [<object
object at 0x7f8bcd80fa00>] (fields=['title', 'author'])
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'create index "" on
"model_indexes_book" ("title", "author")'
>
> ======================================================================
> FAIL: test_func_with_tablespace (model_indexes.tests.IndexesTests)
> ...
> AssertionError: '"idx_def_tbsp"' not found in 'CREATE INDEX
"functional_no_tbls" ON "model_indexes_book" ((LOWER("shortcut")) DESC)'
> }}}

Unfortunately it's not covered by CI because we set the same tablespace to
the `DEFAULT_TABLESPACE` and `DEFAULT_INDEX_TABLESPACE`. Overriding
`DEFAULT_TABLESPACE` to `None` should make it covered.

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

Django

unread,
Jun 13, 2022, 1:17:26 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
-------------------------------------+-------------------------------------

Reporter: Bruce Cutler | Owner: Bruce
| Cutler
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:7>

Django

unread,
Jun 13, 2022, 2:47:01 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
-------------------------------------+-------------------------------------
Reporter: Bruce Cutler | Owner: Bruce
| Cutler
Type: Bug | Status: closed
Component: Migrations | Version: 4.0
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
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:"de1c8320cedee5d4f91b153a7fe82eb63876470f" de1c8320]:
{{{
#!CommitTicketReference repository=""
revision="de1c8320cedee5d4f91b153a7fe82eb63876470f"
Fixed #33773 -- Made Index with multiple fields respect
DEFAULT_INDEX_TABLESPACE.

Thanks to Simon Charette for locating where issue lay.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:8>

Django

unread,
Jun 13, 2022, 2:50:06 AM6/13/22
to django-...@googlegroups.com
#33773: DEFAULT_INDEX_TABLESPACE setting is ignored for Indexes defined with
multiple fields.
-------------------------------------+-------------------------------------
Reporter: Bruce Cutler | Owner: Bruce
| Cutler
Type: Bug | Status: closed
Component: Migrations | Version: 4.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"a7f398ae597536d0cee67cb525f165d87ca2fe49" a7f398ae]:
{{{
#!CommitTicketReference repository=""
revision="a7f398ae597536d0cee67cb525f165d87ca2fe49"
[4.1.x] Fixed #33773 -- Made Index with multiple fields respect
DEFAULT_INDEX_TABLESPACE.

Thanks to Simon Charette for locating where issue lay.

Backport of de1c8320cedee5d4f91b153a7fe82eb63876470f from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33773#comment:9>

Reply all
Reply to author
Forward
0 new messages