Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory management for Datum to String conversion #1684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pgrx/src/datum/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,20 @@ impl FromDatum for String {
is_null: bool,
typoid: pg_sys::Oid,
) -> Option<String> {
FromDatum::from_polymorphic_datum(datum, is_null, typoid).map(|s: &str| s.to_owned())
let varlena = pg_sys::pg_detoast_datum_packed(datum.cast_mut_ptr());
let converted_varlena = convert_varlena_to_str_memoized(varlena);
let ret_string = converted_varlena.to_owned();

// If the datum is EXTERNAL or COMPRESSED, then detoasting creates a pfree-able chunk
// that needs to be freed. We can free it because `to_owned` above creates a Rust copy
// of the string.
if varlena::varatt_is_1b_e(datum.cast_mut_ptr())
|| varlena::varatt_is_4b_c(datum.cast_mut_ptr())
{
pg_sys::pfree(varlena.cast());
}

Some(ret_string)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pgrx/src/varlena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub unsafe fn varatt_is_4b_u(ptr: *const pg_sys::varlena) -> bool {
/// ((((varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
/// ```
#[inline]
pub unsafe fn varatt_is_b8_c(ptr: *const pg_sys::varlena) -> bool {
pub unsafe fn varatt_is_4b_c(ptr: *const pg_sys::varlena) -> bool {
sardination marked this conversation as resolved.
Show resolved Hide resolved
let va1b = ptr as *const pg_sys::varattrib_1b;
(*va1b).va_header & 0x03 == 0x02
}
Expand Down
Loading