Skip to content

Commit 082a127

Browse files
rebeccahumvnsavage
andauthored
[PLTFRM-1887] Route SELECT FOR UPDATE queries to writable servers (#192)
* Route SELECT FOR UPDATE queries to writable servers * Avoid false positives in locking read detection * Feedback * Simplify locking read detection 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. * Make locking read detection linear 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. --------- Co-authored-by: Iliya Polihronov <vnsavage@gmail.com>
1 parent 6fd480d commit 082a127

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

‎db.php‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,31 @@ public function get_table_from_query( $q ) {
340340
* @return bool
341341
*/
342342
public function is_write_query( $q ) {
343-
// Quick and dirty: only SELECT statements are considered read-only.
344343
$q = ltrim( $q, "\r\n\t (" );
345-
return ! preg_match( '/^(?:SELECT|SHOW|DESCRIBE|DESC|EXPLAIN)\s/i', $q );
344+
345+
// Quick and dirty: only SELECT statements are considered read-only.
346+
if ( ! preg_match( '/^(?:SELECT|SHOW|DESCRIBE|DESC|EXPLAIN)\s/i', $q ) ) {
347+
return true;
348+
}
349+
350+
// A SELECT that locks rows (FOR UPDATE, FOR SHARE, LOCK IN SHARE MODE) counts as a
351+
// write: the locks only mean something on a writable server.
352+
//
353+
// Quick and dirty again. To ignore those words inside a string value, such as
354+
// WHERE title = 'for update'
355+
// only the text after the last quote character is searched. That gets two kinds
356+
// of query wrong, both on purpose:
357+
//
358+
// SELECT ... /* for update */ no lock, but treated as a write
359+
// SELECT ... FOR UPDATE /* don't remove */ real lock, but treated as a read
360+
//
361+
// The first costs a query on the master. The second needs a quote somewhere after
362+
// the lock clause, and is routed the way every locking read was before this check.
363+
$tail = substr( $q, (int) max( strrpos( $q, "'" ), strrpos( $q, '"' ) ) );
364+
365+
// preg_match() returns false if the regex engine gives up, which is not known to
366+
// happen here. If it ever does we cannot tell, so assume a write.
367+
return 0 !== preg_match( '/\b(?:FOR\s+UPDATE|FOR\s+SHARE|LOCK\s+IN\s+SHARE\s+MODE)\b/i', $tail );
346368
}
347369

348370
/**

0 commit comments

Comments
 (0)