Path: redb/redb.Tests.Integration/Tests/PostgresPro/PostgresProListRoundTripsTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/PostgresPro/PostgresProListRoundTripsTests.cs ---- using System.Diagnostics; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using redb.Core.Extensions; using redb.Core.Pro.Extensions; using redb.Postgres.Pro.Extensions; using redb.Tests.Integration.Tests.Base; namespace redb.Tests.Integration.Tests.PostgresPro; [Collection("PostgresPro")] public class PostgresProListRoundTripsTests : ListRoundTripsTestsBase { private const string ProbeName = "redb.Tests.PostgresProListRoundTrips"; private static readonly ActivitySource Probe = new(ProbeName); protected override void Register(IServiceCollection services, Action configure) => services.AddRedbPro(configure); protected override void UseProvider(RedbOptionsBuilder options) { var cs = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("Postgres")!; options.UsePostgres(cs); } /// /// The statements Npgsql sends for , one activity each, without the statistics guard's own: until /// the tables have planner statistics it checks them and sets enable_nestloop, on a recheck interval, so whether a /// counted call meets it depends on timing. Everything else counts, the scheme hash and the type table included. /// protected override async Task CountRoundTripsAsync(Func call) { var statements = new List(); using var listener = new ActivityListener { ShouldListenTo = s => s.Name.StartsWith("Npgsql", StringComparison.Ordinal) || s.Name == ProbeName, Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded, ActivityStopped = a => { lock (statements) statements.Add(a); }, }; ActivitySource.AddActivityListener(listener); ActivityTraceId trace; using (var parent = Probe.StartActivity("round-trips")!) { trace = parent.TraceId; await call(); } lock (statements) return statements.Count(a => a.Source.Name.StartsWith("Npgsql", StringComparison.Ordinal) && a.TraceId == trace && !IsStatisticsGuard((a.GetTagItem("db.query.text") ?? a.GetTagItem("db.statement"))?.ToString())); } /// /// The list goes to the server as its builder writes it (the statement ToSqlStringAsync shows), with the scheme hash /// as a second command of the same batch: no wrapper around the page. Two wrapped forms made large pages slower. /// [Fact] public async Task ListStatement_IsSentAsBuilt_WithTheHashBesideIt() { var run = Guid.NewGuid().ToString("N")[..8]; var services = new ServiceCollection(); Register(services, options => { UseProvider(options); options.Configure(c => { c.EnablePropsCache = false; c.CacheDomain = $"round-trips-text-{run}"; }); }); await using var sp = services.BuildServiceProvider(); var redb = sp.GetRequiredService(); await redb.InitializeAsync(ensureCreated: true); await redb.SyncSchemeAsync(); var query = redb.Query().Where(p => p.Tag == run).OrderBy(p => p.Number).Take(10); await query.ToListAsync(); var preview = await query.ToSqlStringAsync(); var built = string.Join("\n", preview.Split('\n').Where(l => !l.TrimStart().StartsWith("--", StringComparison.Ordinal))).Trim(); var sent = new List(); using var listener = new ActivityListener { ShouldListenTo = s => s.Name.StartsWith("Npgsql", StringComparison.Ordinal), Sample = (ref ActivityCreationOptions _) => ActivitySamplingResult.AllDataAndRecorded, ActivityStopped = a => { if ((a.GetTagItem("db.query.text") ?? a.GetTagItem("db.statement"))?.ToString() is { } text) lock (sent) sent.Add(text); }, }; ActivitySource.AddActivityListener(listener); await query.ToListAsync(); lock (sent) { sent.Should().NotContain(s => s.Contains("redb_page", StringComparison.Ordinal), "the page is not wrapped"); sent.Should().Contain(s => s.Contains(built, StringComparison.Ordinal), "the list goes as its builder writes it"); sent.Should().Contain(s => s.Contains("_structure_hash FROM _schemes", StringComparison.Ordinal), "the hash rides in the batch"); } } private static bool IsStatisticsGuard(string? statement) => statement != null && (statement.Contains("reltuples", StringComparison.Ordinal) || statement.Contains("enable_nestloop", StringComparison.Ordinal)); }