Skip to content

Commit

Permalink
Fix github issue #49 (#187)
Browse files Browse the repository at this point in the history
* Fix github issue #49

pg_fdw was not working with PG11 because the layout
of TupleDesc has changed.  Return an array of
FormData_pg_attribute instead of an array of
points to them.

* replace tab with space to make clippy happy.

* formatting fixes for cargo fmt

* Fix formatting for cargo fmt
  • Loading branch information
ssinger authored Aug 28, 2020
1 parent 578e0a6 commit f601332
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
8 changes: 8 additions & 0 deletions 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
Expand Up @@ -8,7 +8,7 @@ members = [
"examples/adding",
# Examples disabled because FDW support broken with PostgreSQL 11+.
# See https://github.com/bluejekyll/pg-extend-rs/issues/49
# "examples/fdw",
"examples/fdw",
# "examples/fdw-rw",
"examples/logging",
"examples/memory_context",
Expand Down
2 changes: 1 addition & 1 deletion examples/fdw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extern crate pg_extend;
extern crate pg_extern_attr;

use pg_extend::pg_fdw::{ForeignData, ForeignRow, OptionMap, ForeignTableMetadata};
use pg_extend::pg_fdw::{ForeignData, ForeignRow, ForeignTableMetadata, OptionMap};
use pg_extend::{pg_datum, pg_magic, pg_type};
use pg_extern_attr::pg_foreignwrapper;

Expand Down
1 change: 0 additions & 1 deletion integration-tests/tests/fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use integration_tests::*;
// FDW tests disabled because it's broken with PostgreSQL 11+.
// See See https://github.com/bluejekyll/pg-extend-rs/issues/49
#[test]
#[ignore] // this test is currently broken
fn test_fdw() {
test_in_db("fdw", |mut conn| {
conn.batch_execute(
Expand Down
28 changes: 16 additions & 12 deletions pg-extend/src/pg_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,30 @@ impl<T: ForeignData> ForeignWrapper<T> {
let mut t = HashMap::new();

for i in 0..(attrs.len().min(data.len())) {
let name = Self::name_to_string(unsafe { (*attrs[i]).attname });
let name = Self::name_to_string((attrs[i]).attname);
let data = unsafe { pg_datum::PgDatum::from_raw(memory_context, data[i], isnull[i]) };
t.insert(name, data);
}

t
}

unsafe fn tupdesc_attrs(tupledesc: &pg_sys::tupleDesc) -> &[pg_sys::Form_pg_attribute] {
unsafe fn tupdesc_attrs(tupledesc: &pg_sys::tupleDesc) -> &[pg_sys::FormData_pg_attribute] {
#[cfg(feature = "postgres-11")]
#[allow(clippy::cast_ptr_alignment)]
let attrs = (*tupledesc).attrs.as_ptr() as *const _;
{
let attrs = (*tupledesc).attrs.as_ptr();
std::slice::from_raw_parts(attrs, (*tupledesc).natts as usize)
}
#[cfg(not(feature = "postgres-11"))]
let attrs = (*tupledesc).attrs;

std::slice::from_raw_parts(attrs, (*tupledesc).natts as usize)
{
let attrs = (*tupledesc).attrs;
std::slice::from_raw_parts(*attrs, (*tupledesc).natts as usize)
}
}

/// Retrieve next row from the result set, or clear tuple slot to indicate
/// EOF.
/// EOF.
/// Fetch one row from the foreign
/// (the node's ScanTupleSlot should be used for this purpose).
/// Return NULL if no more rows are available.
Expand All @@ -351,7 +355,7 @@ impl<T: ForeignData> ForeignWrapper<T> {
let mut isnull = vec![pgbool!(true); attrs.len()];
for (i, pattr) in attrs.iter().enumerate() {
// TODO: There must be a better way to do this?
let result = Self::get_field(&memory_context, &(**pattr), &(*row));
let result = Self::get_field(&memory_context, &(*pattr), &(*row));
match result {
Err(err) => {
warn!("{}", err);
Expand Down Expand Up @@ -408,11 +412,11 @@ impl<T: ForeignData> ForeignWrapper<T> {

if let Some(keys) = T::index_columns(&table_metadata) {
// Build a map of column names to attributes and column index
let attrs: HashMap<String, (&pg_sys::Form_pg_attribute, usize)> =
let attrs: HashMap<String, (&pg_sys::FormData_pg_attribute, usize)> =
Self::tupdesc_attrs(&*(*target_relation).rd_att)
.iter()
.enumerate()
.map(|(idx, rel)| (Self::name_to_string((**rel).attname), (rel, idx)))
.map(|(idx, rel)| (Self::name_to_string((rel).attname), (rel, idx)))
.collect();

for key in keys {
Expand All @@ -428,8 +432,8 @@ impl<T: ForeignData> ForeignWrapper<T> {
let var = pg_sys::makeVar(
(*parsetree).resultRelation as u32,
*idx as i16 + 1, // points to the position in the tuple, 1-indexed
(*attr).atttypid,
(*attr).atttypmod,
(attr).atttypid,
(attr).atttypmod,
0 as pg_sys::Oid, // InvalidOid
0,
);
Expand Down

0 comments on commit f601332

Please sign in to comment.