diff -u b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php --- b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php @@ -95,11 +95,12 @@ assert(is_string($prefix), 'The \'$prefix\' argument to ' . __METHOD__ . '() must be a string'); $this->prefix = $prefix; - // Add the schema name of it is not set to public, else it will use the + // Add the schema name if it is not set to public, otherwise it will use the // default schema name. $quoted_schema = ''; - if (isset($this->connectionOptions['schema']) && ($this->connectionOptions['schema'] != 'public')) { - $quoted_schema = $this->identifierQuotes[0] . $this->connectionOptions['schema'] . $this->identifierQuotes[1] . '.'; + if (isset($this->connectionOptions['schema']) && ($this->connectionOptions['schema'] !== 'public')) { + $sanitizedSchema = preg_replace('/[^A-Za-z0-9_]+/', '', $this->connectionOptions['schema']); + $quoted_schema = $this->identifierQuotes[0] . $sanitizedSchema . $this->identifierQuotes[1] . '.'; } $this->tablePlaceholderReplacements = [ diff -u b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php --- b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php +++ b/core/modules/pgsql/src/Driver/Database/pgsql/Schema.php @@ -56,9 +56,10 @@ public function __construct($connection) { parent::__construct($connection); - // If the schema is not set in the connection options than schema defaults + // If the schema is not set in the connection options then schema defaults // to public. $this->defaultSchema = $connection->getConnectionOptions()['schema'] ?? 'public'; + $this->defaultSchema = preg_replace('/[^A-Za-z0-9_]+/', '', $this->defaultSchema); } /** @@ -128,6 +129,7 @@ public function queryTableInformation($table) { // Generate a key to reference this table's information on. $prefixed_table = $this->connection->getPrefix() . $table; + $key = $this->connection->prefixTables('{' . $table . '}'); // Take into account that temporary tables are stored in a different schema. // \Drupal\Core\Database\Connection::generateTemporaryTableName() sets the @@ -136,12 +138,8 @@ $key = $quoted_key = $this->getTempNamespaceName() . '.' . $prefixed_table; } - elseif ($this->defaultSchema != 'public') { + else { $key = $this->defaultSchema . '.' . $prefixed_table; $quoted_key = '"' . $this->defaultSchema . '"."' . $prefixed_table . '"'; } - else { - $key = $this->defaultSchema . '.' . $prefixed_table; - $quoted_key = '"' . $prefixed_table . '"'; - } if (!isset($this->tableInformation[$key])) { @@ -723,7 +721,6 @@ // clause and not used as an identifier. $index_name = str_replace('"', '', $index_name); - // Get the schema and tablename for the old table. $sql_params = [ ':schema' => $this->defaultSchema, ':table' => $this->connection->getPrefix() . $table,