Repository navigation
[PLTFRM-1887] Route SELECT FOR UPDATE queries to writable servers - #192
Conversation
There was a problem hiding this comment.
Pull request overview
Routes SELECT ... FOR UPDATE locking reads to writable database servers.
Changes:
- Detects
FOR UPDATEwithinSELECTqueries. - Classifies matching queries as writes for routing.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
7c7cff6 to
728f4f5
Compare
b417d13 to
d7b69b0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
db.php:348
- This comment check also treats comment markers inside SQL literals as real comments. For example,
SELECT '#', t.* FROM t FOR UPDATEmatches$line_comment, so the locking read falls through and is routed to a read replica. SQL comments and quoted strings need to be tokenized (or otherwise distinguished) before suppressing aFOR UPDATEmatch.
$line_comment = "/(?:--[ \\t]|#)[^\\r\\n]*\\bFOR\\s+UPDATE\\b$locking_modifier\\s*;?\\s*\\z/i";
db.php:347
- Requiring the locking clause to reach the absolute end misses valid queries with trailing comments, such as
SELECT * FROM t FOR UPDATE /* trace */orSELECT * FROM t FOR UPDATE -- trace. These then fall through as ordinary reads and can still reach a replica. Strip/skip trailing SQL comments safely before matching the clause.
$locking_clause = "/\\bFOR\\s+UPDATE\\b$locking_modifier\\s*;?\\s*\\z/i";
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
db.php:351
- The end-of-string anchor leaves valid locking reads classified as read-only whenever the clause is followed by syntax other than whitespace/
;. For example,(SELECT * FROM wp_posts FOR UPDATE)has its opening parenthesis removed above but retains the closing one, andSELECT * FROM wp_posts FOR UPDATE -- tracehas a valid trailing comment; both fall through tofalseand can be sent to a replica. Please detect the clause after masking SQL comments while allowing closing query-expression parentheses (and other query-block syntax), rather than requiringFOR UPDATEto be the raw terminal text.
$locking_clause = "/\\bFOR\\s+UPDATE\\b$of_clause$locking_modifier\\s*;?\\s*\\z/i";
$line_comment = "/(?:--[ \\t]|#)[^\\r\\n]*\\bFOR\\s+UPDATE\\b$of_clause$locking_modifier\\s*;?\\s*\\z/i";
There was a problem hiding this comment.
🟡 Changes recommended
The locking-read detection can miss valid clauses when quoted values contain comment markers or when clauses precede trailing comments or closing parentheses, allowing queries that require a writable server to reach replicas.
2 open issues
Review effort: Balanced
| $identifier = "$identifier_part(?:\\.$identifier_part)?"; | ||
| $of_clause = "(?:\\s+OF\\s+$identifier(?:\\s*,\\s*$identifier)*)?"; | ||
| $locking_modifier = '(?:\s+(?:NOWAIT|SKIP\s+LOCKED|WAIT\s+\d+(?:\.\d+)?))?'; | ||
| $locking_clause = "/\\bFOR\\s+UPDATE\\b$of_clause$locking_modifier\\s*;?\\s*\\z/i"; |
| $of_clause = "(?:\\s+OF\\s+$identifier(?:\\s*,\\s*$identifier)*)?"; | ||
| $locking_modifier = '(?:\s+(?:NOWAIT|SKIP\s+LOCKED|WAIT\s+\d+(?:\.\d+)?))?'; | ||
| $locking_clause = "/\\bFOR\\s+UPDATE\\b$of_clause$locking_modifier\\s*;?\\s*\\z/i"; | ||
| $line_comment = "/(?:--[ \\t]|#)[^\\r\\n]*\\bFOR\\s+UPDATE\\b$of_clause$locking_modifier\\s*;?\\s*\\z/i"; |
Detect FOR UPDATE, FOR SHARE and LOCK IN SHARE MODE with a single tail-anchored regex. Allowing anything but a quote after the clause rules out string literals while still matching OF/NOWAIT/SKIP LOCKED/WAIT modifiers, semicolons and trailing comments, which the previous version missed and routed to a replica. The quick-n-dirty match is first as it's the more popular case.
|
Updated the PR to make the regex simpler and also have the quick-n-dirty match be first as the simpler/more common case. The previous version, besides being a bit hard for me to read/understand, I think missed some cases of legitimate locking cases: ` - WHERE tag = '#foo' FOR UPDATE
This new one will throw a false-positive if someone literally writes FOR UPDATE in a comment, but I think that's acceptable |
|
@vnsavage Thank you very much! Are we good to merge? |
The check rescanned to the next quote for every "FOR UPDATE" in the query, so a SELECT with a long string value repeating those words took seconds to classify. Search only the text after the last quote instead. The rule is unchanged, and a regex error now counts as a write. Still on purpose: the words in a comment count as a write, and a lock clause followed by a quote is missed. Both are described in the code.
|
|
Approved. Feel free to test on the problem you were originally trying to solve before merging. |
Bumps drop-ins/hyperdb from 6fd480d to 082a127 (Automattic/HyperDB#192). SELECT ... FOR UPDATE, FOR SHARE and LOCK IN SHARE MODE are now treated as writes, so they no longer fail on read-only replicas. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>




Summary
SELECT ... FOR UPDATElocking reads as write queriesTesting
php -l db.phpis_write_query()classification checksgit diff --check