diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
index f081eff..7068d2d 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
@@ -203,7 +203,17 @@ public function query($query, array $args = array(), $options = array()) {
     return $return;
   }
 
+  /**
+   * {@inheritdoc}
+   */
   public function prepareQuery($query) {
+    // The statement class is not guaranteed to be set. This works around the
+    // issue that may be caused by a race condition in closing and opening
+    // the connection.
+    $statementClass = $this->connection->getAttribute(\PDO::ATTR_STATEMENT_CLASS);
+    if ($statementClass[0] !== $this->statementClass) {
+      $this->connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));
+    }
     // mapConditionOperator converts LIKE operations to ILIKE for consistency
     // with MySQL. However, Postgres does not support ILIKE on bytea (blobs)
     // fields.
