From 660c96c22dd0024de75d10f24879c24a32b83763 Mon Sep 17 00:00:00 2001 From: VladMakarenko99 Date: Wed, 3 Dec 2025 10:54:43 +0100 Subject: [PATCH 1/2] Add check for existing 'age' extension before loading in AgeConnectionManager --- .../ApacheAge/AgeConnectionManager.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs b/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs index 59bb7a28b..44841e721 100644 --- a/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs +++ b/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs @@ -132,12 +132,27 @@ private async Task LoadAgeAsync(NpgsqlConnection connection, CancellationToken c { try { + await using var checkCommand = connection.CreateCommand(); + checkCommand.CommandText = "SELECT 1 FROM pg_extension WHERE extname = 'age';"; + checkCommand.CommandTimeout = 0; + var result = await checkCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); + + if (result is not null) + { + LogMessages.ExtensionLoaded(_logger, ConnectionString); + return; + } + await using var load = connection.CreateCommand(); load.CommandText = "LOAD 'age';"; load.CommandTimeout = 0; await load.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); LogMessages.ExtensionLoaded(_logger, ConnectionString); } + catch (PostgresException ex) when (ex.SqlState == "42501") + { + LogMessages.ExtensionLoaded(_logger, ConnectionString); + } catch (PostgresException ex) { LogMessages.ExtensionNotLoadedError(_logger, ConnectionString, ex.MessageText); From 8f41b76278364c881d6a5d6bc7143b89910737c1 Mon Sep 17 00:00:00 2001 From: VladMakarenko99 Date: Wed, 3 Dec 2025 12:55:34 +0100 Subject: [PATCH 2/2] Bump version to 10.0.6 and refine AGE extension initialization logic in AgeConnectionManager. --- Directory.Build.props | 4 ++-- .../ApacheAge/AgeConnectionManager.cs | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 03e4b6e0a..ff1e5ee05 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -26,8 +26,8 @@ https://github.com/managedcode/graphrag https://github.com/managedcode/graphrag Managed Code GraphRag - 10.0.5 - 10.0.5 + 10.0.6 + 10.0.6 diff --git a/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs b/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs index 44841e721..a90b49c51 100644 --- a/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs +++ b/src/ManagedCode.GraphRag.Postgres/ApacheAge/AgeConnectionManager.cs @@ -137,10 +137,9 @@ private async Task LoadAgeAsync(NpgsqlConnection connection, CancellationToken c checkCommand.CommandTimeout = 0; var result = await checkCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); - if (result is not null) + if (result is null) { - LogMessages.ExtensionLoaded(_logger, ConnectionString); - return; + throw new AgeException("AGE extension is not installed."); } await using var load = connection.CreateCommand(); @@ -151,6 +150,18 @@ private async Task LoadAgeAsync(NpgsqlConnection connection, CancellationToken c } catch (PostgresException ex) when (ex.SqlState == "42501") { + await using var initCommand = connection.CreateCommand(); + initCommand.CommandText = "SELECT ag_catalog.create_graph('__age_init__'); SELECT ag_catalog.drop_graph('__age_init__', true);"; + initCommand.CommandTimeout = 0; + + try + { + await initCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + catch (PostgresException) + { + } + LogMessages.ExtensionLoaded(_logger, ConnectionString); } catch (PostgresException ex)