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
51 changes: 51 additions & 0 deletions Darling/Darling.Tests/PgStatementsReadableInTheLogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2026 Erik Darling, Darling Data LLC
*
* This file is part of the SQL Server Performance Monitor.
*
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/

using System.Collections.Generic;
using PerformanceMonitor.Collectors;
using PerformanceMonitor.Darling.Service;
using PerformanceMonitor.Darling.Service.Mcp;
using PerformanceMonitor.Darling.Storage;
using Xunit;

namespace Darling.Tests;

/// <summary>
/// #9236: <see cref="PgLogTextRedactor.RedactStoredStatement"/> withholds a statement whole when an odd run of backslashes
/// stands right before a quote, which is the shape of <c>ESCAPE '\'</c>. A statement the product sends to PostgreSQL that
/// carries one shows as "statement withheld" in the store log and in a slow-plan report, so a timeout cannot be traced to
/// it. Each statement here is the text the product sends; the redactor must give it back as text.
/// </summary>
public sealed class PgStatementsReadableInTheLogTests
{
public static IEnumerable<object[]> Statements()
{
yield return ["find_queries_by_text Query Store", DarlingMcpFindQueriesByTextTools.QueryStoreSql];
yield return ["find_queries_by_text plan cache", DarlingMcpFindQueriesByTextTools.PlanCacheSql];
yield return ["find_queries_by_text all servers", DarlingMcpFindQueriesByTextTools.PlanCacheAllServersSql];
yield return ["find_queries_by_text all servers span", DarlingMcpFindQueriesByTextTools.PlanCacheAllServersSpanSql];
yield return ["find_queries_by_text all servers lookup span", DarlingMcpFindQueriesByTextTools.PlanCacheAllServersLookupSpanSql];
yield return ["find_queries_by_text join, newest span", DarlingMcpFindQueriesByTextTools.AllServersJoinSql(0)];
yield return ["find_queries_by_text join, oldest span", DarlingMcpFindQueriesByTextTools.AllServersJoinSql(int.MaxValue)];
yield return ["find_queries_by_text matched digests", DarlingMcpFindQueriesByTextTools.MatchedDigestsSql];
yield return ["find_queries_by_text sample probe", DarlingMcpFindQueriesByTextTools.PlanCacheAllServersProbeSql];
yield return ["find_queries_by_text plan cache probe", DarlingMcpFindQueriesByTextTools.PlanCacheProbeSql];
yield return ["collected databases search", CollectedDatabases.NamesSearchLimitedSql];
yield return ["setting scrub candidates", PgSettingScrub.CandidateSql];
}

[Theory]
[MemberData(nameof(Statements))]
public void The_log_shows_the_statement_the_product_sent(string label, string statement)
{
var shown = PgLogTextRedactor.RedactStoredStatement(statement);

Assert.True(shown is not null, $"{label}: the statement is withheld from the log");
Assert.False(string.IsNullOrWhiteSpace(shown), $"{label}: the statement shows as empty text");
}
}
4 changes: 2 additions & 2 deletions Darling/Darling.Tests/ServerDatabasesEndpointLiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,14 @@ public void NamesSql_DeduplicatesInsideEachArm_SoTheUnionSortsNamesNotRows()
Assert.Contains("SELECT DISTINCT database_name FROM v_database_size_stats WHERE server_id = $1", sql, StringComparison.Ordinal);
}

