diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
index 106add49efb0..afb7f604fbde 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php
@@ -1060,6 +1060,23 @@ EOD;
return strtr($hash, ['+' => '_', '/' => '_', '=' => '']);
}
+ /**
+ * Determines whether the PostgreSQL extension is created.
+ *
+ * @param string $name
+ * The name of the extension.
+ *
+ * @return bool
+ * Return TRUE when the extension is created, FALSE otherwise.
+ *
+ * @internal
+ */
+ public function extensionExists($name): bool {
+ return (bool) $this->connection->query('SELECT installed_version FROM pg_available_extensions WHERE name = :name', [
+ ':name' => $name,
+ ])->fetchField();
+ }
+
}
/**
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index e19a96853d93..6eae0bb05095 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -472,6 +472,21 @@ function system_requirements($phase) {
}
}
+ // Test with PostgreSQL databases for the status of the pg_trgm extension.
+ if ($phase === 'runtime') {
+ if (Database::isActiveConnection() && (Database::getConnection()->driver() == 'pgsql') && !Database::getConnection()->schema()->extensionExists('pg_trgm')) {
+ $requirements['pgsql_extension_pg_trgm'] = [
+ 'severity' => REQUIREMENT_WARNING,
+ 'title' => t('PostgreSQL pg_trgm extension'),
+ 'value' => t('Not created'),
+ 'description' => t('The pg_trgm PostgreSQL extension is not present. The extension will be required by Drupal 10 to improve performance when using PostgreSQL. See Drupal database server requirements for more information.', [
+ ':pg_trgm' => 'https://www.postgresql.org/docs/current/pgtrgm.html',
+ ':requirements' => 'https://www.drupal.org/docs/system-requirements/database-server-requirements',
+ ]),
+ ];
+ }
+ }
+
// Test PHP memory_limit
$memory_limit = ini_get('memory_limit');
$requirements['php_memory_limit'] = [
diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
index da8578bc7386..039e916a33ab 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php
@@ -1347,4 +1347,19 @@ class SchemaTest extends KernelTestBase {
$this->assertSame('default value', $result->column7);
}
+ /**
+ * @covers \Drupal\Core\Database\Driver\pgsql\Schema::extensionExists
+ */
+ public function testPgsqlExtensionExists() {
+ if ($this->connection->databaseType() !== 'pgsql') {
+ $this->markTestSkipped("This test only runs for PostgreSQL.");
+ }
+
+ // Test the method for a non existing extension.
+ $this->assertFalse($this->schema->extensionExists('non_existing_extension'));
+
+ // Test the method for an existing extension.
+ $this->assertTrue($this->schema->extensionExists('pg_trgm'));
+ }
+
}