Skip to content

fix: Count does not work with MasterKey on Postgres #9220

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

Closed
wants to merge 9 commits into from

Conversation

dplewis
Copy link
Member

@dplewis dplewis commented Jul 18, 2024

Pull Request

Issue

Count operations are expensive so for Postgres we use estimated counts. The method to updated these estimated counts is called valcuuming which can happen at any time. If it doesn't happen you end up with 0 for your count. This has been a problem specifically for the Parse Dashboard.

https://www.postgresql.org/docs/current/routine-vacuuming.html

Closes: #8286
Closes: #8502

Approach

  • Whenever an object is created or deleted update the estimated count
  • Remove duplicate tests
  • Enable tests for PG

Tasks

  • Add tests
  • Add changes to documentation (guides, repository pages, code comments)

Copy link

Thanks for opening this pull request!

Copy link

codecov bot commented Jul 18, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.74%. Comparing base (4d86ace) to head (b62f7e7).

Files Patch % Lines
...dapters/Storage/Postgres/PostgresStorageAdapter.js 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            alpha    #9220      +/-   ##
==========================================
- Coverage   93.75%   93.74%   -0.01%     
==========================================
  Files         184      184              
  Lines       14675    14677       +2     
==========================================
+ Hits        13758    13759       +1     
- Misses        917      918       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dplewis dplewis requested a review from a team July 18, 2024 02:00
Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR title doesn't seem to fit the PR description. Is this about master key or about the count op not working for postgres in general? If these are 2 unrelated issues, then they'd require 2 PRs.

@dplewis
Copy link
Member Author

dplewis commented Jul 19, 2024

This PR fixes estimated counts on Postgres, which happens when you useMasterKey with count.

@dplewis dplewis requested a review from mtrezza July 19, 2024 15:11
@mtrezza
Copy link
Member

mtrezza commented Jul 20, 2024

So this counts issue only occurs when using master key?

@dplewis dplewis requested a review from mtrezza July 20, 2024 14:11
async updateEstimatedCount(className: string) {
return this._client.none('ANALYZE $1:name', [className]);
return this._client.none('ANALYZE $1:name', [className]).catch(() => {});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like you are swallowing the error, do you think it should be logged as an error or warning? Especially since this method isn’t just used for tests anymore. This will help track down if the method stops working and can’t update the estimate in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it. Maybe a user doesn't have permission to run ANALYZE

@@ -2048,9 +2048,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
if (where.pattern.length > 0 || !estimate) {
qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
} else {
await this.updateEstimatedCount(className);
Copy link
Contributor

@cbaker6 cbaker6 Jul 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, this else statement usually gets hit by the ParseDashboard and maybe the use of the masterKey. Since the dashboard heavily relies on count, do you anticipate a noticeable performance hit whenever a database manager (or multiple managers) are logged into the Dashboard?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the dashboard heavily relies on count

The dashboard relies heavily on count on the initial browser load. I've never personally used ANALYZE on a large dataset before so I'm not sure of the performance.

For the dashboard, I think we can add some documentation to recommend users to enable autovalcuum and its threshold if they are using Postgres.

Copy link
Member

@mtrezza mtrezza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing from #9220 (comment).

The question of the perf impact still remains. If an app is currently calling count many times, even though that may not be not efficient, we run ANALYZE more often than needed. We may even need to consider this a breaking change, due to its possible performance impact.

More generally, I think that queries and DB maintenance are two different realms and therefore I'm still not sure we should fuse them together like this.

I'm beginning to suspect that the solution to this is to advise developers to properly set up and maintain the DB, maybe add a docs mention regarding auto vacuum.

@dplewis
Copy link
Member Author

dplewis commented Jul 20, 2024

I'm beginning to suspect that the solution to this is to advise developers to properly set up and maintain the DB, maybe add a docs mention regarding auto vacuum.

I agree

@dplewis dplewis closed this Jul 20, 2024
@dplewis dplewis deleted the estimated-count-pg branch July 20, 2024 16:29
@mtrezza
Copy link
Member

mtrezza commented Jul 20, 2024

@dplewis if there is any other change in this PR that should be merged, please feel free to re-open.

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.

Count Objects not working in some classes Count does not work with masterKey on postgres
3 participants