diff --git a/lib/dml/pgsql_native_moodle_database.php b/lib/dml/pgsql_native_moodle_database.php index 67ade460871..9143f105520 100644 --- a/lib/dml/pgsql_native_moodle_database.php +++ b/lib/dml/pgsql_native_moodle_database.php @@ -48,6 +48,9 @@ class pgsql_native_moodle_database extends moodle_database { /** @var int Number of cursors used (for constructing a unique ID) */ protected $cursorcount = 0; + /** @var string Unique identifier for this request (also for constructing a unique ID) */ + protected $requestunique = null; + /** @var int Default number of rows to fetch at a time when using recordsets with cursors */ const DEFAULT_FETCH_BUFFER_SIZE = 100000; @@ -760,9 +763,18 @@ class pgsql_native_moodle_database extends moodle_database { if ($usecursors) { // Work out the cursor unique identifer. This is based on a simple count used which // should be OK because the identifiers only need to be unique within the current - // transaction. + // transaction. However, if persistent database connections are used, then this could + // cause problems, so we also add a unique per-request identifier based on the current + // time and a random number. + if ($this->requestunique === null) { + if (empty($this->dboptions['dbpersist'])) { + $this->requestunique = time() . '_' . random_string(8) . '_'; // TODO + } else { + $this->requestunique = ''; + } + } $this->cursorcount++; - $cursorname = 'crs' . $this->cursorcount; + $cursorname = 'crs' . $this->requestunique . $this->cursorcount; // Do the query to a cursor. $sql = 'DECLARE ' . $cursorname . ' NO SCROLL CURSOR WITH HOLD FOR ' . $sql;