Skip to content

ci: check query plans of the PostgreSQL database tests - #66854

Open
juliusknorr wants to merge 3 commits into
masterfrom
ci/explain-seqscan-check
Open

juliusknorr wants to merge 3 commits into
masterfrom
ci/explain-seqscan-check

Conversation

@juliusknorr

@juliusknorr juliusknorr commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Since v33, getByEmail() searches LOWER("indexed") = ?, so the oc_preferences index only narrows down to all email rows. CI didn't notice: results and query count stayed the same.

  • Fix: emails are stored lowercase, so UserConfig lowercases the search value for settings/email and compares indexed directly. A case-insensitive search on other indexed keys logs a warning
  • CI check: the PostgreSQL 18 DB test job records each distinct query and its caller, then runs EXPLAIN on them with seq scans disabled. New queries that filter a large table in a way no index can serve (a remaining seq scan, or a function around an indexed column) fail the job. Known ones are listed in tests/data/query-plan-baseline.json

Without the fix, the check reports:

preferences | OC\User\Manager::getByEmail | SELECT "userid" FROM "oc_preferences" WHERE ("appid" = :dcValue1) AND ("configkey" = :dcValue2) AND (LOWER("indexed") = :dcValue3)

Limits: the check only sees queries that DB tests run, and it doesn't flag an OR with a non-indexed branch.

On a full test:db run (6372 tests, 3044 distinct queries) the check flags one existing query: FilesByType in OpenMetrics filters oc_filecache on path LIKE 'files/%' (from #58052), which reads the whole filecache on every cache refresh (30 min TTL). It's in the baseline for now, to be followed up on #58052.

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Emails are stored lowercase, so a case-insensitive search can lowercase
the value and compare the indexed column directly instead of LOWER(indexed).
Case-insensitive searches on other indexed keys log a warning, as they
cannot use the index.

Signed-off-by: Julius Knorr <jus@bitgrid.net>
Assisted-by: ClaudeCode:claude-opus-5-5
Records each distinct query of the test run with its caller and explains
it afterwards with sequential scans disabled. Fails on new queries that
filter a large table in a way no index can serve, such as a function
around an indexed column.

Signed-off-by: Julius Knorr <jus@bitgrid.net>
Assisted-by: ClaudeCode:claude-opus-5-5
Also ignores queries issued from PHPUnit's base TestCase, e.g. in setUp.

Signed-off-by: Julius Knorr <jus@bitgrid.net>
Assisted-by: ClaudeCode:claude-opus-5-5
@juliusknorr
juliusknorr marked this pull request as ready for review November 20, 2026 11:00
@juliusknorr
juliusknorr requested a review from a team as a code owner November 20, 2026 11:00
@juliusknorr
juliusknorr requested review from leftybournes and salmart-dev and removed request for a team November 20, 2026 11:00
@juliusknorr juliusknorr added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Nov 20, 2026
@juliusknorr juliusknorr added this to the Nextcloud 36 milestone Nov 20, 2026
@juliusknorr
juliusknorr requested a review from samin-z November 20, 2026 11:03
@github-actions

Copy link
Copy Markdown
Contributor

Possible performance regression detected

Show Output
8 queries added

= /remote.php/dav/files/test
= /remote.php/dav/files/test/test.txt
= /remote.php/dav/files/test/many_files
= /remote.php/dav/files/test/new_file.txt
≠ /remote.php/dav/files/test/new_file.txt with 8 queries added
  - UPDATE "oc_filecache" SET "size" = :dcValue4 WHERE ("fileid" = :dcValue1) AND ("storage" = :dcValue2) AND (("size" <> :dcValue3) OR ("size" IS NULL))
  - UPDATE "oc_filecache" SET "size" = :dcValue4 WHERE ("fileid" = :dcValue1) AND ("storage" = :dcValue2) AND (("size" <> :dcValue3) OR ("size" IS NULL))
  - SELECT "path" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  - SELECT "path" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  + SELECT "storage", "path", "mimetype" FROM "oc_filecache" WHERE "fileid" = :dcValue1
  + SELECT "storage_id", "root_id", "user_id", "mount_point", "mount_id", "f"."path", "mount_provider_class" FROM "oc_mounts" "m" INNER JOIN "oc_filecache" "f" ON "m"."root_id" = "f"."fileid" WHERE ("m"."storage_id" = :dcValue1) AND ("f"."storage" = :dcValue2) AND (("f"."fileid" = :dcValue3) OR ("f"."path" = '') OR (("f"."path" || :dcValue4) = SUBSTR(:dcValue5, :dcValue6, LENGTH("f"."path") + :dcValue7))) AND ("user_id" = :dcValue8)
  + SELECT "filecache"."fileid", "storage", "path", "path_hash", "filecache"."parent", "filecache"."name", "mimetype", "mimepart", "size", "mtime", "storage_mtime", "encrypted", "filecache"."etag", "filecache"."permissions", "checksum", "unencrypted_size", "metadata_etag", "creation_time", "upload_time", "meta"."json" AS "meta_json", "meta"."sync_token" AS "meta_sync_token" FROM "oc_filecache" "filecache" LEFT JOIN "oc_filecache_extended" "fe" ON "filecache"."fileid" = "fe"."fileid" LEFT JOIN "oc_files_metadata" "meta" ON "filecache"."fileid" = "meta"."file_id" WHERE ("filecache"."fileid" = :dcValue1) AND ("storage" = :dcValue2)
  + DELETE FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("fileid" = :dcValue2)
  + DELETE FROM "oc_filecache_extended" WHERE "fileid" = :dcValue1
  + SELECT "file_id", "json", "sync_token" FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + SELECT "file_id", "json", "sync_token" FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + DELETE FROM "oc_files_metadata" WHERE "file_id" IN (:dcValue1)
  + DELETE FROM "oc_files_metadata_index" WHERE "file_id" IN (:dcValue1)
  + SELECT "fileid" FROM "oc_filecache" WHERE ("storage" = :dcValue1) AND ("path_hash" = :dcValue2)
  + UPDATE "oc_file_locks" SET "lock" = :dcValue1, "ttl" = :dcValue2 WHERE ("key" = :dcValue3) AND ("lock" = :dcValue4)
  + SELECT "filecache"."fileid", "storage", "path", "path_hash", "filecache"."parent", "filecache"."name", "mimetype", "mimepart", "size", "mtime", "storage_mtime", "encrypted", "filecache"."etag", "filecache"."permissions", "checksum", "unencrypted_size", "metadata_etag", "creation_time", "upload_time", "meta"."json" AS "meta_json", "meta"."sync_token" AS "meta_sync_token" FROM "oc_filecache" "filecache" LEFT JOIN "oc_filecache_extended" "fe" ON "filecache"."fileid" = "fe"."fileid" LEFT JOIN "oc_files_metadata" "meta" ON "filecache"."fileid" = "meta"."file_id" WHERE ("path_hash" = :dcValue1) AND ("storage" = :dcValue2)
= ./occ profiler:list

@juliusknorr

Copy link
Copy Markdown
Member Author

I went through the slow-query fixes merged on master over the past year to see what this check would have caught. If any of them were reverted, it would only have flagged #59650. Most of the others are query-count problems or index-usage patterns the current rules don't cover. Follow-ups I'm planning, as separate PRs so this one stays small:

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants