Skip to content
This repository was archived by the owner on Mar 19, 2025. It is now read-only.

Commit 82229a1

Browse files
fix: Load httpfs & Store DuckDB extensions in PGDATA (#200)
* try to load httpfs * Make read-only * Fix * try this * Set extension path to PGDATA dir * rm chmod and codecov * also rm above * cargo fmt --------- Co-authored-by: Ming Ying <ming.ying.nyc@gmail.com>
1 parent 7a67e82 commit 82229a1

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

.github/workflows/test-pg_analytics.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ jobs:
149149
LLVM_PROFILE_FILE: target/coverage/pg_analytics-%p-%m.profraw
150150
RUST_BACKTRACE: full
151151
run: |
152-
echo ""
153-
echo "Enabling code coverage..."
154-
echo -e "\n# Enable code coverage on Linux only, for CI builds\n[target.'cfg(target_os=\"linux\")']\nrustflags = [\"-Cinstrument-coverage\"]" >> .cargo/config.toml
155-
mkdir -p target/coverage target/coverage-report
156-
157152
echo ""
158153
echo "Running Rust tests..."
159154
export DATABASE_URL=postgresql://localhost:288${{ matrix.pg_version }}/postgres
@@ -267,11 +262,6 @@ jobs:
267262
LLVM_PROFILE_FILE: target/coverage/pg_analytics-%p-%m.profraw
268263
RUST_BACKTRACE: full
269264
run: |
270-
echo ""
271-
echo "Enabling code coverage..."
272-
echo -e "\n# Enable code coverage on Linux only, for CI builds\n[target.'cfg(target_os=\"linux\")']\nrustflags = [\"-Cinstrument-coverage\"]" >> .cargo/config.toml
273-
mkdir -p target/coverage target/coverage-report
274-
275265
echo ""
276266
echo "Running Rust tests..."
277267
export DATABASE_URL=postgresql://localhost:288${{ matrix.pg_version }}/postgres

src/duckdb/connection.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ pub fn set_search_path(search_path: Vec<String>) -> Result<()> {
255255
Ok(())
256256
}
257257

258+
pub fn set_duckdb_extension_directory(extension_directory_path: &str) -> Result<()> {
259+
// Set duckdb extension directory
260+
execute(
261+
format!("SET extension_directory = '{extension_directory_path}'").as_str(),
262+
[],
263+
)?;
264+
265+
Ok(())
266+
}
267+
258268
pub fn execute_explain(query: &str) -> Result<String> {
259269
let conn = unsafe { &*get_global_connection().get() };
260270
let mut stmt = conn.prepare(query)?;
@@ -272,3 +282,11 @@ pub fn execute_explain(query: &str) -> Result<String> {
272282

273283
Ok(rows.join(""))
274284
}
285+
286+
pub fn install_httpfs() -> Result<()> {
287+
if !check_extension_loaded("httpfs")? {
288+
execute("INSTALL httpfs", [])?;
289+
execute("LOAD httpfs", [])?;
290+
}
291+
Ok(())
292+
}

src/fdw/base.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ use anyhow::{anyhow, bail, Result};
1919
use duckdb::arrow::array::RecordBatch;
2020
use pgrx::*;
2121
use std::collections::HashMap;
22+
use std::ffi::CStr;
2223
use strum::IntoEnumIterator;
2324
use supabase_wrappers::prelude::*;
2425
use thiserror::Error;
2526

2627
use super::handler::FdwHandler;
2728
use crate::duckdb::connection;
29+
use crate::fdw::base::connection::set_duckdb_extension_directory;
2830
use crate::schema::cell::*;
31+
2932
#[cfg(debug_assertions)]
3033
use crate::DEBUG_GUCS;
3134

@@ -223,6 +226,18 @@ pub fn register_duckdb_view(
223226
connection::create_secret(DEFAULT_SECRET, user_mapping_options)?;
224227
}
225228

229+
// In CloudNativePG, the filesystem is read-only. To work around this, we force
230+
// DuckDB to install its extensions in the PGDATA directory, which is writable.
231+
let data_dir = unsafe {
232+
CStr::from_ptr(pgrx::pg_sys::DataDir)
233+
.to_str()
234+
.map_err(|e| anyhow::anyhow!("Failed to convert DataDir to &str: {}", e))?
235+
};
236+
let _ = set_duckdb_extension_directory(data_dir);
237+
238+
// duckdb-rs stopped bundling in httpfs, so we need to load it ourselves
239+
connection::install_httpfs()?;
240+
226241
if !connection::view_exists(table_name, schema_name)? {
227242
// Initialize DuckDB view
228243
connection::execute(

0 commit comments

Comments
 (0)