Skip to content

postgres: fix ERROR-level log spam from explain_parameterized_queries on type mismatch#23993

Open
pierreln-dd wants to merge 6 commits into
masterfrom
pierreln-dd/SDBM-2665-fix-type-mismatch-error-logs-v2
Open

postgres: fix ERROR-level log spam from explain_parameterized_queries on type mismatch#23993
pierreln-dd wants to merge 6 commits into
masterfrom
pierreln-dd/SDBM-2665-fix-type-mismatch-error-logs-v2

Conversation

@pierreln-dd

@pierreln-dd pierreln-dd commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Catch psycopg.errors.UndefinedFunction and psycopg.errors.DatatypeMismatch inside _explain_prepared_statement before they escape to the @tracked_method decorator. Log at DEBUG, return None as a sentinel. explain_statement maps NoneDBExplainError.undefined_function so the failure remains observable in collection_errors telemetry.

Motivation

When explain_parameterized_queries: true is enabled and the agent runs EXPLAIN EXECUTE dd_X(null, ...) against a query that compares a non-text column to an untyped $N parameter, PostgreSQL raises UndefinedFunction (SQLSTATE 42883). This escaped _explain_prepared_statement to the @tracked_method decorator, which calls check.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_statement but left the EXECUTE phase unpatched. This PR closes that gap.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add qa/required if this PR needs QA validation, or qa/skip-qa if it does not. Exactly one of the two is required.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jun 10, 2026

Copy link
Copy Markdown

Tests  Code Coverage

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 94.44%
Overall Coverage: 94.07% (+6.35%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: ea516ad | Docs | Datadog PR Page | Give us feedback!

@pierreln-dd pierreln-dd added the qa/skip-qa Automatically skip this PR for the next QA label Jun 10, 2026
pierreln-dd and others added 3 commits June 10, 2026 13:22
… 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>
@pierreln-dd pierreln-dd force-pushed the pierreln-dd/SDBM-2665-fix-type-mismatch-error-logs-v2 branch from 48fca85 to 819322a Compare June 10, 2026 11:22
pierreln-dd and others added 3 commits June 10, 2026 13:30
…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>
@dd-octo-sts

dd-octo-sts Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Validation Report

All 21 validations passed.

Show details
Validation Description Status
agent-reqs Verify check versions match the Agent requirements file
ci Validate CI configuration and code coverage settings
codeowners Validate every integration has a CODEOWNERS entry
config Validate default configuration files against spec.yaml
dep Verify dependency pins are consistent and Agent-compatible
http Validate integrations use the HTTP wrapper correctly
imports Validate check imports do not use deprecated modules
integration-style Validate check code style conventions
jmx-metrics Validate JMX metrics definition files and config
labeler Validate PR labeler config matches integration directories
legacy-signature Validate no integration uses the legacy Agent check signature
license-headers Validate Python files have proper license headers
licenses Validate third-party license attribution list
metadata Validate metadata.csv metric definitions
models Validate configuration data models match spec.yaml
openmetrics Validate OpenMetrics integrations disable the metric limit
package Validate Python package metadata and naming
qa-label Validate the pull request declares whether it needs QA for the next Agent release
readmes Validate README files have required sections
saved-views Validate saved view JSON file structure and fields
version Validate version consistency between package and changelog

View full run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant