Sitelet https://github.com/npgsql/npgsql/pull/6139/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,9 @@ void WriteStartupMessage(string username)
if (Settings.Database is not null)
startupParams["database"] = Settings.Database;

if (Settings.ApplicationName?.Length > 0)
startupParams["application_name"] = Settings.ApplicationName;
var applicationName = Settings.ApplicationName ?? PostgresEnvironment.AppName;
if (applicationName?.Length > 0)
startupParams["application_name"] = applicationName;

if (Settings.SearchPath?.Length > 0)
startupParams["search_path"] = Settings.SearchPath;
Expand Down
2 changes: 2 additions & 0 deletions src/Npgsql/PostgresEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ internal static string? SslCertRootDefault

internal static string? RequireAuth => Environment.GetEnvironmentVariable("PGREQUIREAUTH");

internal static string? AppName => Environment.GetEnvironmentVariable("PGAPPNAME");

static string? GetHomeDir()
=> Environment.GetEnvironmentVariable(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "APPDATA" : "HOME");

Expand Down
41 changes: 41 additions & 0 deletions test/Npgsql.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,47 @@ public async Task Timezone_connection_param()

#endregion Timezone

#region Application Name

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/6133")]
[NonParallelizable] // Sets environment variable
public async Task Application_name_env_var()
{
const string testAppName = "MyTestApp";

// Note that the pool is unaware of the environment variable, so if a connection is

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we have this with various other environment variables as well; modifying environment variables as they application is running is not supported in thaat sense.

// returned from the pool it may contain the wrong application name
using var _ = SetEnvironmentVariable("PGAPPNAME", testAppName);
await using var dataSource = CreateDataSource();
await using var conn = await dataSource.OpenConnectionAsync();
Assert.That(conn.PostgresParameters["application_name"], Is.EqualTo(testAppName));
}

[Test]
public async Task Application_name_connection_param()
{
const string testAppName = "MyTestApp2";

await using var dataSource = CreateDataSource(csb => csb.ApplicationName = testAppName);
await using var conn = await dataSource.OpenConnectionAsync();
Assert.That(conn.PostgresParameters["application_name"], Is.EqualTo(testAppName));
}

[Test]
[NonParallelizable] // Sets environment variable
public async Task Application_name_connection_param_overrides_env_var()
{
const string envAppName = "EnvApp";
const string connAppName = "ConnApp";

using var _ = SetEnvironmentVariable("PGAPPNAME", envAppName);
await using var dataSource = CreateDataSource(csb => csb.ApplicationName = connAppName);
await using var conn = await dataSource.OpenConnectionAsync();
Assert.That(conn.PostgresParameters["application_name"], Is.EqualTo(connAppName));
}

#endregion Application Name

#region ConnectionString - Host

[TestCase("127.0.0.1", ExpectedResult = new [] { "127.0.0.1:5432" })]
Expand Down