/** * PostgreSQL 数据库适配器 * 使用 pg 驱动实现 DbAdapter 接口 * * 性能优化:使用批量查询获取 Schema 信息,避免 N+1 查询问题 * 连接管理:使用连接池 + TCP Keep-Alive + 断线自动重试,确保长连接稳定性 */ import { BaseAdapter, ExecuteScriptOptions, ExecuteBatchOptions, TransactionContext, type BatchResult } from './base.js'; import type { QueryResult, SchemaInfo } from '../types/adapter.js'; /** * v5.0.1 Bug N10: 把 MySQL 风格的 `?` 占位符按出现顺序替换为 PG 风格的 `$1..$N`。 * 在 executeBatch 中每行单独调用一次(每行的 N 由该行的 params 数量决定)。 * 测试可独立验证占位符转换的正确性。 */ export declare function pgConvertPlaceholders(sql: string, nParams: number): string; export declare class PostgreSQLAdapter extends BaseAdapter { private pool; private config; constructor(config: { host: string; port: number; user?: string; password?: string; database?: string; poolConfig?: { max?: number; min?: number; idleTimeoutMs?: number; }; }); /** * 连接到 PostgreSQL 数据库 */ connect(): Promise; /** * v3.2.4 Bug #7 fix: retry connect query with exponential backoff. * Cold-start race in pg.Pool after process restart would fail first 4-5 attempts * because: (a) min pool clients haven't been established; (b) DNS / TCP socket * may be in TIME_WAIT from previous session. Backoff gives TCP stack time to settle. */ private connectWithRetry; /** * 断开数据库连接 */ disconnect(): Promise; /** * 执行 SQL 查询 */ executeQuery(query: string, params?: unknown[]): Promise; /** * 获取数据库结构信息(批量查询优化版本) * * 优化前:每个表需要 4 次查询(列、主键、索引、行数) * 优化后:只需要 4 次查询获取所有表的信息 */ getSchema(): Promise; /** * getSchema 内部实现 */ private _getSchemaImpl; /** * 构建带 schema 前缀的表名键 * 默认 schema (public) 的表直接用表名,保持向后兼容 */ private makeTableKey; /** * 组装 Schema 信息 */ private assembleSchema; /** * 检查是否为写操作 */ isWriteOperation(query: string): boolean; protected getDialect(): import('../utils/adapter-factory.js').DbType; /** * P0-3: Override withTransaction to use a single physical client from the pool. */ withTransaction(fn: (tx: TransactionContext) => Promise): Promise; /** * v5.0.1 Bug N10: PG `pg` driver 只支持 `$1, $2, ...` 数字占位符,不支持 MySQL 风格的 `?`。 * csv-reader.ts:244 为所有非 ClickHouse DB 生成 `?` 占位符,默认走 BaseAdapter.executeBatch * 把 SQL 原样发给 PG → "syntax error at or near ','"。 * 修复: 重写 executeBatch,把每行 SQL 中的 `?` 替换为 `$1..$N`,沿用 withTransaction * 单 client + 多语句的模式。 */ executeBatch(sql: string, paramsList: unknown[][], options?: ExecuteBatchOptions): Promise; executeScript(query: string, options?: ExecuteScriptOptions): Promise; } //# sourceMappingURL=postgres.d.ts.map