Discussion:
PostgreSQL Partial Indexes package
Mattias Linnap
2017-10-07 08:56:00 UTC
Permalink
Hi django-developers,

I have written a package that implements PostgreSQL and SQLite partial
indexes on top of the new class-based
indexes: https://github.com/mattiaslinnap/django-partial-index
The most common use case is partial unique constraints, but I have a few
projects where non-unique partial indexes have turned out useful as well.

I have a few questions on how to continue with this:

1. Right now the "where condition" expression is provided as a string, and
has to be different for PostgreSQL and SQLite in some common cases (for
example boolean literals). Is there a good abstraction for SQL expressions
somewhere in Django internals that I could use instead, something similar
to Q-expressions perhaps? In particular, to add validate_unique() support
to ModelForms, I would need to be able to extract all fields that are
mentioned in the where condition.
2. I've seen mentions of a "check constraints" support being in development
(https://github.com/django/django/pull/7615). Will that include partial
unique constraints, or is just for the per-column checks?
3. If separate, then it would be nice to one day get partial indexes merged
into the contrib.postgres package. Do you have any suggestions on what
needs to happen before that - more test coverage, more contributors, more
users, or similar?

Best,

Mattias
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/2f9b5333-8e6d-4c0f-9bd3-ae1cf79397c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tim Allen
2017-10-08 11:38:42 UTC
Permalink
I would love to see partial indexes supported. Great work! As far as
databases with Django support:

- PostgreSQL supports partial indexes
- SQLite supports partial indexes
- SQL Server supports them, called "filtered indexes"
- Oracle: Sort of supports
them: https://blog.jooq.org/2017/01/18/how-to-emulate-partial-indexes-in-oracle/
- MySQL/Maria/Drizzle: no support, AFAIK.

Would we want to build partial indexes in for all databases, with the
caveat that they would be ignored on MySQL and perhaps Oracle? In those
cases, would be default to a full index?

Regards,

Tim
Post by Mattias Linnap
Hi django-developers,
I have written a package that implements PostgreSQL and SQLite partial
https://github.com/mattiaslinnap/django-partial-index
The most common use case is partial unique constraints, but I have a few
projects where non-unique partial indexes have turned out useful as well.
1. Right now the "where condition" expression is provided as a string, and
has to be different for PostgreSQL and SQLite in some common cases (for
example boolean literals). Is there a good abstraction for SQL expressions
somewhere in Django internals that I could use instead, something similar
to Q-expressions perhaps? In particular, to add validate_unique() support
to ModelForms, I would need to be able to extract all fields that are
mentioned in the where condition.
2. I've seen mentions of a "check constraints" support being in
development (https://github.com/django/django/pull/7615). Will that
include partial unique constraints, or is just for the per-column checks?
3. If separate, then it would be nice to one day get partial indexes
merged into the contrib.postgres package. Do you have any suggestions on
what needs to happen before that - more test coverage, more contributors,
more users, or similar?
Best,
Mattias
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/df13ff66-8033-4f91-b49a-081bfa28db00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Adam Johnson
2017-10-08 13:01:45 UTC
Permalink
It looks to me that the same trick on Oracle can be used on MySQL/MariaDB -
indexing a generated column. There's a comment on
https://mariadb.com/kb/en/library/generated-columns/ to that effect.

P.S. Drizzle is long dead, the site isn't even up any more :)
Post by Tim Allen
I would love to see partial indexes supported. Great work! As far as
- PostgreSQL supports partial indexes
- SQLite supports partial indexes
- SQL Server supports them, called "filtered indexes"
- Oracle: Sort of supports them: https://blog.jooq.org/
2017/01/18/how-to-emulate-partial-indexes-in-oracle/
- MySQL/Maria/Drizzle: no support, AFAIK.
Would we want to build partial indexes in for all databases, with the
caveat that they would be ignored on MySQL and perhaps Oracle? In those
cases, would be default to a full index?
Regards,
Tim
Post by Mattias Linnap
Hi django-developers,
I have written a package that implements PostgreSQL and SQLite partial
indexes on top of the new class-based indexes: https://github.com/ma
ttiaslinnap/django-partial-index
The most common use case is partial unique constraints, but I have a few
projects where non-unique partial indexes have turned out useful as well.
1. Right now the "where condition" expression is provided as a string,
and has to be different for PostgreSQL and SQLite in some common cases (for
example boolean literals). Is there a good abstraction for SQL expressions
somewhere in Django internals that I could use instead, something similar
to Q-expressions perhaps? In particular, to add validate_unique() support
to ModelForms, I would need to be able to extract all fields that are
mentioned in the where condition.
2. I've seen mentions of a "check constraints" support being in
development (https://github.com/django/django/pull/7615). Will that
include partial unique constraints, or is just for the per-column checks?
3. If separate, then it would be nice to one day get partial indexes
merged into the contrib.postgres package. Do you have any suggestions on
what needs to happen before that - more test coverage, more contributors,
more users, or similar?
Best,
Mattias
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/
msgid/django-developers/df13ff66-8033-4f91-b49a-
081bfa28db00%40googlegroups.com
<https://groups.google.com/d/msgid/django-developers/df13ff66-8033-4f91-b49a-081bfa28db00%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Adam
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM0qxu7aPuRGy1yGUgG6FJbNHj%2Bnt3PyjY3FPnfCRuMGjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Ashley Waite
2017-10-20 11:37:14 UTC
Permalink
I did a similar thing (but only tested in postgres) a while back, using Q's
/ filter clause to generate the where clause.

Perhaps with our powers combined we're close to a generalised
implementation?

https://github.com/ashleywaite/django-more/blob/master/django_more/indexes.py
Post by Mattias Linnap
Hi django-developers,
I have written a package that implements PostgreSQL and SQLite partial
https://github.com/mattiaslinnap/django-partial-index
The most common use case is partial unique constraints, but I have a few
projects where non-unique partial indexes have turned out useful as well.
1. Right now the "where condition" expression is provided as a string, and
has to be different for PostgreSQL and SQLite in some common cases (for
example boolean literals). Is there a good abstraction for SQL expressions
somewhere in Django internals that I could use instead, something similar
to Q-expressions perhaps? In particular, to add validate_unique() support
to ModelForms, I would need to be able to extract all fields that are
mentioned in the where condition.
2. I've seen mentions of a "check constraints" support being in
development (https://github.com/django/django/pull/7615). Will that
include partial unique constraints, or is just for the per-column checks?
3. If separate, then it would be nice to one day get partial indexes
merged into the contrib.postgres package. Do you have any suggestions on
what needs to happen before that - more test coverage, more contributors,
more users, or similar?
Best,
Mattias
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/84f4431a-cdab-43c7-b96b-de4d2eb9805b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sergey Fursov
2018-11-17 10:51:45 UTC
Permalink
Hi everyone,

In our company, we also use the ability to define our own indexes, but with
the current implementation of base Index class, we basically need to copy
several methods entirely from it.
What do we want is implement UNIQUE indexes with the UPPER function wrapper
around field names [1].
This provides two benefits to us:
1) case-insensitive uniqueness of the field or the fields combination
2) quick filtering using iexact lookup, which uses UPPER(<field>) LIKE
<string> statement in SQL (at least for the Postgres)

I think, adding more flexibility in current indexes build logic would help
to implement and further support custom indexes implementations (like
django-partial-index <https://github.com/mattiaslinnap/django-partial-index>)
if we are not going to merge them in Django core or contrib.

[1] https://gist.github.com/GeyseR/363b4b9d2d1cd8c91d51ebfe9369a7ac
Post by Ashley Waite
I did a similar thing (but only tested in postgres) a while back, using
Q's / filter clause to generate the where clause.
Perhaps with our powers combined we're close to a generalised
implementation?
https://github.com/ashleywaite/django-more/blob/master/django_more/indexes.py
Post by Mattias Linnap
Hi django-developers,
I have written a package that implements PostgreSQL and SQLite partial
https://github.com/mattiaslinnap/django-partial-index
The most common use case is partial unique constraints, but I have a few
projects where non-unique partial indexes have turned out useful as well.
1. Right now the "where condition" expression is provided as a string,
and has to be different for PostgreSQL and SQLite in some common cases (for
example boolean literals). Is there a good abstraction for SQL expressions
somewhere in Django internals that I could use instead, something similar
to Q-expressions perhaps? In particular, to add validate_unique() support
to ModelForms, I would need to be able to extract all fields that are
mentioned in the where condition.
2. I've seen mentions of a "check constraints" support being in
development (https://github.com/django/django/pull/7615). Will that
include partial unique constraints, or is just for the per-column checks?
3. If separate, then it would be nice to one day get partial indexes
merged into the contrib.postgres package. Do you have any suggestions on
what needs to happen before that - more test coverage, more contributors,
more users, or similar?
Best,
Mattias
--
You received this message because you are subscribed to the Google Groups
"Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/84f4431a-cdab-43c7-b96b-de4d2eb9805b%40googlegroups.com
<https://groups.google.com/d/msgid/django-developers/84f4431a-cdab-43c7-b96b-de4d2eb9805b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAG-zk-CJnbuQASEmey6%2BWEewVrn8TPhxpUfv8TfzQEakAgwM%2Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...