Skip to content

fix(backend): split Drizzle error fingerprints and handle transient PG failures - #3183

Open
riderx wants to merge 6 commits into
mainfrom
cursor/fix-notification-settings-drizzle-error-b8ea
Open

fix(backend): split Drizzle error fingerprints and handle transient PG failures#3183
riderx wants to merge 6 commits into
mainfrom
cursor/fix-notification-settings-drizzle-error-b8ea

Conversation

@riderx

@riderx riderx commented Aug 23, 2026

Copy link
Copy Markdown
Member

Summary (AI generated)

  • Investigated PostHog issue 019f3c00-5ad8-7e12-881d-743797f0bbc4 reporting ~5.5k DrizzleQueryError hits on notification_app_settings
  • Found only 5 real notification_app_settings failures in the last 90 days (all GET /notifications/settings); ~32.7k sibling events were manifest query failures grouped into the same issue
  • No schema mismatch or missing-row bug: getNotificationSettings() already returns defaults when no row exists
  • Added shared pg_errors helpers and fixed PostHog exception fingerprinting to include the queried table / PG code
  • Transient Drizzle/PG connection failures now return 503 upstream_unavailable without Discord/PostHog alerts
  • Included local Supabase port 54322 in PG connect allowlist for dev/CI transient DB detection

Motivation (AI generated)

The PostHog issue looked like a high-volume notification_app_settings regression, but the inflated count came from overly broad Drizzle exception fingerprints that merged unrelated failed queries (mostly manifest). The handful of real notification settings failures were sporadic transient Postgres connection errors, not a logic bug in getNotificationSettings().

Business Impact (AI generated)

  • Stops misleading error-tracking noise that hid the true scale of notification settings failures
  • Gives operators PG cause codes on non-transient Drizzle errors for faster triage
  • Lets dashboard clients retry notification settings loads cleanly on transient DB blips instead of treating them as hard 500s

Test Plan (AI generated)

  • bunx vitest run tests/pg-errors.unit.test.ts tests/on-error-posthog.unit.test.ts tests/posthog.unit.test.ts tests/rbac-permission-infra-errors.unit.test.ts
  • bun lint
  • CI green (including Capgo CLI integration tests)

Generated with AI

Open in Web Open in Cursor 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Review in cubic

@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 26 days. After that, they cost $0.25 per reviewed file.

Or wait 7 minutes for your next included review.

View limit details

Limit details: You’ve used all 2 included reviews currently available. Your 53 included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d914ebc6-edcc-45a0-b653-9a5ea36d3bcb

📥 Commits

Reviewing files that changed from the base of the PR and between dc57849 and 0afb7a3.

📒 Files selected for processing (10)
  • supabase/functions/_backend/plugin_runtime/utils/on_error.ts
  • supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts
  • supabase/functions/_backend/plugin_runtime/utils/posthog.ts
  • supabase/functions/_backend/utils/on_error.ts
  • supabase/functions/_backend/utils/pg_errors.ts
  • supabase/functions/_backend/utils/posthog.ts
  • supabase/functions/_backend/utils/rbac.ts
  • tests/on-error-posthog.unit.test.ts
  • tests/pg-errors.unit.test.ts
  • tests/posthog.unit.test.ts

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 43 untouched benchmarks
⏩ 2 skipped benchmarks1


Comparing cursor/fix-notification-settings-drizzle-error-b8ea (0afb7a3) with main (dc57849)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@riderx
riderx marked this pull request as ready for review August 23, 2026 23:15

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 10 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread supabase/functions/_backend/plugin_runtime/utils/on_error.ts Outdated
Comment thread supabase/functions/_backend/utils/posthog.ts Outdated
Comment thread supabase/functions/_backend/utils/posthog.ts
Comment thread supabase/functions/_backend/plugin_runtime/utils/posthog.ts
Comment thread supabase/functions/_backend/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
Comment thread tests/posthog.unit.test.ts Outdated

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 10 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread supabase/functions/_backend/plugin_runtime/utils/on_error.ts Outdated
Comment thread supabase/functions/_backend/utils/on_error.ts Outdated
Comment thread supabase/functions/_backend/utils/on_error.ts Outdated

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 6 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts
@cursor
cursor Bot deployed to deepsec-pr August 25, 2026 01:12 Active

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread supabase/functions/_backend/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
@cursor
cursor Bot deployed to deepsec-pr August 25, 2026 01:20 Active

@cubic-dev-ai cubic-dev-ai 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.

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread supabase/functions/_backend/utils/pg_errors.ts Outdated
Comment thread supabase/functions/_backend/plugin_runtime/utils/pg_errors.ts Outdated
cursoragent and others added 5 commits August 25, 2026 09:14
…G failures

PostHog grouped unrelated DrizzleQueryErrors (manifest vs
notification_app_settings) because exception fingerprints ignored the
failed query. notification_app_settings only had 5 real failures in 90
days; the rest were manifest query noise.

- Add shared pg_errors helpers for transient detection and table-based
  Drizzle fingerprints
- Return 503 upstream_unavailable for transient Drizzle errors without
  Discord/PostHog alerts
- Attach pg_error_cause to PostHog exception payloads for faster triage

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
- Classify transient native pg errors before the Drizzle guard
- Fingerprint Drizzle errors by table plus PostgreSQL code
- Parse quoted schema-qualified table names correctly
- Send only pg_error_code to PostHog (no user-supplied cause text)
- Drop 08P01 from transient SQLSTATE set (protocol violations stay visible)

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
- Add isDatabaseOriginError/isTransientDatabaseError to avoid masking
  unrelated upstream network failures as database outages
- Handle quickError originalCause before HTTP alert path
- Include pgErrorCode in transient error logs for triage

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
- Validate SQLSTATE shape instead of any five-character code
- Detect pg connect failures via syscall/port metadata, not only :5432
- Keep unrelated upstream network and custom error codes out of 503 path

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
…tion

- Replace non-db port denylist with explicit PG port allowlist (5432/6543/6432)
- Match db.*.supabase hostnames instead of any supabase API URL
- Preserve :5432 detection for message-only wrapped connection errors

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
@cursor
cursor Bot force-pushed the cursor/fix-notification-settings-drizzle-error-b8ea branch from 8bf37c4 to 82e23ac Compare August 25, 2026 09:15
@cursor
cursor Bot deployed to deepsec-pr August 25, 2026 09:15 Active
Add 54322 to postgres port detection so transient local database
connection failures still return the retryable 503 path.

Co-authored-by: Martin DONADIEU <martindonadieu@gmail.com>
@cursor
cursor Bot deployed to deepsec-pr August 25, 2026 09:38 Active
@sonarqubecloud

Copy link
Copy Markdown

@TorichanCapgo TorichanCapgo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

CI green, all cubic review threads resolved. Local Supabase port 54322 added to PG connect allowlist.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants