/**
 * Database connections
 */
import { Pool } from 'pg';
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
/** Database type for Drizzle ORM */
export type Database = NodePgDatabase;
import { InstrumentedPool } from './db-metrics.js';
/** Default statement timeout for regular queries (30 seconds) */
export declare const DEFAULT_STATEMENT_TIMEOUT_MS = 30000;
/** Extended timeout for long-running queries like reports/exports (2 minutes) */
export declare const LONG_QUERY_TIMEOUT_MS = 120000;
/** Short timeout for health checks and quick operations (5 seconds) */
export declare const SHORT_QUERY_TIMEOUT_MS = 5000;
/**
 * Options for database query execution
 */
export interface QueryOptions {
    /** Statement timeout in milliseconds */
    statementTimeout?: number;
}
/**
 * Lazily create Drizzle database connection backed by pg Pool.
 */
export declare function getDatabase(): NodePgDatabase;
/**
 * Get the instrumented pool for direct query execution with metrics.
 * Returns null if database has not been initialized.
 */
export declare function getInstrumentedPool(): InstrumentedPool | null;
/**
 * Get the raw pool for direct access (use sparingly).
 * Returns null if database has not been initialized.
 */
export declare function getPool(): Pool | null;
/**
 * Close pool (mainly for tests).
 */
export declare function closeDatabase(): Promise<void>;
/**
 * Check database health by running a simple query.
 * Returns true if the database is healthy, false otherwise.
 *
 * @param timeoutMs - Optional timeout in milliseconds (defaults to config value)
 */
export declare function checkDatabaseHealth(timeoutMs?: number): Promise<{
    healthy: boolean;
    latencyMs?: number;
    error?: string;
    timedOut?: boolean;
}>;
/**
 * Error thrown when a database statement exceeds its timeout.
 * PostgreSQL error code 57014 (query_canceled) is returned when statement_timeout is exceeded.
 */
export declare class StatementTimeoutError extends Error {
    readonly code = "STATEMENT_TIMEOUT";
    readonly timeoutMs: number;
    readonly operation: string;
    constructor(message: string, timeoutMs: number, operation: string);
}
/**
 * Check if an error is a PostgreSQL statement timeout error.
 * PostgreSQL returns error code 57014 (query_canceled) when statement_timeout is exceeded.
 */
export declare function isStatementTimeoutError(error: unknown): boolean;
/**
 * Execute a database operation with a specific statement timeout.
 * Uses PostgreSQL's SET LOCAL statement_timeout within a transaction
 * to apply the timeout only to the current operation.
 *
 * @param fn - The database operation to execute
 * @param timeoutMs - Statement timeout in milliseconds
 * @param operationName - Name of the operation for metrics/logging
 * @returns The result of the database operation
 * @throws StatementTimeoutError if the operation exceeds the timeout
 *
 * @example
 * ```typescript
 * // Regular query with default timeout
 * const intents = await withStatementTimeout(
 *   async () => db.select().from(intents).limit(100),
 *   DEFAULT_STATEMENT_TIMEOUT_MS,
 *   'listIntents'
 * );
 *
 * // Long-running export with extended timeout
 * const exportData = await withStatementTimeout(
 *   async () => db.select().from(userDataExport).where(...),
 *   LONG_QUERY_TIMEOUT_MS,
 *   'exportAllUserData'
 * );
 * ```
 */
export declare function withStatementTimeout<T>(fn: () => Promise<T>, timeoutMs?: number, operationName?: string): Promise<T>;
/**
 * Execute a database operation with the long query timeout.
 * Convenience wrapper for operations like reports and data exports.
 *
 * @param fn - The database operation to execute
 * @param operationName - Name of the operation for metrics/logging
 * @returns The result of the database operation
 */
export declare function withLongQueryTimeout<T>(fn: () => Promise<T>, operationName?: string): Promise<T>;
//# sourceMappingURL=db.d.ts.map