postgres: fix ERROR-level log spam from explain_parameterized_queries on type mismatch#23993
Open
pierreln-dd wants to merge 6 commits into
Open
postgres: fix ERROR-level log spam from explain_parameterized_queries on type mismatch#23993pierreln-dd wants to merge 6 commits into
pierreln-dd wants to merge 6 commits into
Conversation
pierreln-dd
added a commit
that referenced
this pull request
Jun 10, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: ea516ad | Docs | Datadog PR Page | Give us feedback! |
… on type mismatch UndefinedFunction and DatatypeMismatch raised during EXPLAIN EXECUTE (when _explain_prepared_statement runs with untyped NULL params against non-text columns) previously escaped the function body to @tracked_method, which logged at ERROR with no rate limiting and no _explain_errors_cache write — causing hundreds of thousands of ERROR logs per day on ORM-generated queries. Catch these errors before they reach the decorator: log at DEBUG, return None as a sentinel. explain_statement maps None -> DBExplainError.undefined_function so the failure stays observable in collection_errors telemetry, consistent with how the PREPARE phase handles the same error class (PRs #19969/#19998). The April 2025 PREPARE-phase fixes left the EXECUTE phase unpatched; this closes that gap. Confirmed unfixed through current master (HEAD 22faef8). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
_execute_query_and_fetch_rows returns list, never None (cursor.fetchall() always returns a list). Update the mock to return_value=[] so the test correctly exercises the no_plan_returned_with_prepared_statement path, which the result is None sentinel no longer handles. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
48fca85 to
819322a
Compare
…in_statement Verify that explain_statement maps _explain_prepared_statement's None return value to DBExplainError.undefined_function so type-mismatch failures are observable in collection_errors telemetry. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
explain_statement checks self._check.version < V12 and opens a db_pool connection. Unit test context provides neither; mock both so the test runs without a real Postgres instance. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Validation ReportAll 21 validations passed. Show details
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Catch
psycopg.errors.UndefinedFunctionandpsycopg.errors.DatatypeMismatchinside_explain_prepared_statementbefore they escape to the@tracked_methoddecorator. Log at DEBUG, returnNoneas a sentinel.explain_statementmapsNone→DBExplainError.undefined_functionso the failure remains observable incollection_errorstelemetry.Motivation
When
explain_parameterized_queries: trueis enabled and the agent runsEXPLAIN EXECUTE dd_X(null, ...)against a query that compares a non-text column to an untyped$Nparameter, PostgreSQL raisesUndefinedFunction(SQLSTATE 42883). This escaped_explain_prepared_statementto the@tracked_methoddecorator, which callscheck.log.exception()at ERROR with no rate limiting — one ERROR per explain attempt per unique query signature.The April 2025 PREPARE-phase fixes (#19969 / #19998) patched the same error classes in
_create_prepared_statementbut left the EXECUTE phase unpatched. This PR closes that gap.Review checklist (to be filled by reviewers)
qa/requiredif this PR needs QA validation, orqa/skip-qaif it does not. Exactly one of the two is required.backport/<branch-name>label to the PR and it will automatically open a backport PR once this one is merged