Skip to content

Performance optimisation. Do not query database for existence of schema upon every call#411

Open
NielsKSchjoedt wants to merge 2 commits intoinfluitive:developmentfrom
AutoUncle:development
Open

Performance optimisation. Do not query database for existence of schema upon every call#411
NielsKSchjoedt wants to merge 2 commits intoinfluitive:developmentfrom
AutoUncle:development

Conversation

@NielsKSchjoedt
Copy link
Copy Markdown

@NielsKSchjoedt NielsKSchjoedt commented Mar 9, 2017

@mikecmpbll
Copy link
Copy Markdown
Collaborator

thanks! what are the implications of this?

Can you verify what happens if you straight up remove that line and try to connect to an invalid tenant?

@NielsKSchjoedt
Copy link
Copy Markdown
Author

NielsKSchjoedt commented Mar 10, 2017

It removes the absurd amount of calls to:

  SELECT COUNT(*)
     FROM pg_namespace
     WHERE nspname = ?

No exception is raised though, if you set the path to something like Apartment::Tenant.switch!('sdgjkdfkg') it just does, then you will of cause get an error like ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist, if you try to do User.find(1) afterwards. But I guess that is okay, given that we save all the calls

@caironoleto
Copy link
Copy Markdown
Contributor

For me, it's ok too but without a specific error can lead to a misunderstood about the problem.

Another way is to check the error to find a pattern like "relation (*) does not exist" and assume that error is because there is no schema. But is a weak solution.

@mikecmpbll
Copy link
Copy Markdown
Collaborator

mikecmpbll commented Mar 10, 2017

i was looking in to how this relates to the TenantNotFound exception, and oddly it looks as though the pg adapter won't raise TenantNotFound because ActiveRecord::StatementInvalid isn't being caught :/. I assume the intention here was to raise TenantNotFound like the abstract adapter's connect_to_new does.

@jhubert
Copy link
Copy Markdown

jhubert commented Sep 10, 2020

Fwiw, this is what it does:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants