Skip to content

Commit

Permalink
Fix: GUC Database Utilization for Background Transformer Function (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
analyzer1 authored Sep 21, 2024
1 parent f70784e commit 9a39972
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion extension/src/controller/bgw_transformer_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ const MAX_TRANSFORMER_RETRIES: u8 = 3; // TODO: Set in GUC
#[no_mangle]
pub extern "C" fn background_worker_transformer_client(_arg: pg_sys::Datum) {

let database_name_string = guc::get_guc(guc::PgAutoDWGuc::DatabaseName);
let database_name_o: Option<&str> = database_name_string.as_deref();

BackgroundWorker::attach_signal_handlers(SignalWakeFlags::SIGHUP | SignalWakeFlags::SIGTERM);
BackgroundWorker::connect_worker_to_spi(Some("pg_auto_dw"), None);
BackgroundWorker::connect_worker_to_spi(database_name_o, None);

// Initialize Tokio runtime
let runtime = Runtime::new().expect("Failed to create Tokio runtime");

while BackgroundWorker::wait_latch(Some(Duration::from_secs(10))) {

extension_log("BGWorker: Transformer Client", "INFO", "Beginning Transformer Background Process.");

// Load Prompts into Results
let result: Result<Vec<source_objects::SourceTablePrompt>, pgrx::spi::Error> = BackgroundWorker::transaction(|| {
Expand Down Expand Up @@ -289,6 +294,20 @@ pub extern "C" fn background_worker_transformer_client(_arg: pg_sys::Datum) {
}
}

fn extension_log(process: &str, level: &str, message: &str) {

let insert_statement = format!(r#"
INSERT INTO auto_dw.log (process, level, message)
VALUES ('{}', '{}', '{}');
"#, process, level, message);

BackgroundWorker::transaction(|| {
Spi::connect(|mut client| {
_ = client.update(insert_statement.as_str(), None, None);
})
});
}

fn extract_column_numbers(json_str: &str) -> Vec<u32> {
// Define a regex to capture the column numbers
let re = Regex::new(r"Column No: (\d+)").expect("Invalid regex");
Expand Down
10 changes: 10 additions & 0 deletions extension/src/utility/sql/info_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ CREATE TABLE dv_repo (
insert_time TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'UTC'),
schema JSON
);

DROP TABLE IF EXISTS log;

CREATE TABLE log (
pk_log BIGSERIAL PRIMARY KEY,
log_ts TIMESTAMP WITHOUT TIME ZONE DEFAULT (NOW() AT TIME ZONE 'UTC'),
process VARCHAR(50),
level VARCHAR(50),
message TEXT
);

0 comments on commit 9a39972

Please sign in to comment.