Files
orca/cloud/apps/relay/src/database/postgres-query-failure.ts
T
Neil 46fa97a6b4 refactor(relay): group route, forwarding and connection owners (#38374)
* refactor(relay): group route, forwarding and connection owners

* refactor(relay): use explicit defining-owner type imports
2026-11-04 21:59:50 -08:00

65 lines
1.8 KiB
TypeScript

import { isPostgresPoolConnectTimeout } from './postgres-pool-pressure.js'
type QueryFailurePhase = 'acquire' | 'execute'
const ERROR_CODES = new Set([
'57014',
'55P03',
'40P01',
'40001',
'53300',
'57P01',
'57P02',
'57P03',
'08000',
'08001',
'08003',
'08006',
'ECONNRESET',
'ECONNREFUSED',
'ETIMEDOUT',
'EPIPE'
])
// A recognised SQLSTATE or errno, or 'unknown': whatever else a driver attached
// to `code` is not a bounded log category.
export function postgresErrorCodeCategory(error: unknown): string {
const code =
typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined
return typeof code === 'string' && ERROR_CODES.has(code) ? code : 'unknown'
}
export function reportPostgresQueryFailure(input: {
error: unknown
phase: QueryFailurePhase
sql: string
// The routing verdict, supplied by the caller that owns it.
transient: boolean
elapsedMs: number
pool: { totalCount: number; idleCount: number; waitingCount: number }
}): void {
// Emit only bounded categories: error messages and SQL can contain credentials or identities.
try {
const code = postgresErrorCodeCategory(input.error)
const connectionTimeout = isPostgresPoolConnectTimeout(input.error)
console.warn(
JSON.stringify({
event: 'orca_relay_postgres_query_failed',
phase: input.phase,
operation: /^\s*WITH\s+assignment_state\s+AS\s+MATERIALIZED\b/i.test(input.sql)
? 'control-renewal'
: 'other',
code,
connectionTimeout,
transient: input.transient,
elapsedMs: Math.max(0, Math.round(input.elapsedMs)),
poolTotal: input.pool.totalCount,
poolIdle: input.pool.idleCount,
poolWaiting: input.pool.waitingCount
})
)
} catch {
// Diagnostics must not replace the original database failure.
}
}