Skip to content

Commit 033a654

Browse files
Merge pull request #42929 from nextcloud/fix/db/transacted-read-not-dirty
fix(db): Do not log transacted reads as dirty read
2 parents 9bce24b + f54b08c commit 033a654

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/private/DB/Connection.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,16 @@ public function prepare($sql, $limit = null, $offset = null): Statement {
265265
*/
266266
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
267267
$tables = $this->getQueriedTables($sql);
268-
if (count(array_intersect($this->tableDirtyWrites, $tables)) === 0 && !$this->isTransactionActive()) {
268+
if ($this->isTransactionActive()) {
269+
// Transacted queries go to the primary. The consistency of the primary guarantees that we can not run
270+
// into a dirty read.
271+
} elseif (count(array_intersect($this->tableDirtyWrites, $tables)) === 0) {
269272
// No tables read that could have been written already in the same request and no transaction active
270273
// so we can switch back to the replica for reading as long as no writes happen that switch back to the primary
271274
// We cannot log here as this would log too early in the server boot process
272275
$this->ensureConnectedToReplica();
273276
} else {
274-
// Read to a table that was previously written to
277+
// Read to a table that has been written to previously
275278
// While this might not necessarily mean that we did a read after write it is an indication for a code path to check
276279
$this->logger->debug('dirty table reads: ' . $sql, ['tables' => $this->tableDirtyWrites, 'reads' => $tables, 'exception' => new \Exception()]);
277280
}

0 commit comments

Comments
 (0)