Skip to content

Commit

Permalink
basic setup to automate (#16)
Browse files Browse the repository at this point in the history
* basic setup to automate

* add indexing and partitioning function

* add basic setup steps as postgresql functions

* update function signature

* basic mkdocs setup

* release new version

* Revert "basic mkdocs setup"

This reverts commit 8106280.
  • Loading branch information
Jayko001 authored Apr 23, 2024
1 parent c48c9ff commit fb1e871
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prometheus_fdw"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

[lib]
Expand Down
19 changes: 19 additions & 0 deletions sql/prometheus_fdw--0.1.4--0.1.5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE FUNCTION basic_setup(base_url text)
RETURNS void
AS 'MODULE_PATHNAME', 'basic_setup'
LANGUAGE C STRICT;

CREATE FUNCTION create_tables()
RETURNS void
AS 'MODULE_PATHNAME', 'create_tables'
LANGUAGE C STRICT;

CREATE FUNCTION create_indexes()
RETURNS void
AS 'MODULE_PATHNAME', 'create_indexes'
LANGUAGE C STRICT;

CREATE FUNCTION create_partitions(retention_period text)
RETURNS void
AS 'MODULE_PATHNAME', 'create_partitions'
LANGUAGE C STRICT;
105 changes: 105 additions & 0 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use pgrx::prelude::*;
use std::error::Error;

#[pg_extern]
fn basic_setup(base_url: &str) -> Result<(), Box<dyn Error>> {
let queries = format!(
r#"
-- Enable the extensions
CREATE EXTENSION IF NOT EXISTS prometheus_fdw CASCADE;
CREATE EXTENSION IF NOT EXISTS pg_partman CASCADE;
CREATE EXTENSION IF NOT EXISTS pg_cron CASCADE;
-- Create the FDW
CREATE FOREIGN DATA WRAPPER prometheus_wrapper
HANDLER prometheus_fdw_handler
VALIDATOR prometheus_fdw_validator;
-- Configure connection to server
CREATE SERVER my_prometheus_server
FOREIGN DATA WRAPPER prometheus_wrapper
OPTIONS (
base_url '{}');
-- Create FDW table we can query to get metrics
CREATE FOREIGN TABLE metrics (
metric_name TEXT,
metric_labels JSONB,
metric_time BIGINT,
metric_value FLOAT8
)
SERVER my_prometheus_server
OPTIONS (
object 'metrics',
step '{}'
);
"#,
base_url, "10m"
);

Spi::run(&queries);

Ok(())
}

/// Creates the necessary tables for metric tracking.
#[pg_extern]
fn create_tables() -> Result<(), Box<dyn Error>> {
let queries = r#"
CREATE TABLE IF NOT EXISTS metric_labels (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
labels jsonb NOT NULL
);
CREATE TABLE IF NOT EXISTS metric_values (
label_id INTEGER REFERENCES metric_labels (id),
time TIMESTAMP NOT NULL,
value DOUBLE PRECISION NOT NULL
) PARTITION BY RANGE (time);
"#;

Spi::run(&queries);

Ok(())
}

/// Creates indexes to optimize query performance.
#[pg_extern]
fn create_indexes() -> Result<(), Box<dyn Error>> {
let queries = r#"
CREATE INDEX idx_metric_labels_name ON metric_labels (name);
CREATE INDEX idx_metric_labels_labels ON metric_labels USING GIN (labels);
CREATE INDEX idx_metric_values_time ON metric_values (time);
CREATE INDEX idx_metric_values_label_id ON metric_values (label_id);
"#;

Spi::run(queries);
Ok(())
}

/// Sets up partitioning for the metric_values table and configures retention policy.
#[pg_extern]
fn create_partitions(retention_period: &str) -> Result<(), Box<dyn Error>> {
let setup_partitioning = r#"
SELECT create_parent('public.metric_values', 'time', 'native', '1 day');
"#;

// Execute the partition setup query
Spi::run(setup_partitioning);

let setup_retention = format!(
r#"
UPDATE part_config
SET retention = '{}',
retention_keep_table = false,
retention_keep_index = false,
infinite_time_partitions = true
WHERE parent_table = 'public.metric_values';
"#,
retention_period
);

// Execute the retention setup query
Spi::run(&setup_retention);
Ok(())
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod init;

use pgrx::warning;
use pgrx::{pg_sys, prelude::*, JsonB};
use reqwest::{self, Client};
Expand Down Expand Up @@ -62,7 +64,7 @@ fn resp_to_rows(obj: &str, resp: &JsonValue, quals: &[Qual]) -> Vec<Row> {
}

#[wrappers_fdw(
version = "0.1.4",
version = "0.1.5",
author = "Jay Kothari",
website = "https://tembo.io"
)]
Expand Down

0 comments on commit fb1e871

Please sign in to comment.