Repository navigation
perf(jdbc): avoid cubic regex backtracking when finding primary keys - #1490
Merged
Merged
Conversation
Collaborator
|
nice work, thanks |
gotson
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PrimaryKeyFinder, which backs getPrimaryKeys, getExportedKeys and getImportedKeys, runs PK_NAMED_PATTERN over the table's CREATE statement from sqlite_schema with find(), and that pattern backtracks cubically whenever the DDL has no named primary key. Root cause: find() retries the leading
.*from every offset, and from every CONSTRAINT keyword the lazy name group rescans to the end of the string. I found it while timing the metadata regexes against long schemas: a 10 KB CREATE TABLE with 400 CHECK constraints and a column-level primary key spends about 125 s in the regex (2.5 KB takes 2 s, 5 KB takes 13 s), so a tool calling getPrimaryKeys on a database file it did not create can be pinned on a single table, and ordinary schemas with a lot of checks are already noticeably slow.Both patterns are now anchored with
\A, which returns the same groups because a DOTALL leading.*matches from index 0 whenever it matches anywhere. The name group can no longer cross another CONSTRAINT keyword, which the greedy.*already rules out, and the DDL is cut after its last)because both patterns need one after PRIMARY KEY. With that the same 10 KB DDL takes 0.16 ms and 1 MB takes about 17 ms; the old and new patterns gave identical groups across 1M random DDL fragments, and a 100k character constraint name does not overflow the stack on JDK 8, 11 or 21. The new DBMetaDataTest case times out after 10 s on master and passes with the fix. I left the unused copies of these patterns in CoreDatabaseMetaData alone.