-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathlib.rs
More file actions
458 lines (420 loc) · 16.8 KB
/
Copy pathlib.rs
File metadata and controls
458 lines (420 loc) · 16.8 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
/*
Copyright 2026 The Spice.ai OSS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! `PostgreSQL` data connector for Spice.ai runtime.
//!
//! This crate provides the `PostgreSQL` connector implementation, allowing
//! Spice.ai to connect to `PostgreSQL` databases as data sources. It also
//! exposes a direct WAL-based `ChangesStream`, so users can set
//! `acceleration.refresh_mode: changes` on a Postgres dataset and get
//! change-by-change replication into the local accelerator without Debezium.
use async_trait::async_trait;
use datafusion::datasource::TableProvider;
use datafusion_table_providers::postgres::PostgresTableFactory;
use datafusion_table_providers::sql::db_connection_pool::dbconnection;
use datafusion_table_providers::sql::db_connection_pool::{
Error as DbConnectionPoolError,
postgrespool::{self, PostgresConnectionPool},
};
use runtime::component::dataset::Dataset;
use runtime::component::metrics::MetricsProvider;
use runtime::dataconnector::{
ConnectorComponent, ConnectorParams, DataConnector, DataConnectorError, DataConnectorFactory,
DataConnectorResult, NewDataConnectorResult,
};
use runtime::parameters::ParameterSpec;
use secrecy::SecretBox;
use snafu::prelude::*;
use std::any::Any;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
mod replication;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Unable to create Postgres connection pool: {source}"))]
UnableToCreatePostgresConnectionPool { source: DbConnectionPoolError },
}
/// `PostgreSQL` data connector.
pub struct Postgres {
factory: PostgresTableFactory,
pool: Arc<PostgresConnectionPool>,
params: runtime::parameters::Parameters,
replication_metrics:
std::sync::Arc<data_components::postgres_replication::ReplicationMetricsCollector>,
}
impl std::fmt::Debug for Postgres {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Postgres").finish_non_exhaustive()
}
}
/// Factory for creating `PostgreSQL` connector instances.
#[derive(Default, Copy, Clone)]
pub struct PostgresFactory {}
impl PostgresFactory {
#[must_use]
pub fn new() -> Self {
Self {}
}
#[must_use]
pub fn new_arc() -> Arc<dyn DataConnectorFactory> {
Arc::new(Self {}) as Arc<dyn DataConnectorFactory>
}
}
const POSTGRES_DOCS: &str = "https://spiceai.org/docs/components/data-connectors/postgres";
const PARAMETERS: &[ParameterSpec] = &[
ParameterSpec::component("connection_string")
.description(
"Full libpq-style connection string. Overrides other connection params if set.",
)
.examples(&["host=db.example.com port=5432 dbname=app user=ro sslmode=require"])
.help_link(POSTGRES_DOCS)
.secret(),
ParameterSpec::component("user")
.description("PostgreSQL username.")
.examples(&["postgres", "spice_reader"])
.help_link(POSTGRES_DOCS)
.secret(),
ParameterSpec::component("pass")
.description("PostgreSQL password.")
.help_link(POSTGRES_DOCS)
.secret(),
ParameterSpec::component("host")
.description("PostgreSQL server hostname or IP.")
.examples(&["db.internal", "10.0.0.5"])
.help_link(POSTGRES_DOCS),
ParameterSpec::component("port")
.description("PostgreSQL TCP port.")
.examples(&["5432"])
.help_link(POSTGRES_DOCS),
ParameterSpec::component("db")
.description("Database name.")
.examples(&["app", "analytics"])
.help_link(POSTGRES_DOCS),
ParameterSpec::component("sslmode")
.description("libpq SSL mode: disable, allow, prefer, require, verify-ca, verify-full.")
.one_of(&[
"disable",
"allow",
"prefer",
"require",
"verify-ca",
"verify-full",
])
.help_link(POSTGRES_DOCS),
ParameterSpec::component("sslrootcert")
.description(
"Path to, or inline PEM content for, a CA certificate used when sslmode is verify-ca/verify-full.",
)
.help_link(POSTGRES_DOCS),
ParameterSpec::component("connection_pool_min_idle")
.description("The minimum number of idle connections to keep open in the pool.")
.default("1")
.help_link(POSTGRES_DOCS),
ParameterSpec::runtime("connection_pool_size")
.description("The maximum number of connections created in the connection pool.")
.default("5")
.help_link(POSTGRES_DOCS),
// --- Logical replication (WAL streaming) ---
ParameterSpec::component("replication_slot").description(
"Name of the Postgres replication slot to create/reuse for this dataset. \
Defaults to `spice_<dataset>_<dataset-hash>_<instance-hash>`. Each Spice replica \
MUST have its own unique slot.",
),
ParameterSpec::component("publication").description(
"Name of the Postgres publication to create/reuse for this dataset. \
Defaults to `spice_<dataset>_<dataset-hash>_pub`. Shared across replicas for the \
same dataset.",
),
ParameterSpec::component("replication_initial_snapshot")
.description(
"Whether to take an initial snapshot of the table's existing rows on first \
connection, before streaming WAL changes. Default: true.",
)
.default("true"),
ParameterSpec::component("replication_temporary_slot")
.description(
"If true, create a temporary replication slot that is dropped when the \
Spice process disconnects. Default: false (durable slot).",
)
.default("false"),
ParameterSpec::component("replication_status_interval")
.description(
"How often to send StandbyStatusUpdate to Postgres (e.g. '10s'). \
Default: 10s.",
)
.default("10s"),
ParameterSpec::component("replication_bootstrap_batch_size")
.description(
"Rows per emitted batch during the initial replication snapshot. \
Default: 8192. Maximum: 1048576.",
)
.default("8192"),
];
impl DataConnectorFactory for PostgresFactory {
fn as_any(&self) -> &dyn Any {
self
}
fn create(
&self,
params: ConnectorParams,
) -> Pin<Box<dyn Future<Output = NewDataConnectorResult> + Send>> {
Box::pin(async move {
let mut param_map = params.parameters.to_secret_map();
param_map.insert(
"application_name".to_string(),
SecretBox::from(format!("Spice.ai {}", env!("CARGO_PKG_VERSION"))),
);
let params_for_replication = params.parameters.clone();
match PostgresConnectionPool::new(param_map).await {
Ok(pool) => {
let unsupported_type_action = params
.unsupported_type_action
.unwrap_or(datafusion_table_providers::UnsupportedTypeAction::String);
let pool = pool.with_unsupported_type_action(unsupported_type_action);
let pool = Arc::new(pool);
let factory = PostgresTableFactory::new(Arc::clone(&pool));
Ok(Arc::new(Postgres {
factory,
pool,
params: params_for_replication,
replication_metrics:
data_components::postgres_replication::ReplicationMetricsCollector::new(
),
}) as Arc<dyn DataConnector>)
}
Err(e) => match e {
postgrespool::Error::InvalidUsernameOrPassword { .. } => Err(
DataConnectorError::UnableToConnectInvalidUsernameOrPassword {
dataconnector: "postgres".to_string(),
connector_component: params.component.clone(),
}
.into(),
),
postgrespool::Error::InvalidHostOrPortError {
host,
port,
source: _,
} => Err(DataConnectorError::UnableToConnectInvalidHostOrPort {
dataconnector: "postgres".to_string(),
connector_component: params.component.clone(),
host,
port: format!("{port}"),
}
.into()),
_ => Err(DataConnectorError::UnableToConnectInternal {
dataconnector: "postgres".to_string(),
connector_component: params.component.clone(),
source: Box::new(e),
}
.into()),
},
}
})
}
fn supports_unsupported_type_action(&self) -> bool {
true
}
fn prefix(&self) -> &'static str {
"pg"
}
fn parameters(&self) -> &'static [ParameterSpec] {
PARAMETERS
}
}
async fn postgres_comment_metadata(
pool: &Arc<PostgresConnectionPool>,
table_path: &str,
) -> std::result::Result<
(HashMap<String, String>, data_components::FieldMetadata),
Box<dyn std::error::Error + Send + Sync>,
> {
let conn = pool.connect_direct().await?;
let rows = conn
.conn
.query(
"SELECT \
obj_description(c.oid, 'pg_class') AS table_comment, \
a.attname AS column_name, \
col_description(c.oid, a.attnum) AS column_comment, \
format_type(a.atttypid, a.atttypmod) AS column_source_type \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_attribute a \
ON a.attrelid = c.oid \
AND a.attnum > 0 \
AND NOT a.attisdropped \
WHERE c.oid = to_regclass($1) \
ORDER BY a.attnum",
&[&table_path],
)
.await?;
let rows = rows
.iter()
.map(|row| (row.get(0), row.get(1), row.get(2), row.get(3)));
Ok(data_components::postgres::provider::postgres_metadata_from_rows(rows))
}
async fn enrich_with_postgres_comments(
pool: &Arc<PostgresConnectionPool>,
dataset: &Dataset,
provider: Arc<dyn TableProvider>,
) -> Arc<dyn TableProvider> {
match postgres_comment_metadata(pool, dataset.path()).await {
Ok((table_metadata, field_metadata)) => {
if table_metadata.is_empty() && field_metadata.is_empty() {
provider
} else {
data_components::metadata_enriched_table_provider(
provider,
table_metadata,
field_metadata,
)
}
}
Err(error) => {
tracing::warn!(
dataset = %dataset.name,
source = %dataset.path(),
error = %error,
"Failed to query PostgreSQL comments; registering without comment metadata"
);
provider
}
}
}
#[async_trait]
impl DataConnector for Postgres {
fn as_any(&self) -> &dyn Any {
self
}
async fn read_write_provider(
&self,
dataset: &Dataset,
) -> Option<DataConnectorResult<Arc<dyn TableProvider>>> {
match self
.factory
.read_write_table_provider(dataset.path().into())
.await
{
Ok(provider) => Some(Ok(enrich_with_postgres_comments(
&self.pool, dataset, provider,
)
.await)),
Err(e) => {
if let Some(err_source) = e.source() {
match err_source.downcast_ref::<dbconnection::Error>() {
Some(dbconnection::Error::UndefinedTable {
table_name,
source: _,
}) => {
return Some(Err(DataConnectorError::InvalidTableName {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
table_name: table_name.clone(),
}));
}
Some(dbconnection::Error::UnsupportedDataType {
data_type,
field_name,
}) => {
return Some(Err(DataConnectorError::UnsupportedDataType {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
data_type: data_type.clone(),
field_name: field_name.clone(),
}));
}
_ => {}
}
}
Some(Err(DataConnectorError::UnableToGetReadWriteProvider {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
source: e,
}))
}
}
}
async fn read_provider(
&self,
dataset: &Dataset,
) -> DataConnectorResult<Arc<dyn TableProvider>> {
match self.factory.table_provider(dataset.path().into()).await {
Ok(provider) => Ok(enrich_with_postgres_comments(&self.pool, dataset, provider).await),
Err(e) => {
if let Some(err_source) = e.source() {
match err_source.downcast_ref::<dbconnection::Error>() {
Some(dbconnection::Error::UndefinedTable {
table_name,
source: _,
}) => {
return Err(DataConnectorError::InvalidTableName {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
table_name: table_name.clone(),
});
}
Some(dbconnection::Error::UnsupportedDataType {
data_type,
field_name,
}) => {
return Err(DataConnectorError::UnsupportedDataType {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
data_type: data_type.clone(),
field_name: field_name.clone(),
});
}
_ => {}
}
}
Err(DataConnectorError::UnableToGetReadProvider {
dataconnector: "postgres".to_string(),
connector_component: ConnectorComponent::from(dataset),
source: e,
})
}
}
}
fn supports_changes_stream(&self) -> bool {
true
}
fn changes_stream(
&self,
federated_table: Arc<runtime::federated_table::FederatedTable>,
dataset: &Dataset,
_accelerated_table_provider: Arc<dyn TableProvider>,
_accelerator_write_mutex: Arc<tokio::sync::Mutex<()>>,
_cpu_runtime: Option<tokio::runtime::Handle>,
) -> Option<data_components::cdc::ChangesStream> {
Some(replication::build_changes_stream(
&self.params,
dataset,
federated_table,
Arc::clone(&self.replication_metrics),
))
}
fn metrics_provider(&self) -> Option<Arc<dyn MetricsProvider>> {
Some(Arc::new(replication::PostgresMetricsProvider::new(
data_components::postgres_replication::ReplicationMetrics::new(Arc::clone(
&self.replication_metrics,
)),
)))
}
}
/// The name used to identify this connector in configuration.
pub const CONNECTOR_NAME: &str = "postgres";
/// Returns a new instance of the `PostgreSQL` connector factory.
#[must_use]
pub fn factory() -> Arc<dyn DataConnectorFactory> {
PostgresFactory::new_arc()
}