Skip to content

Commit 76b0a8f

Browse files
committed
Remove global DatabaseInfo caching
1 parent 7b0dfca commit 76b0a8f

16 files changed

Lines changed: 16 additions & 100 deletions

src/Npgsql/Internal/NpgsqlDatabaseInfo.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public abstract class NpgsqlDatabaseInfo
1717
{
1818
#region Fields
1919

20-
internal static readonly ConcurrentDictionary<NpgsqlDatabaseInfoCacheKey, NpgsqlDatabaseInfo> Cache
21-
= new();
22-
2320
static volatile INpgsqlDatabaseInfoFactory[] Factories = new INpgsqlDatabaseInfoFactory[]
2421
{
2522
new PostgresMinimalDatabaseInfoFactory(),
@@ -302,8 +299,6 @@ public static void RegisterFactory(INpgsqlDatabaseInfoFactory factory)
302299
factories[0] = factory;
303300
Array.Copy(Factories, 0, factories, 1, Factories.Length);
304301
Factories = factories;
305-
306-
Cache.Clear();
307302
}
308303

309304
internal static async Task<NpgsqlDatabaseInfo> Load(NpgsqlConnector conn, NpgsqlTimeout timeout, bool async)
@@ -324,14 +319,11 @@ internal static async Task<NpgsqlDatabaseInfo> Load(NpgsqlConnector conn, Npgsql
324319

325320
// For tests
326321
internal static void ResetFactories()
327-
{
328-
Factories = new INpgsqlDatabaseInfoFactory[]
322+
=> Factories = new INpgsqlDatabaseInfoFactory[]
329323
{
330324
new PostgresMinimalDatabaseInfoFactory(),
331325
new PostgresDatabaseInfoFactory()
332326
};
333-
Cache.Clear();
334-
}
335327

336328
#endregion Factory management
337329
}

src/Npgsql/NpgsqlConnection.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ internal NpgsqlDataSource NpgsqlDataSource
8787
/// The global type mapper, which contains defaults used by all new connections.
8888
/// Modify mappings on this mapper to affect your entire application.
8989
/// </summary>
90+
[Obsolete("Global-level type mapping has been replaced with data source mapping, see the 7.0 release notes.")]
9091
public static INpgsqlTypeMapper GlobalTypeMapper => TypeMapping.GlobalTypeMapper.Instance;
9192

9293
/// <summary>
@@ -1882,9 +1883,8 @@ public void ReloadTypes()
18821883

18831884
using var scope = StartTemporaryBindingScope(out var connector);
18841885

1885-
_dataSource!.SetupMappings(
1886+
_dataSource!.Bootstrap(
18861887
connector,
1887-
forceReload: true,
18881888
NpgsqlTimeout.Infinite,
18891889
async: false,
18901890
CancellationToken.None)
@@ -1901,9 +1901,8 @@ public async Task ReloadTypesAsync()
19011901

19021902
using var scope = StartTemporaryBindingScope(out var connector);
19031903

1904-
await _dataSource!.SetupMappings(
1904+
await _dataSource!.Bootstrap(
19051905
connector,
1906-
forceReload: true,
19071906
NpgsqlTimeout.Infinite,
19081907
async: true,
19091908
CancellationToken.None);

src/Npgsql/NpgsqlDataSource.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,6 @@ public static NpgsqlDataSource Create(NpgsqlConnectionStringBuilder connectionSt
196196
=> Create(connectionStringBuilder.ToString());
197197

198198
internal async Task Bootstrap(NpgsqlConnector connector, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
199-
{
200-
await SetupMappings(connector, forceReload: false, timeout, async, cancellationToken);
201-
202-
IsBootstrapped = true;
203-
}
204-
205-
internal async Task SetupMappings(
206-
NpgsqlConnector connector,
207-
bool forceReload,
208-
NpgsqlTimeout timeout,
209-
bool async,
210-
CancellationToken cancellationToken = default)
211199
{
212200
var hasSemaphore = async
213201
? await _setupMappingsSemaphore.WaitAsync(timeout.CheckAndGetTimeLeft(), cancellationToken)
@@ -223,17 +211,17 @@ internal async Task SetupMappings(
223211
var typeMapper = new TypeMapper(connector, _defaultNameTranslator);
224212
connector.TypeMapper = typeMapper;
225213

226-
var key = new NpgsqlDatabaseInfoCacheKey(Settings);
227-
if (forceReload || !NpgsqlDatabaseInfo.Cache.TryGetValue(key, out var database))
228-
{
229-
using var _ = connector.StartUserAction(ConnectorState.Executing, cancellationToken);
230-
NpgsqlDatabaseInfo.Cache[key] = database = await NpgsqlDatabaseInfo.Load(connector, timeout, async);
231-
}
214+
NpgsqlDatabaseInfo databaseInfo;
232215

233-
DatabaseInfo = database;
234-
connector.DatabaseInfo = database;
235-
typeMapper.Initialize(database, _resolverFactories, _userTypeMappings);
216+
using (connector.StartUserAction(ConnectorState.Executing, cancellationToken))
217+
databaseInfo = await NpgsqlDatabaseInfo.Load(connector, timeout, async);
218+
219+
DatabaseInfo = databaseInfo;
220+
connector.DatabaseInfo = databaseInfo;
221+
typeMapper.Initialize(databaseInfo, _resolverFactories, _userTypeMappings);
236222
TypeMapper = typeMapper;
223+
224+
IsBootstrapped = true;
237225
}
238226
finally
239227
{

src/Npgsql/NpgsqlDatabaseInfoCacheKey.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/Npgsql.Tests/BugTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ public async Task Bug1987()
247247
dataSourceBuilder.MapEnum<Mood>(type);
248248
await using var dataSource = dataSourceBuilder.Build();
249249
await using var connection = await dataSource.OpenConnectionAsync();
250-
await connection.ReloadTypesAsync();
251250

252251
for (var i = 0; i < 2; i++)
253252
{
@@ -358,7 +357,6 @@ await adminConnection.ExecuteNonQueryAsync($@"
358357
dataSourceBuilder.MapEnum<Bug2278EnumType>(enumType);
359358
await using var dataSource = dataSourceBuilder.Build();
360359
await using var connection = await dataSource.OpenConnectionAsync();
361-
await connection.ReloadTypesAsync();
362360

363361
await connection.ExecuteScalarAsync($"SELECT * FROM {table} AS d");
364362
}
@@ -1197,7 +1195,6 @@ public async Task CompositePostgresType()
11971195
dataSourceBuilder.MapComposite<SomeComposite>(type);
11981196
await using var dataSource = dataSourceBuilder.Build();
11991197
await using var connection = await dataSource.OpenConnectionAsync();
1200-
await connection.ReloadTypesAsync();
12011198

12021199
await connection.ExecuteNonQueryAsync(@$"
12031200
CREATE OR REPLACE FUNCTION {func}(id int, out comp1 {type}, OUT comp2 {type}[])

test/Npgsql.Tests/CommandBuilderTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,6 @@ public async Task DeriveParameters_text_mapped_enum()
621621
await using var adminConnection = await OpenConnectionAsync();
622622
await using var _ = await GetTempTypeName(adminConnection, out var type);
623623
await adminConnection.ExecuteNonQueryAsync($@"CREATE TYPE {type} AS ENUM ('apple', 'cherry', 'plum')");
624-
adminConnection.ReloadTypes();
625624

626625
var dataSourceBuilder = CreateDataSourceBuilder();
627626
dataSourceBuilder.MapEnum<Fruit>(type);
@@ -670,7 +669,6 @@ public async Task DeriveParameters_text_mapped_composite()
670669
dataSourceBuilder.MapComposite<SomeComposite>(type);
671670
await using var dataSource = dataSourceBuilder.Build();
672671
await using var connection = await dataSource.OpenConnectionAsync();
673-
await connection.ReloadTypesAsync();
674672

675673
var expected1 = new SomeComposite { X = 8, SomeText = "foo" };
676674
var expected2 = new[] { expected1, new SomeComposite {X = 9, SomeText = "bar"} };

test/Npgsql.Tests/CopyTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,13 @@ public async Task Enum()
552552
{
553553
await using var adminConnection = await OpenConnectionAsync();
554554
await using var _ = await GetTempTypeName(adminConnection, out var type);
555+
await adminConnection.ExecuteNonQueryAsync($"CREATE TYPE {type} AS ENUM ('sad', 'ok', 'happy')");
555556

556557
var dataSourceBuilder = CreateDataSourceBuilder();
557558
dataSourceBuilder.MapEnum<Mood>(type);
558559
await using var dataSource = dataSourceBuilder.Build();
559560
await using var connection = await dataSource.OpenConnectionAsync();
560561

561-
await connection.ExecuteNonQueryAsync($"CREATE TYPE {type} AS ENUM ('sad', 'ok', 'happy')");
562-
await connection.ReloadTypesAsync();
563562
await using var __ = await CreateTempTable(connection, $"mymood {type}, mymoodarr {type}[]", out var table);
564563

565564
await using (var writer = await connection.BeginBinaryImportAsync($"COPY {table} (mymood, mymoodarr) FROM STDIN BINARY"))

test/Npgsql.Tests/ReaderNewSchemaTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ public async Task DataType_with_composite()
443443
dataSourceBuilder.MapComposite<SomeComposite>(type);
444444
await using var dataSource = dataSourceBuilder.Build();
445445
await using var connection = await dataSource.OpenConnectionAsync();
446-
await connection.ReloadTypesAsync();
447446

448447
await using var cmd = new NpgsqlCommand($"SELECT comp,'(4)'::{type} FROM {tableName}", connection);
449448
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SchemaOnly);

test/Npgsql.Tests/Replication/CommonReplicationTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ public class CommonReplicationTests<TConnection> : SafeReplicationTestBase<TConn
2222
{
2323
#region Open
2424

25-
[Test, Parallelizable(ParallelScope.None)]
25+
[Test, NonParallelizable]
2626
public async Task Open()
2727
{
28-
// Force type reloading from the replication connection if it is a logical replication connection
29-
if (typeof(TConnection) == typeof(LogicalReplicationConnection))
30-
Internal.NpgsqlDatabaseInfo.Cache.Clear();
3128
await using var rc = await OpenReplicationConnectionAsync();
3229
}
3330

test/Npgsql.Tests/Replication/PgOutputReplicationTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,6 @@ await adminConnection.ExecuteNonQueryAsync(@$"
10231023
// In regular tests we'd use a data source, but replication doesn't work with data sources (yet).
10241024
// In addition, clear the DatabaseInfo cache.
10251025
using var _ = CreateTempPool(ConnectionString, out var connString);
1026-
Internal.NpgsqlDatabaseInfo.Cache.Clear();
10271026
var rc = await OpenReplicationConnectionAsync(connString);
10281027
var slot = await rc.CreatePgOutputReplicationSlot(slotName);
10291028
var expected = new Descriptor { Id = 1248, Name = "My Descriptor" };

0 commit comments

Comments
 (0)