Skip to content

fix(postgres): tolerate sources that reject SET statement_timeout - #70311

Merged
Gilbert09 merged 2 commits into
masterfrom
posthog-code/postgres-set-statement-timeout-unsupported
Jul 13, 2026
Merged

fix(postgres): tolerate sources that reject SET statement_timeout#70311
Gilbert09 merged 2 commits into
masterfrom
posthog-code/postgres-set-statement-timeout-unsupported

Conversation

@Gilbert09

Copy link
Copy Markdown
Member

Problem

Error tracking surfaced a Postgres data-warehouse import failing repeatedly with:

FeatureNotSupported: setting configuration parameter "statement_timeout" not supported

Error tracking issue: https://us.posthog.com/project/2/error_tracking/019f57ed-b58f-7421-badd-25948364f78d

The real stack originates in the Postgres source's row-serving setup (postgres_sourcesource_for_pipeline), where we run a session-level SET statement_timeout on the setup connection before streaming rows. Some Postgres-compatible engines and connection poolers speak the wire protocol but reject SET statement_timeout, raising FeatureNotSupported (SQLSTATE 0A000).

That SET was the one unguarded SET statement_timeout call left in this file. The three other sites (the metadata probes in _get_table and _get_columns_for_tables, and the streaming-connection setup) already treat it as best-effort and fall back to the source's default. The row-serving setup site did not, so the error escaped the setup error handling, matched no get_non_retryable_errors substring, and Temporal retried an import that can never succeed.

The statement_timeout we set here is a safety net, not a correctness requirement. Crashing the whole sync because a source won't accept it is strictly worse than running without it.

Changes

Wrap the row-serving SET statement_timeout in the same best-effort guard the metadata sites use:

  • On FeatureNotSupported (or any other non-drop psycopg.Error), log at debug and fall back to the source's default timeout instead of failing the sync.
  • A genuine connection drop or connection-limit error stringifies differently, so those still re-raise and let the setup retry recover on a fresh connection rather than silently losing the timeout guard.

This is a fixable-bug fix (missing graceful-skip), not a NonRetryableErrors change. The connection is autocommit, so no rollback is needed.

How did you test this code?

I'm an agent. I added a regression test (TestSetupStatementTimeoutUnsupported) that drives postgres_source through setup with a mock connection whose setup cursor rejects SET statement_timeout with FeatureNotSupported, and asserts the import completes and streams to exhaustion instead of raising. I verified it fails against the unfixed code (raises FeatureNotSupported at the SET statement_timeout site) and passes with the fix.

.venv/bin/python -m pytest products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py -k "statement_timeout or SetupStatementTimeout or SetupRecoveryConflict or ServerCursor or NonRetryable"
# 122 passed, 441 deselected, 3 errors

The 3 errors are pre-existing environment failures — those tests need a live Postgres connection that isn't available in this sandbox (DNS "Name or service not known"), unrelated to this change. ruff check and ruff format are clean.

🤖 Agent context

Autonomy: Fully autonomous

Authored by an agent triaging a live error-tracking issue for the Postgres warehouse source. I confirmed the crash originates in the source's own setup code (postgres_source at the SET statement_timeout call, into psycopg execute), not in serialized log context. I classified it as a fixable bug rather than a NonRetryableErrors addition because the other three SET statement_timeout sites in the same file already fall back gracefully — this site simply missed the guard. Invoked the /writing-tests skill before adding the regression test. Checked open PRs (including #66527, which handles a different FeatureNotSupported — a customer view expression matched on not supported for type — and #70293, a setup commit-nesting fix) and confirmed neither addresses this error.


Created with PostHog Code

The row-serving setup connection in postgres_source runs a session-level SET
statement_timeout before streaming. Some Postgres-compatible engines and poolers
reject it with FeatureNotSupported, which escaped the setup error handling and
matched no non-retryable substring, so Temporal retried an import that could never
succeed.

Wrap the SET as best-effort, mirroring the metadata-probe sites in _get_table and
_get_columns_for_tables: fall back to the source's default timeout on
FeatureNotSupported, while still re-raising genuine connection drop/limit errors so
the setup retry recovers on a fresh connection.

Generated-By: PostHog Code
Task-Id: b35a78a8-a943-46f1-908a-38022bf8acf9
@github-actions

Copy link
Copy Markdown
Contributor

Hey @Gilbert09! 👋

It looks like your git author email on this PR isn't your @posthog.com address (owerstom@gmail.com). Since you're on the PostHog team, it's worth pointing your local git author email at your @posthog.com address. Why it matters:

  • Consistent work identity in git history — internal tooling that attributes commits to team members keys off your @posthog.com address.
  • Keeps team contributions easy to tell apart from external community ones when scanning history.

You can fix it for this repo with:

git config user.email "you@posthog.com"

Or set it globally with git config --global user.email "you@posthog.com". No need to redo this PR — just a nudge for next time. 🙂

@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team July 12, 2026 20:12
@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(postgres): tolerate sources that rej..." | Re-trigger Greptile

@trunk-io

trunk-io Bot commented Jul 12, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@Gilbert09 Gilbert09 added the stamphog Request AI approval (no full review) label Jul 12, 2026 — with PostHog
@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

🤖 CI report

Playwright — all passed

All tests passed.

View test results →

⚠️ Backend snapshots — 2 updated (2 modified, 0 added, 0 deleted)

Query snapshots: Backend query snapshots updated

Changes: 2 snapshots (2 modified, 0 added, 0 deleted)

What this means:

  • Query snapshots have been automatically updated to match current output
  • These changes reflect modifications to database queries or schema

Next steps:

  • Review the query changes to ensure they're intentional
  • If unexpected, investigate what caused the query to change

Review snapshot changes →

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contained, well-tested fix: wraps one previously-unguarded SET statement_timeout call in the same best-effort error handling already used at three other sites in this file, reusing existing helper functions; the bulk of the diff is unrelated regenerated test snapshots. Not risky territory (data-warehouse batch import setup, not auth/billing/schema/API/ingestion-path), author is on the owning team, and the added regression test demonstrates the fix.

  • Author wrote 1% of the modified lines and has 38 merged PRs in these paths (familiarity MODERATE).
  • 👍 on the PR from greptile-apps[bot], hex-security-app[bot].
Gate mechanics and policy version
Gate Result
prerequisites all clear
deny-list no deny categories matched
size 19L, 1F substantive, 14086L/4F incl. docs/generated/snapshots — within ceiling
tier T1-agent / T1d-complex (14086L, 4F, two-areas, fix)
stamphog 2.0.0b3 .stamphog/policy.yml @ be35044 · reviewed head 23556ab

@Gilbert09
Gilbert09 merged commit a99b200 into master Jul 13, 2026
236 checks passed
@Gilbert09
Gilbert09 deleted the posthog-code/postgres-set-statement-timeout-unsupported branch July 13, 2026 10:00
@deployment-status-posthog

deployment-status-posthog Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-07-13 11:21 UTC Run
prod-us ✅ Deployed 2026-07-13 11:34 UTC Run
prod-eu ✅ Deployed 2026-07-13 11:36 UTC Run

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

Labels

stamphog Request AI approval (no full review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant