Skip to content

[PLTFRM-1887] Route SELECT FOR UPDATE queries to writable servers - #192

Merged
rebeccahum merged 5 commits into
trunkfrom
codex/route-select-for-update-to-primary
Nov 13, 2026
Merged

rebeccahum merged 5 commits into
trunkfrom
codex/route-select-for-update-to-primary

Conversation

@rebeccahum

Copy link
Copy Markdown
Contributor

Summary

  • classify SELECT ... FOR UPDATE locking reads as write queries
  • route these queries to writable database servers instead of read replicas

Testing

  • php -l db.php
  • focused is_write_query() classification checks
  • git diff --check

@rebeccahum
rebeccahum marked this pull request as ready for review August 21, 2026 19:46
@rebeccahum
rebeccahum requested review from vnsavage and a balanced review from Copilot August 21, 2026 19:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Routes SELECT ... FOR UPDATE locking reads to writable database servers.

Changes:

  • Detects FOR UPDATE within SELECT queries.
  • 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.

Comment thread db.php Resolved
@rebeccahum
rebeccahum force-pushed the codex/route-select-for-update-to-primary branch 2 times, most recently from 7c7cff6 to 728f4f5 Compare August 21, 2026 19:56
@rebeccahum
rebeccahum force-pushed the codex/route-select-for-update-to-primary branch from b417d13 to d7b69b0 Compare August 21, 2026 20:05
@rebeccahum
rebeccahum requested a balanced review from Copilot August 21, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 UPDATE matches $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 a FOR UPDATE match.
		$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 */ or SELECT * 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";

Comment thread db.php Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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, and SELECT * FROM wp_posts FOR UPDATE -- trace has a valid trailing comment; both fall through to false and 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 requiring FOR UPDATE to 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";

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

Comment thread db.php Outdated
$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";
Comment thread db.php Outdated
$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.
@vnsavage

Copy link
Copy Markdown
Contributor

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

  • WHERE note = 'a -- b' FOR UPDATE
  • FOR UPDATE /* dataset foo */
  • FOR UPDATE -- lock it
    `

This new one will throw a false-positive if someone literally writes FOR UPDATE in a comment, but I think that's acceptable

@rebeccahum

Copy link
Copy Markdown
Contributor Author

@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.
@sonarqubecloud

Copy link
Copy Markdown

@vnsavage vnsavage left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@vnsavage

Copy link
Copy Markdown
Contributor

Approved. Feel free to test on the problem you were originally trying to solve before merging.

@rebeccahum
rebeccahum merged commit 082a127 into trunk Nov 13, 2026
5 checks passed
@rebeccahum
rebeccahum deleted the codex/route-select-for-update-to-primary branch November 13, 2026 18:09
rebeccahum added a commit to Automattic/vip-go-mu-plugins that referenced this pull request Nov 16, 2026
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>
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.

3 participants