/// <summary>#5314: the search read is the shared list narrowed by one <c>ILIKE</c> against $3 with a backslash escape, ahead of
/// <summary>#5314: the search read is the shared list narrowed by one <c>ILIKE</c> against $3 with the default backslash escape (no ESCAPE clause, #9236), ahead of
/// the ORDER BY and the LIMIT, so the limit counts matches; the pattern builder escapes the three characters that mean
/// something to LIKE.</summary>
[Fact]
public void NamesSearchLimitedSql_NarrowsBeforeTheLimit_WithOneEscapedPatternParameter()
{
var sql = CollectedDatabases.NamesSearchLimitedSql;
var like = sql.IndexOf("database_name ILIKE $3 ESCAPE '\\'", StringComparison.Ordinal);
var like = sql.IndexOf("database_name ILIKE $3", StringComparison.Ordinal);
var order = sql.IndexOf("ORDER BY lower(database_name), database_name", StringComparison.Ordinal);
var limit = sql.IndexOf("LIMIT $2", StringComparison.Ordinal);
Assert.True(like > 0 && like < order && order < limit, sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal sealed record SourceBudget(int Milliseconds, double TestDelaySeconds =
FROM query_store_text AS t
WHERE t.server_id = $1
AND ($2::text IS NULL OR t.database_name = $2)
AND t.query_sql_text ILIKE '%' || $3 || '%' ESCAPE '\'
AND t.query_sql_text ILIKE '%' || $3 || '%'
ORDER BY t.database_name, t.query_id
LIMIT $4
) AS m
Expand Down Expand Up @@ -223,7 +223,7 @@ known AS MATERIALIZED (
FROM unnest($9::bytea[], $10::boolean[]) AS k(digest, matched)
),
judged AS MATERIALIZED (
SELECT f.digest, COALESCE((d.query_text || '') ILIKE '%' || $3 || '%' ESCAPE '\', FALSE) AS matched
SELECT f.digest, COALESCE((d.query_text || '') ILIKE '%' || $3 || '%', FALSE) AS matched
FROM (SELECT DISTINCT s.query_text_digest AS digest
FROM seen AS s
WHERE NOT EXISTS (SELECT 1 FROM known AS k WHERE k.digest = s.query_text_digest)) AS f
Expand Down Expand Up @@ -272,7 +272,7 @@ FROM judged AS j
WITH matched AS MATERIALIZED (
SELECT digest
FROM query_text_dim
WHERE query_text ILIKE '%' || $1 || '%' ESCAPE '\'
WHERE query_text ILIKE '%' || $1 || '%'
),
hits AS (
SELECT DISTINCT ON (s.server_id, s.query_hash, s.database_name)
Expand Down Expand Up @@ -305,7 +305,7 @@ FROM page AS p
WITH matched AS MATERIALIZED (
SELECT digest
FROM query_text_dim
WHERE query_text ILIKE '%' || $1 || '%' ESCAPE '\'
WHERE query_text ILIKE '%' || $1 || '%'
),
hits AS (
SELECT DISTINCT ON (s.server_id, s.query_hash, s.database_name)
Expand Down Expand Up @@ -358,7 +358,7 @@ FROM seen AS f
CROSS JOIN LATERAL (SELECT 1 AS hit
FROM query_text_dim AS d
WHERE d.digest = f.digest
AND d.query_text ILIKE '%' || $1 || '%' ESCAPE '\'
AND d.query_text ILIKE '%' || $1 || '%'
LIMIT 1) AS t
),
hits AS (
Expand Down Expand Up @@ -496,7 +496,7 @@ internal static object SearchedWindow(object servers, int? serverCount, string s
internal const string MatchedDigestsSql = @"
SELECT digest
FROM query_text_dim
WHERE query_text ILIKE '%' || $1 || '%' ESCAPE '\'
WHERE query_text ILIKE '%' || $1 || '%'
LIMIT $2";

/// <summary>#7849: the sample read for a pattern that matched at most <see cref="ProbeDigestCap"/> texts, one span of the raw window (see
Expand Down
9 changes: 5 additions & 4 deletions Darling/PerformanceMonitor.Darling.Service/PgSettingScrub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ ON CONFLICT (server_id, collector_name, state_key)
/// masked corpus case's VALUE against, so the two can never drift out of rule-for-rule sync again (#4348).
/// Each entry is a plain ILIKE pattern; <c>%</c>/<c>_</c> are literal wildcards, no escaping needed here
/// because none of these terms contain a literal <c>%</c> or <c>_</c> character themselves (the
/// percent-encoding catch-all is expressed separately in <see cref="CandidateSql"/> with its own ESCAPE
/// clause). This is only half of the coarse filter — <c>CandidateSql</c> also selects on the setting's
/// percent-encoding catch-all is expressed separately in <see cref="CandidateSql"/>, with the backslash as
/// PostgreSQL's default LIKE escape and no ESCAPE clause, because the log reader withholds a statement that has one, #9236).
/// This is only half of the coarse filter — <c>CandidateSql</c> also selects on the setting's
/// NAME (<c>ssl_passphrase_command</c> by exact name, or a dotted extension name containing one of
/// <see cref="CandidateNameTerms"/>), which the census test checks separately by name.
/// </summary>
Expand All @@ -208,7 +209,7 @@ ON CONFLICT (server_id, collector_name, state_key)
"%credential%", "%pwd%", "%pass%",
];

private static readonly string CandidateSql = BuildCandidateSql();
internal static readonly string CandidateSql = BuildCandidateSql();

private static string BuildCandidateSql()
{
Expand All @@ -234,7 +235,7 @@ private static string BuildCandidateSql()
FROM collect.pg_server_config
WHERE
{terms}
OR setting LIKE '%\%%' ESCAPE '\' OR reset_val LIKE '%\%%' ESCAPE '\' OR boot_val LIKE '%\%%' ESCAPE '\'
OR setting LIKE '%\%%' OR reset_val LIKE '%\%%' OR boot_val LIKE '%\%%'
OR (name = 'ssl_passphrase_command' AND (setting <> '' OR boot_val <> '' OR reset_val <> ''))
OR (name LIKE '%.%' AND (
{nameTerms}))";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ AND database_name NOT IN ('master', 'model', 'msdb', 'tempdb')
/// <summary>
/// <see cref="NamesLimitedSql"/> narrowed to the names that contain a search text: $3 is the LIKE pattern
/// <see cref="SearchPattern"/> builds (one text parameter, matched case-insensitively with <c>ILIKE</c>, with
/// backslash as the escape character so <c>%</c>, <c>_</c> and <c>\</c> in the text match only themselves). The
/// PostgreSQL's default LIKE escape character, the backslash, so <c>%</c>, <c>_</c> and <c>\</c> in the text match only themselves; no ESCAPE clause is written, because the log reader withholds a statement with one, #9236). The
/// narrowing is applied BEFORE the row limit, so a database past the list's cap is found by its text. This is what
/// lets the web picker's search box reach past the list's cap. #5314.
/// </summary>
public const string NamesSearchLimitedSql = NamesBody
+ "\nAND database_name ILIKE $3 ESCAPE '\\'"
+ "\nAND database_name ILIKE $3"
+ "\nORDER BY lower(database_name), database_name"
+ "\nLIMIT $2";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ FROM config_alert_log
special characters escaped) by AlertContextSerializer.BuildDedupKeyLikePattern
rather than hand-concatenated here — see its doc comment for why. NULL context_json
rows fail the match either way. */
+ (dedupKey is null ? "" : "\nAND context_json LIKE $3 ESCAPE '\\'"), connection) { CommandTimeout = _commandTimeoutSeconds };
+ (dedupKey is null ? "" : "\nAND context_json LIKE $3"), connection) { CommandTimeout = _commandTimeoutSeconds };
command.Parameters.AddWithValue(sid.Value);
command.Parameters.AddWithValue(metricName);
if (dedupKey is not null)
Expand Down
22 changes: 11 additions & 11 deletions Darling/PerformanceMonitor.Darling.Storage/SchemaHistoryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ public static async Task<IReadOnlyList<SchemaFoundObject>> FindCurrentAsync(
if (text != null)
{
q.Text.Append($@",
coalesce(o.view_definition ILIKE {text} ESCAPE '\', false),
EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND x.computed_definition ILIKE {text} ESCAPE '\'),
EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND x.default_definition ILIKE {text} ESCAPE '\'),
EXISTS (SELECT 1 FROM collect.schema_constraints x WHERE x.version_id = o.version_id AND x.is_current AND x.definition ILIKE {text} ESCAPE '\'),
EXISTS (SELECT 1 FROM collect.schema_indexes x WHERE x.version_id = o.version_id AND x.is_current AND x.filter_definition ILIKE {text} ESCAPE '\')");
coalesce(o.view_definition ILIKE {text}, false),
EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND x.computed_definition ILIKE {text}),
EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND x.default_definition ILIKE {text}),
EXISTS (SELECT 1 FROM collect.schema_constraints x WHERE x.version_id = o.version_id AND x.is_current AND x.definition ILIKE {text}),
EXISTS (SELECT 1 FROM collect.schema_indexes x WHERE x.version_id = o.version_id AND x.is_current AND x.filter_definition ILIKE {text})");
}
else
{
Expand All @@ -377,12 +377,12 @@ public static async Task<IReadOnlyList<SchemaFoundObject>> FindCurrentAsync(

if (search.NamePrefix != null)
{
q.Text.Append(" AND lower(o.object_name) LIKE lower(").Append(q.P(NpgsqlDbType.Text, EscapeLike(search.NamePrefix) + "%")).Append(@") ESCAPE '\'");
q.Text.Append(" AND lower(o.object_name) LIKE lower(").Append(q.P(NpgsqlDbType.Text, EscapeLike(search.NamePrefix) + "%")).Append(')');
}

if (search.NameContains != null)
{
q.Text.Append(" AND lower(o.object_name) LIKE lower(").Append(q.P(NpgsqlDbType.Text, "%" + EscapeLike(search.NameContains) + "%")).Append(@") ESCAPE '\'");
q.Text.Append(" AND lower(o.object_name) LIKE lower(").Append(q.P(NpgsqlDbType.Text, "%" + EscapeLike(search.NameContains) + "%")).Append(')');
}

if (search.ColumnName != null)
Expand All @@ -405,10 +405,10 @@ public static async Task<IReadOnlyList<SchemaFoundObject>> FindCurrentAsync(

if (text != null)
{
q.Text.Append($@" AND (o.view_definition ILIKE {text} ESCAPE '\'
OR EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND (x.computed_definition ILIKE {text} ESCAPE '\' OR x.default_definition ILIKE {text} ESCAPE '\'))
OR EXISTS (SELECT 1 FROM collect.schema_constraints x WHERE x.version_id = o.version_id AND x.is_current AND x.definition ILIKE {text} ESCAPE '\')
OR EXISTS (SELECT 1 FROM collect.schema_indexes x WHERE x.version_id = o.version_id AND x.is_current AND x.filter_definition ILIKE {text} ESCAPE '\'))");
q.Text.Append($@" AND (o.view_definition ILIKE {text}
OR EXISTS (SELECT 1 FROM collect.schema_columns x WHERE x.version_id = o.version_id AND x.is_current AND (x.computed_definition ILIKE {text} OR x.default_definition ILIKE {text}))
OR EXISTS (SELECT 1 FROM collect.schema_constraints x WHERE x.version_id = o.version_id AND x.is_current AND x.definition ILIKE {text})
OR EXISTS (SELECT 1 FROM collect.schema_indexes x WHERE x.version_id = o.version_id AND x.is_current AND x.filter_definition ILIKE {text}))");
}

q.Text.Append(" ORDER BY o.database_name, o.schema_name, o.object_name, o.version_id LIMIT ").Append(q.P(NpgsqlDbType.Integer, fetch));
Expand Down
Loading