New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #28107 -- Added DatabaseFeatures.allows_group_by_selected_pks_on_model() to allow enabling optimization for unmanaged models. #11606
Conversation
147f93d
to
655e305
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tasssadar Thanks for this patch 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tasssadar Thanks for updates 👍 . I left few more comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like the right approach but here's a few comments
- The current code is not schema aware and could break with usage of
SET search_path
and friends or namedspaceddb_table
(e.g.Meta.db_table = '"schema"."table"
. - We should try to find a more appropriate name than ordinary to categorize such tables.
- Using
Model._meta
as a cache store is a big no-no IMO because it doesn't take connection aliases into account. Results should be cached using a(alias, schema, table_name)
namespace.
Given the rare cases when this is useful and the complexity of implementing a bullet proof caching mechanism I'd favor simply exposing the allows_group_by_selected_pks_on_model
hook and letting developers override it they deem it necessary.
Thanks for your comments!
I copied the WHERE partly from
That's the nomenclature used by Postgres docs, https://www.postgresql.org/docs/9.3/catalog-pg-class.html , that's why I put it everywhere (instead of just
Where should this hook be? I can't see a way to override connection features from the user app without creating a new DB engine :/ |
Hey @Tasssadar
TIL, I didn't know that was the name used by Postgres. Thanks for pointing that out. Reading the documentation makes me wonder if the optimization works for foreign and partitioned tables though. I guess that's another reason why this should be left to the user.
That would effectively require the user to subclass the Postgres db engine which is not as daunting as it might seems. |
I have removed the autodetection and just added a hook that just returns The I tried writing the subclassed engine, and it's not bad: from django.db.backends.postgresql import base, features
class DatabaseFeatures(features.DatabaseFeatures):
def allows_group_by_selected_pks_on_model(self, model):
return True
class DatabaseWrapper(base.DatabaseWrapper):
features_class = DatabaseFeatures I'm not sure how you're merging PRs, there's a lot of commits here now with useless code, I can rebase it to master if you wish. |
Hello, any followup please? The tests failed, but I'm fairly sure it was something in the test environment wrong, not the code. |
@Tasssadar Thanks. I rebased from master, squashed commits, restored one test and added release notes. |
1e1fd22
to
384c294
Compare
Rebased due to conflicts, the failing test is caused by ed2d411 and not this PR. |
@Tasssadar Yes my mistake, I will fix this in a minute. |
…n_model() to allow enabling optimization for unmanaged models.
@Tasssadar @carltongibson I rebased from master, moved "Subclassing the built-in database backends" to a separate commit and rephrased it a bit. IMO we're ready to merge 🎊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, great stuff @felixxm. That reads very nicely.
First, you have to create a new directory with a
base
module in it...
Smooth 🙂
👍
@Tasssadar Thanks, welcome aboard ⛵ @carltongibson Thanks for checking 👍 |
Thank you! This first release with this included will be 3.0 in December, right? |
Yes. |
No description provided.