-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathPostgresDriver.ts
More file actions
514 lines (439 loc) · 16.7 KB
/
PostgresDriver.ts
File metadata and controls
514 lines (439 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
* @copyright Cube Dev, Inc.
* @license Apache-2.0
* @fileoverview The `PostgresDriver` and related types declaration.
*/
import { getEnv, assertDataSource, Pool, type PoolUserOptions } from '@cubejs-backend/shared';
import { types, FieldDef } from 'pg';
// eslint-disable-next-line import/no-extraneous-dependencies
import { TypeId, TypeFormat } from 'pg-types';
import {
BaseDriver,
DownloadQueryResultsOptions, DownloadTableMemoryData, DriverInterface,
GenericDataBaseType, IndexesSQL, TableStructure, StreamOptions,
StreamTableDataWithTypes, QueryOptions, DownloadQueryResultsResult, DriverCapabilities, TableColumn, createPoolName,
} from '@cubejs-backend/base-driver';
import { QueryStream } from './QueryStream';
import { PgClient, PgClientConfig } from './PgClient';
import { ConnectionError, PostgresError } from './errors';
import { dateTypeParser, timestampTypeParser, timestampTzTypeParser } from './type-parsers';
const GenericTypeToPostgres: Record<GenericDataBaseType, string> = {
string: 'text',
double: 'decimal',
int: 'int8',
// Revert mapping for internal pre-aggregations
HLL_POSTGRES: 'hll',
};
const NativeTypeToPostgresType: Record<string, string> = {};
Object.entries(types.builtins).forEach(([key, value]) => {
NativeTypeToPostgresType[value] = key;
});
// pg-types lacks the default `unknown` type since it's a pseudo-type
NativeTypeToPostgresType['705'] = 'UNKNOWN';
const PostgresToGenericType: Record<string, GenericDataBaseType> = {
// bpchar (“blank-padded char”, the internal name of the character data type)
bpchar: 'varchar',
// External mapping
hll: 'HLL_POSTGRES',
};
const hllTypeParser = (val: string) => Buffer.from(
// Postgres uses prefix as \x for encoding
val.slice(2),
'hex'
).toString('base64');
export type PostgresDriverConfiguration = PgClientConfig & PoolUserOptions & {
// @deprecated Please use maxPoolSize
max?: number | undefined;
// @deprecated Please use minPoolSize
min?: number | undefined;
storeTimezone?: string,
executionTimeout?: number,
readOnly?: boolean,
/**
* The export bucket CSV file escape symbol.
*/
exportBucketCsvEscapeSymbol?: string,
};
/**
* Postgres driver class.
*/
export class PostgresDriver<Config extends PostgresDriverConfiguration = PostgresDriverConfiguration>
extends BaseDriver implements DriverInterface {
/**
* Returns default concurrency value.
*/
public static getDefaultConcurrency(): number {
return 2;
}
private enabled: boolean = false;
protected readonly pool: Pool<PgClient>;
protected readonly config: Partial<Config>;
public constructor(
config: Partial<Config> & {
/**
* Data source name.
*/
dataSource?: string,
/**
* Whether this driver is used for pre-aggregations.
*/
preAggregations?: boolean,
/**
* Max pool size value for the [cube]<-->[db] pool.
*/
maxPoolSize?: number,
/**
* Min pool size value for the [cube]<-->[db] pool.
*/
minPoolSize?: number,
/**
* Time to wait for a response from a connection after validation
* request before determining it as not valid. Default - 10000 ms.
*/
testConnectionTimeout?: number,
} = {}
) {
super({
testConnectionTimeout: config.testConnectionTimeout,
});
const dataSource =
config.dataSource ||
assertDataSource('default');
const preAggregations = config.preAggregations || false;
const poolConfig: PgClientConfig = {
host: getEnv('dbHost', { dataSource, preAggregations }),
database: getEnv('dbName', { dataSource, preAggregations }),
port: getEnv('dbPort', { dataSource, preAggregations }),
user: getEnv('dbUser', { dataSource, preAggregations }),
password: getEnv('dbPass', { dataSource, preAggregations }),
ssl: this.getSslOptions(dataSource, preAggregations),
...config
};
const poolName = createPoolName('postgres', dataSource, preAggregations);
this.pool = new Pool<PgClient>(poolName, {
create: async () => this.createConnection(poolConfig, poolName),
validate: async (client) => {
if (client.isEnding() || client.isEnded()) {
return false;
}
return client.isQueryable();
},
destroy: async (client) => {
await client.end();
},
}, {
min: config.minPoolSize ||
config.min ||
getEnv('dbMinPoolSize', { dataSource, preAggregations }) ||
0,
max:
config.maxPoolSize ||
config.max ||
getEnv('dbMaxPoolSize', { dataSource, preAggregations }) ||
8,
evictionRunIntervalMillis: config.evictionRunIntervalMillis || 10000,
softIdleTimeoutMillis: config.softIdleTimeoutMillis || 30000,
idleTimeoutMillis: config.idleTimeoutMillis || 30000,
acquireTimeoutMillis: config.acquireTimeoutMillis || 20000,
testOnBorrow: true,
});
// https://github.com/coopernurse/node-pool/blob/ee5db9ddb54ce3a142fde3500116b393d4f2f755/README.md#L220-L226
this.pool.on('factoryCreateError', (err) => this.databasePoolError(err));
this.pool.on('factoryDestroyError', (err) => this.databasePoolError(err));
this.config = <Partial<Config>>{
...this.getInitialConfiguration(dataSource, preAggregations),
executionTimeout: getEnv('dbQueryTimeout', { dataSource, preAggregations }),
exportBucketCsvEscapeSymbol: getEnv('dbExportBucketCsvEscapeSymbol', { dataSource, preAggregations }),
...config,
};
this.enabled = true;
}
protected async createConnection(poolConfig: PgClientConfig, poolName: string): Promise<PgClient> {
const client = new PgClient(poolConfig);
client.on('error', (err) => this.databasePoolError(err));
try {
await client.connect();
} catch (e: unknown) {
throw new ConnectionError(e as Error, poolName);
}
return client;
}
protected primaryKeysQuery(conditionString?: string): string | null {
return `SELECT
columns.table_schema as ${this.quoteIdentifier('table_schema')},
columns.table_name as ${this.quoteIdentifier('table_name')},
columns.column_name as ${this.quoteIdentifier('column_name')}
FROM information_schema.table_constraints tc
JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS columns ON columns.table_schema = tc.constraint_schema
AND tc.table_name = columns.table_name AND ccu.column_name = columns.column_name
WHERE constraint_type = 'PRIMARY KEY' AND columns.table_schema NOT IN ('pg_catalog', 'information_schema', 'mysql', 'performance_schema', 'sys', 'INFORMATION_SCHEMA')${conditionString ? ` AND (${conditionString})` : ''}`;
}
protected foreignKeysQuery(conditionString?: string): string | null {
return `SELECT
tc.table_schema as ${this.quoteIdentifier('table_schema')},
tc.table_name as ${this.quoteIdentifier('table_name')},
kcu.column_name as ${this.quoteIdentifier('column_name')},
columns.table_name as ${this.quoteIdentifier('target_table')},
columns.column_name as ${this.quoteIdentifier('target_column')}
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS columns
ON columns.constraint_name = tc.constraint_name
WHERE
constraint_type = 'FOREIGN KEY'
AND ${this.getColumnNameForSchemaName()} NOT IN ('pg_catalog', 'information_schema', 'mysql', 'performance_schema', 'sys', 'INFORMATION_SCHEMA')
${conditionString ? ` AND (${conditionString})` : ''}
`;
}
/**
* The easiest way how to add additional configuration from env variables, because
* you cannot call method in RedshiftDriver.constructor before super.
*/
protected getInitialConfiguration(
_dataSource: string,
_preAggregations?: boolean,
): Partial<PostgresDriverConfiguration> {
return {
readOnly: true,
};
}
protected getTypeParser = (dataTypeID: TypeId, format: TypeFormat | undefined) => {
// @link TypeId.DATE
if (dataTypeID === 1082) {
return dateTypeParser;
}
// @link TypeId.TIMESTAMP
if (dataTypeID === 1114) {
return timestampTypeParser;
}
// @link TypeId.TIMESTAMPTZ
if (dataTypeID === 1184) {
return timestampTzTypeParser;
}
const typeName = this.getPostgresTypeForField(dataTypeID);
if (typeName === 'hll') {
// We are using base64 encoding as main format for all HLL sketches, but in pg driver it uses binary encoding
return hllTypeParser;
}
return types.getTypeParser(dataTypeID, format);
};
/**
* It's not possible to detect user defined types via constant oids
* For example HLL extensions is using CREATE TYPE HLL which will generate a new pg_type with different oids
*/
protected userDefinedTypes: Record<string, string> | null = null;
protected getPostgresTypeForField(dataTypeID: number): string | null {
if (dataTypeID in NativeTypeToPostgresType) {
return NativeTypeToPostgresType[dataTypeID].toLowerCase();
}
if (this.userDefinedTypes && dataTypeID in this.userDefinedTypes) {
return this.userDefinedTypes[dataTypeID].toLowerCase();
}
return null;
}
public async testConnection(): Promise<void> {
// eslint-disable-next-line no-underscore-dangle
const conn: PgClient = await this.pool._factory.create();
try {
await conn.query('SELECT $1::int AS number', ['1']);
} catch (e) {
if ((e as Error).toString().indexOf('no pg_hba.conf entry for host') !== -1) {
throw new PostgresError(`Please use CUBEJS_DB_SSL=true to connect: ${(e as Error).toString()}`, { cause: e as Error });
}
throw e;
} finally {
// eslint-disable-next-line no-underscore-dangle
await this.pool._factory.destroy(conn);
}
}
protected async loadUserDefinedTypes(conn: PgClient): Promise<void> {
if (!this.userDefinedTypes) {
// Postgres enum types defined as typcategory = 'E' these can be assumed
// to be of type varchar for the drivers purposes.
// TODO: if full implmentation the constraints can be looked up via pg_enum
// https://www.postgresql.org/docs/9.1/catalog-pg-enum.html
const customTypes = await conn.query(
`SELECT
oid,
CASE
WHEN typcategory = 'E' THEN 'varchar'
ELSE typname
END
FROM
pg_type
WHERE
typcategory in ('U', 'E')`,
[]
);
this.userDefinedTypes = customTypes.rows.reduce(
(prev, current) => ({ [current.oid]: current.typname, ...prev }),
{}
);
}
}
protected async prepareConnection(
conn: PgClient,
options: { executionTimeout: number } = {
executionTimeout: this.config.executionTimeout ? <number>(this.config.executionTimeout) * 1000 : 600000
}
) {
await conn.query(`SET TIME ZONE '${this.config.storeTimezone || 'UTC'}'`);
await conn.query(`SET statement_timeout TO ${options.executionTimeout}`);
await this.loadUserDefinedTypes(conn);
}
protected mapFields(fields: FieldDef[]) {
return fields.map((f) => {
const postgresType = this.getPostgresTypeForField(f.dataTypeID);
if (!postgresType) {
throw new PostgresError(
`Unable to detect type for field "${f.name}" with dataTypeID: ${f.dataTypeID}`
);
}
return ({
name: f.name,
type: this.toGenericType(postgresType)
});
});
}
public async stream(
query: string,
values: unknown[],
{ highWaterMark }: StreamOptions
): Promise<StreamTableDataWithTypes> {
PostgresDriver.checkValuesLimit(values);
const conn = await this.pool.acquire();
try {
await this.prepareConnection(conn);
const queryStream = new QueryStream(query, values, {
types: {
getTypeParser: this.getTypeParser,
},
highWaterMark
});
const rowStream: QueryStream = await conn.query(queryStream);
const fields = await rowStream.fields();
return {
rowStream,
types: this.mapFields(fields),
release: async () => {
await this.pool.release(conn);
}
};
} catch (e) {
await this.pool.release(conn);
throw e;
}
}
protected static checkValuesLimit(values?: unknown[]) {
// PostgreSQL protocol allows sending up to 65535 params in a single bind message
// See https://github.com/postgres/postgres/blob/REL_16_0/src/backend/tcop/postgres.c#L1698-L1708
// See https://github.com/postgres/postgres/blob/REL_16_0/src/backend/libpq/pqformat.c#L428-L431
// But 'pg' module does not check for params count, and ends up sending incorrect bind message
// See https://github.com/brianc/node-postgres/blob/92cb640fd316972e323ced6256b2acd89b1b58e0/packages/pg-protocol/src/serializer.ts#L155
// See https://github.com/brianc/node-postgres/blob/92cb640fd316972e323ced6256b2acd89b1b58e0/packages/pg-protocol/src/buffer-writer.ts#L32-L37
const length = (values?.length ?? 0);
if (length >= 65536) {
throw new PostgresError(`PostgreSQL protocol does not support more than 65535 parameters, but ${length} passed`);
}
}
protected async withConnection<T>(fn: (conn: PgClient) => Promise<T>): Promise<T> {
const conn = await this.pool.acquire();
try {
return await fn(conn);
} finally {
await this.pool.release(conn);
}
}
protected async queryResponse(query: string, values: unknown[]) {
PostgresDriver.checkValuesLimit(values);
return this.withConnection(async (conn) => {
await this.prepareConnection(conn);
const res = await conn.query({
text: query,
values: values || [],
types: {
getTypeParser: this.getTypeParser,
},
});
return res;
});
}
public async createTable(quotedTableName: string, columns: TableColumn[]): Promise<void> {
if (quotedTableName.length > 63) {
throw new PostgresError('PostgreSQL can not work with table names longer than 63 symbols. ' +
`Consider using the 'sqlAlias' attribute in your cube definition for ${quotedTableName}.`);
}
return super.createTable(quotedTableName, columns);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async query<R = unknown>(query: string, values: unknown[], options?: QueryOptions): Promise<R[]> {
const result = await this.queryResponse(query, values);
return result.rows;
}
public async downloadQueryResults(query: string, values: unknown[], options: DownloadQueryResultsOptions): Promise<DownloadQueryResultsResult> {
if (options.streamImport) {
return this.stream(query, values, options);
}
const res = await this.queryResponse(query, values);
return {
rows: res.rows,
types: this.mapFields(res.fields),
};
}
public override async tableColumnTypes(table: string): Promise<TableStructure> {
return this.tableColumnTypesWithPrecision(table);
}
protected override toGenericType(columnType: string, precision?: number | null, scale?: number | null): GenericDataBaseType {
return PostgresToGenericType[columnType.toLowerCase()] || super.toGenericType(columnType, precision, scale);
}
public readOnly() {
return !!this.config.readOnly;
}
public async uploadTableWithIndexes(
table: string,
columns: TableStructure,
tableData: DownloadTableMemoryData,
indexesSql: IndexesSQL
) {
if (!tableData.rows) {
throw new PostgresError(`${this.constructor} driver supports only rows upload`);
}
await this.createTable(table, columns);
try {
await this.query(
`INSERT INTO ${table}
(${columns.map(c => this.quoteIdentifier(c.name)).join(', ')})
SELECT * FROM UNNEST (${columns.map((c, columnIndex) => `${this.param(columnIndex)}::${this.fromGenericType(c.type)}[]`).join(', ')})`,
columns.map(c => tableData.rows.map(r => r[c.name]))
);
for (let i = 0; i < indexesSql.length; i++) {
const [query, p] = indexesSql[i].sql;
await this.query(query, p);
}
} catch (e) {
await this.dropTable(table);
throw e;
}
}
public async release() {
if (this.enabled) {
await this.pool.drain();
await this.pool.clear();
this.enabled = false;
}
}
public param(paramIndex: number) {
return `$${paramIndex + 1}`;
}
public fromGenericType(columnType: string) {
return GenericTypeToPostgres[columnType] || super.fromGenericType(columnType);
}
public capabilities(): DriverCapabilities {
return {
incrementalSchemaLoading: true,
};
}
}