Django

Code

Ticket #12234 (closed: fixed)

Opened 4 months ago

Last modified 3 months ago

db_index=True creates indexes which PostgreSQL may not be able to use

Reported by: ubernostrum Assigned to: jbronn
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: carljm Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 1 Patch needs improvement: 0

Description (Last modified by ubernostrum)

When a field is specified with db_index=True, or has an implicit index, Django uses a normal CREATE indexname ON table (column) statement to create the index in PostgreSQL. For queries using most operators this works, but if the column is text-based (e.g., TEXT or VARCHAR) and the locale of the database is other than 'C' -- e.g., if the database is created UTF-8 as recommended -- Postgres will not (and cannot) use this index for LIKE queries. Getting indexes for LIKE queries on these columns requires a second index. For example, given the following model (in an app named weblog):

class Entry(models.Model):
    title = models.CharField(max_length=255, db_index=True)
    pub_date = models.DateTimeField()
    body = models.TextField()

something like the following is needed to enable the index on title to be used for all query types:

CREATE INDEX "weblog_entry_title" ON "weblog_entry" ("title");
CREATE INDEX "weblog_entry_title_like" ON "weblog_entry" ("title" varchar_pattern_ops);

The second index, created with varchar_pattern_ops, will be used on LIKE queries. Similarly, for a TEXT column, a second index must be created using text_pattern_ops.

I'm not sure whether this affects databases other than Postgres.

Attachments

charfield_index_opclass_fix_v1.diff (2.2 kB) - added by jbronn on 12/03/09 13:30:37.
Add extra indexes to include operator class.
charfield_index_opclass_fix_v2.diff (2.1 kB) - added by jbronn on 12/17/09 12:32:37.
simplifed get_index_sql
charfield_index_opclass_fix_v3.diff (3.3 kB) - added by jbronn on 12/17/09 14:36:08.
Introspect on database type string rather than Field class; added docs to databases section.

Change History

11/17/09 10:31:11 changed by Alex

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • version changed from 1.1 to SVN.
  • needs_tests changed.
  • needs_docs changed.

11/17/09 10:33:00 changed by ubernostrum

  • description changed.

11/17/09 10:49:18 changed by carljm

  • cc set to carljm.

12/03/09 13:30:37 changed by jbronn

  • attachment charfield_index_opclass_fix_v1.diff added.

Add extra indexes to include operator class.

12/03/09 13:32:11 changed by jbronn

  • owner changed from nobody to jbronn.
  • needs_docs set to 1.
  • has_patch set to 1.
  • status changed from new to assigned.
  • needs_tests set to 1.

12/17/09 12:32:37 changed by jbronn

  • attachment charfield_index_opclass_fix_v2.diff added.

simplifed get_index_sql

12/17/09 14:36:08 changed by jbronn

  • attachment charfield_index_opclass_fix_v3.diff added.

Introspect on database type string rather than Field class; added docs to databases section.

12/19/09 02:19:38 changed by jbronn

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [11912]) Fixed #12234 -- Create additional indexes that use the appropriate operation class for PostgreSQL varchar and text columns when db_index=True.

12/19/09 02:24:20 changed by jbronn

(In [11913]) [1.1.X] Fixed #12234 -- Create additional indexes that use the appropriate operation class for PostgreSQL varchar and text columns when db_index=True.

Backport of r11912 from trunk.


Add/Change #12234 (db_index=True creates indexes which PostgreSQL may not be able to use)




Change Properties
Action