feat: Add ability to set PostgreSql connection options via appsettings#2975
feat: Add ability to set PostgreSql connection options via appsettings#2975alexeyshibanov wants to merge 12 commits intodevfrom
Conversation
# Conflicts: # tests/VirtoCommerce.Platform.Core.Tests/VirtoCommerce.Platform.Core.Tests.csproj # tests/VirtoCommerce.Platform.Tests/VirtoCommerce.Platform.Tests.csproj # tests/VirtoCommerce.Platform.Web.Tests/VirtoCommerce.Platform.Web.Tests.csproj
|
Review task created: https://virtocommerce.atlassian.net/browse/VCST-4575 |
src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
This PR adds PostgreSQL connection configuration options to allow fine-tuning database connection settings via appsettings.json, particularly optimized for Azure PostgreSQL deployments. The changes enable control over timeouts, connection pooling, and keepalive settings without modifying connection strings directly.
Changes:
- Introduced
PostgreSqlOptionsconfiguration model with properties for command timeouts, pool sizing, connection lifetime, and TCP keepalive settings - Enhanced PostgreSQL connection strings at initialization time with configurable parameters (separate defaults for regular DB and Hangfire operations)
- Improved data layer transaction handling and async disposal patterns in raw SQL helper methods
- Refined command timeout logic in repository and migration setup to only override when necessary
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| src/VirtoCommerce.Platform.Core/PostgreSqlOptions.cs | New configuration model defining PostgreSQL connection parameters with default values |
| src/VirtoCommerce.Platform.Web/appsettings.json | Added PostgreSql configuration section with documented defaults for Azure PostgreSQL |
| src/VirtoCommerce.Platform.Web/Startup.cs | Registered PostgreSqlOptions with DI container and removed unused provider parameter from DbContext setup |
| src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs | Extension method to enhance connection strings with Hangfire-specific settings (HangfireCommandTimeout, HangfireMaxPoolSize) |
| src/VirtoCommerce.Platform.Hangfire/Extensions/ServiceCollectionExtensions.cs | Applied connection string enhancement during Hangfire storage initialization |
| src/VirtoCommerce.Platform.Hangfire/Extensions/ApplicationBuilderExtensions.cs | Applied connection string enhancement during Hangfire middleware setup, removed unused import |
| src/VirtoCommerce.Platform.Data.PostgreSql/Extensions/PostgreSqlOptionsExtensions.cs | Extension method to enhance connection strings with regular DB settings (CommandTimeout, MaxPoolSize) |
| src/VirtoCommerce.Platform.Data.PostgreSql/Extensions/DbContextOptionsBuilderExtensions.cs | Applied connection string enhancement during EF Core PostgreSQL configuration |
| src/VirtoCommerce.Platform.Data/Infrastructure/DbContextRepositoryBase.cs | Modified to only set command timeout when it's unset or lower than connection timeout |
| src/VirtoCommerce.Platform.Data/Extensions/DatabaseFacadeExtensions.cs | Modified to only set command timeout when it's unset or lower than connection timeout |
| src/VirtoCommerce.Platform.Data/Extensions/DbContextCommandExtensions.cs | Enhanced raw SQL methods with transaction participation and async disposal patterns |
| tests/VirtoCommerce.Platform.Tests/UnitTests/PostgreSqlOptionsExtensionsTests.cs | Comprehensive unit tests for connection string enhancement logic |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/VirtoCommerce.Platform.Data.PostgreSql/Extensions/PostgreSqlOptionsExtensions.cs
Show resolved
Hide resolved
src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs
Show resolved
Hide resolved
src/VirtoCommerce.Platform.Data/Extensions/DatabaseFacadeExtensions.cs
Outdated
Show resolved
Hide resolved
tests/VirtoCommerce.Platform.Tests/UnitTests/PostgreSqlOptionsExtensionsTests.cs
Show resolved
Hide resolved
src/VirtoCommerce.Platform.Data/Extensions/DbContextCommandExtensions.cs
Outdated
Show resolved
Hide resolved
tests/VirtoCommerce.Platform.Tests/UnitTests/PostgreSqlOptionsExtensionsTests.cs
Show resolved
Hide resolved
tests/VirtoCommerce.Platform.Tests/UnitTests/PostgreSqlOptionsExtensionsTests.cs
Show resolved
Hide resolved
src/VirtoCommerce.Platform.Data/Infrastructure/DbContextRepositoryBase.cs
Outdated
Show resolved
Hide resolved
src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs
Show resolved
Hide resolved
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.994
Timestamp: 30-01-2026T10:25:02
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.552
Timestamp: 30-01-2026T10:36:23
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.978
Timestamp: 30-01-2026T11:45:21
src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs
Show resolved
Hide resolved
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.831
Timestamp: 30-01-2026T11:58:33
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.727
Timestamp: 02-02-2026T09:42:32
src/VirtoCommerce.Platform.Data/Infrastructure/DbContextRepositoryBase.cs
Show resolved
Hide resolved
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 8.02
Timestamp: 02-02-2026T15:15:29
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| if (commandTimeout is null || (commandTimeout >= 0 && commandTimeout < connectionTimeout)) | ||
| { | ||
| dbContext.Database.SetCommandTimeout(connectionTimeout); | ||
| } |
There was a problem hiding this comment.
Infinite command timeout (0) incorrectly gets overwritten
Medium Severity
The condition commandTimeout >= 0 && commandTimeout < connectionTimeout incorrectly overwrites an infinite command timeout. In EF Core, a commandTimeout of 0 means "no timeout" (wait indefinitely). When a user sets CommandTimeout=0 to allow long-running queries, and connectionTimeout is positive (e.g., 30), the check 0 < 30 evaluates to true, causing the infinite timeout to be replaced with the connection timeout value. Using commandTimeout > 0 instead of >= 0 would preserve the special infinite timeout semantics.
vc-ci
left a comment
There was a problem hiding this comment.
Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.745
Timestamp: 03-02-2026T07:18:07




Description
These changes allow set PostgreSQL connection options via appsettings when we can't do it via configure ConnectionString.
Note
Medium Risk
Medium risk because it changes how PostgreSQL connection strings and EF/Hangfire timeouts are derived at runtime, which can affect pooling, timeouts, and overall DB stability/performance under load.
Overview
Adds a new
PostgreSqlOptionsconfiguration model (with validation) and wires it into Startup so PostgreSQL tuning can be set viaappsettings.json.PostgreSQL connection strings for EF Core and Hangfire are now automatically enhanced with configurable defaults (command timeout, pool sizes, connection lifetime, keepalive/TCP keepalive) only when those parameters aren’t already present, using new
EnhanceConnectionStringhelpers.Adjusts EF repository initialization to avoid overwriting an explicitly configured EF command timeout unless it’s unset or lower than the connection timeout, and adds unit tests covering the connection-string enhancement behavior and casing/alias preservation.
Written by Cursor Bugbot for commit 789a8fb. This will update automatically on new commits. Configure here.
References
QA-test:
Jira-link:
Artifact URL:
Image tag:
ghcr.io/VirtoCommerce/platform:3.1002.0-pr-2975-789a-postgre-options-789a8fb3