From a4d083351ac359c7ac7efe56fd27306b1797f592 Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Sat, 21 Nov 2026 02:52:45 -0500 Subject: [PATCH] Drop ESCAPE '\' from the store's own PostgreSQL statements so the log can name them (#9236) --- .../PgStatementsReadableInTheLogTests.cs | 51 +++++++++++++++++++ .../ServerDatabasesEndpointLiveTests.cs | 4 +- .../Mcp/DarlingMcpFindQueriesByTextTools.cs | 12 ++--- .../PgSettingScrub.cs | 9 ++-- .../CollectedDatabases.cs | 4 +- .../PgAlertHistoryStore.cs | 2 +- .../SchemaHistoryReader.cs | 22 ++++---- 7 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 Darling/Darling.Tests/PgStatementsReadableInTheLogTests.cs diff --git a/Darling/Darling.Tests/PgStatementsReadableInTheLogTests.cs b/Darling/Darling.Tests/PgStatementsReadableInTheLogTests.cs new file mode 100644 index 0000000000..9d6d06f50f --- /dev/null +++ b/Darling/Darling.Tests/PgStatementsReadableInTheLogTests.cs @@ -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; + +/// +/// #9236: withholds a statement whole when an odd run of backslashes +/// stands right before a quote, which is the shape of ESCAPE '\'. 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. +/// +public sealed class PgStatementsReadableInTheLogTests +{ + public static IEnumerable 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"); + } +} diff --git a/Darling/Darling.Tests/ServerDatabasesEndpointLiveTests.cs b/Darling/Darling.Tests/ServerDatabasesEndpointLiveTests.cs index 4171181e2a..c1a6dd8c86 100644 --- a/Darling/Darling.Tests/ServerDatabasesEndpointLiveTests.cs +++ b/Darling/Darling.Tests/ServerDatabasesEndpointLiveTests.cs @@ -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); } - /// #5314: the search read is the shared list narrowed by one ILIKE against $3 with a backslash escape, ahead of + /// #5314: the search read is the shared list narrowed by one ILIKE 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. [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); diff --git a/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpFindQueriesByTextTools.cs b/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpFindQueriesByTextTools.cs index dcbddcd396..362f539ef9 100644 --- a/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpFindQueriesByTextTools.cs +++ b/Darling/PerformanceMonitor.Darling.Service/Mcp/DarlingMcpFindQueriesByTextTools.cs @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 ( @@ -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"; /// #7849: the sample read for a pattern that matched at most texts, one span of the raw window (see diff --git a/Darling/PerformanceMonitor.Darling.Service/PgSettingScrub.cs b/Darling/PerformanceMonitor.Darling.Service/PgSettingScrub.cs index 8d490e0693..5843a8aeff 100644 --- a/Darling/PerformanceMonitor.Darling.Service/PgSettingScrub.cs +++ b/Darling/PerformanceMonitor.Darling.Service/PgSettingScrub.cs @@ -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; %/_ are literal wildcards, no escaping needed here /// because none of these terms contain a literal % or _ character themselves (the - /// percent-encoding catch-all is expressed separately in with its own ESCAPE - /// clause). This is only half of the coarse filter — CandidateSql also selects on the setting's + /// percent-encoding catch-all is expressed separately in , 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 — CandidateSql also selects on the setting's /// NAME (ssl_passphrase_command by exact name, or a dotted extension name containing one of /// ), which the census test checks separately by name. /// @@ -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() { @@ -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}))"; diff --git a/Darling/PerformanceMonitor.Darling.Storage/CollectedDatabases.cs b/Darling/PerformanceMonitor.Darling.Storage/CollectedDatabases.cs index f7db30a6c1..0457990d62 100644 --- a/Darling/PerformanceMonitor.Darling.Storage/CollectedDatabases.cs +++ b/Darling/PerformanceMonitor.Darling.Storage/CollectedDatabases.cs @@ -55,12 +55,12 @@ AND database_name NOT IN ('master', 'model', 'msdb', 'tempdb') /// /// narrowed to the names that contain a search text: $3 is the LIKE pattern /// builds (one text parameter, matched case-insensitively with ILIKE, with - /// backslash as the escape character so %, _ and \ in the text match only themselves). The + /// PostgreSQL's default LIKE escape character, the backslash, so %, _ and \ 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. /// 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"; diff --git a/Darling/PerformanceMonitor.Darling.Storage/PgAlertHistoryStore.cs b/Darling/PerformanceMonitor.Darling.Storage/PgAlertHistoryStore.cs index 5dccf1a333..82e41344ca 100644 --- a/Darling/PerformanceMonitor.Darling.Storage/PgAlertHistoryStore.cs +++ b/Darling/PerformanceMonitor.Darling.Storage/PgAlertHistoryStore.cs @@ -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) diff --git a/Darling/PerformanceMonitor.Darling.Storage/SchemaHistoryReader.cs b/Darling/PerformanceMonitor.Darling.Storage/SchemaHistoryReader.cs index b8ea2152a2..287bf58dff 100644 --- a/Darling/PerformanceMonitor.Darling.Storage/SchemaHistoryReader.cs +++ b/Darling/PerformanceMonitor.Darling.Storage/SchemaHistoryReader.cs @@ -358,11 +358,11 @@ public static async Task> 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 { @@ -377,12 +377,12 @@ public static async Task> 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) @@ -405,10 +405,10 @@ public static async Task> 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));