pub struct PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: WhoAllocated,
{ /* private fields */ }
Expand description

A PgHeapTuple is a lightweight wrapper around Postgres’ pg_sys::HeapTuple object and a PgTupleDesc.

In order to access the attributes within a pg_sys::HeapTuple, the PgTupleDesc is required to describe its structure.

PgHeapTuples can be created from existing (Postgres-provided) pg_sys::HeapTuple pointers, from pg_sys::TriggerData pointers, from a composite datum, or created from scratch using raw Datums.

A PgHeapTuple can either be considered to be allocated by Postgres or by the Rust runtime. If allocated by Postgres, it is not mutable until PgHeapTuple::into_owned is called.

PgHeapTuples also describe composite types as defined by pgrx::composite_type!().

Implementations§

source§

impl<'a> PgHeapTuple<'a, AllocatedByPostgres>

source

pub unsafe fn from_heap_tuple( tupdesc: PgTupleDesc<'a>, heap_tuple: *mut HeapTupleData ) -> PgHeapTuple<'a, AllocatedByPostgres>

Creates a new PgHeapTuple from a PgTupleDesc and a pg_sys::HeapTuple pointer. The returned PgHeapTuple will be considered by have been allocated by Postgres and is not mutable until PgHeapTuple::into_owned is called.

§Safety

This function is unsafe as we cannot guarantee that the pg_sys::HeapTuple pointer is valid, nor can we guaratee that the provided PgTupleDesc properly describes the structure of the heap tuple.

source

pub unsafe fn from_trigger_data( trigger_data: &'a TriggerData, which_tuple: TriggerTuple ) -> Option<PgHeapTuple<'a, AllocatedByPostgres>>

Creates a new PgHeapTuple identified by the which_tuple trigger tuple. The returned PgHeapTuple will be considered by have been allocated by Postgres and is not mutable until PgHeapTuple::into_owned is called.

pgrx also invents the concept of a “current” ([TriggerTuple::Current]) tuple, which is either the new row being inserted or the row being updated (not the new version) or deleted.

Asking for a TriggerTuple that isn’t compatible with how the trigger was fired causes this function to return None. Specifically this means None is always returned for statement-level triggers.

§Safety

This function is unsafe as we cannot guarantee that any pointers in the trigger_data argument are valid or that it’s being used in the context of a firing trigger, which necessitates Postgres internal state be correct for executing a trigger.

source

pub fn into_owned(self) -> PgHeapTuple<'a, AllocatedByRust>

Consumes a [PgHeapTuple] considered to be allocated by Postgres and transforms it into one that is considered allocated by Rust. This is accomplished by copying the underlying pg_sys::HeapTupleData.

source§

impl<'a> PgHeapTuple<'a, AllocatedByRust>

source

pub fn new_composite_type( type_name: &str ) -> Result<PgHeapTuple<'a, AllocatedByRust>, PgHeapTupleError>

Create a new heap tuple in the shape of a defined composite type

use pgrx::prelude::*;

Spi::run("CREATE TYPE dog AS (name text, age int);");
let mut heap_tuple = PgHeapTuple::new_composite_type("dog").unwrap();

assert_eq!(heap_tuple.get_by_name::<String>("name").unwrap(), None);
assert_eq!(heap_tuple.get_by_name::<i32>("age").unwrap(), None);

heap_tuple
    .set_by_name("name", "Brandy".to_string())
    .unwrap();
heap_tuple.set_by_name("age", 42).unwrap();

assert_eq!(
    heap_tuple.get_by_name("name").unwrap(),
    Some("Brandy".to_string())
);
assert_eq!(heap_tuple.get_by_name("age").unwrap(), Some(42i32));
source

pub fn new_composite_type_by_oid( typoid: Oid ) -> Result<PgHeapTuple<'a, AllocatedByRust>, PgHeapTupleError>

source

pub unsafe fn from_datums<I>( tupdesc: PgTupleDesc<'a>, datums: I ) -> Result<PgHeapTuple<'a, AllocatedByRust>, PgHeapTupleError>
where I: IntoIterator<Item = Option<Datum>>,

Create a new PgHeapTuple from a PgTupleDesc from an iterator of Datums.

§Errors
§Safety

This function is unsafe as we cannot guarantee the provided pg_sys::Datums are valid as the specified PgTupleDesc might expect

source

pub unsafe fn from_composite_datum( composite: Datum ) -> PgHeapTuple<'a, AllocatedByRust>

Creates a new PgHeapTuple from an opaque Datum that should be a “composite” type.

The Datum should be a pointer to a pg_sys::HeapTupleHeader. Typically, this will be used in situations when working with SQL ROW(...) constructors, or a composite SQL type such as

CREATE TYPE my_composite AS (name text, age i32);
§Safety

This function is unsafe as we cannot guarantee that the provided Datum is a valid pg_sys::HeapTupleHeader pointer.

source

pub fn set_by_name<T>( &mut self, attname: &str, value: T ) -> Result<(), TryFromDatumError>
where T: IntoDatum,

Given the name for an attribute in this PgHeapTuple, change its value.

Attribute names are case sensitive.

§Errors
source

pub fn set_by_index<T>( &mut self, attno: NonZero<usize>, value: T ) -> Result<(), TryFromDatumError>
where T: IntoDatum,

Given the index for an attribute in this PgHeapTuple, change its value.

Attribute numbers start at 1, not 0.

§Errors
source§

impl<'a, AllocatedBy> PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: WhoAllocated,

source

pub fn into_composite_datum(self) -> Option<Datum>

Consume this PgHeapTuple and return a composite Datum representation, containing the tuple data and the corresponding tuple descriptor information.

source

pub fn into_trigger_datum(self) -> Option<Datum>

Consume this PgHeapTuple and return a Datum representation appropriate for returning from a trigger function

source

pub fn into_pg(self) -> *mut HeapTupleData

Consumes this PgHeapTuple, returning a pointer to a pg_sys::HeapTupleData that can be passed to a Postgres FFI function. It’ll be freed whenever Postgres frees the MemoryContext in which it was allocated.

source

pub fn len(&self) -> usize

Returns the number of attributes in this PgHeapTuple.

source

pub fn attributes( &'a self ) -> impl Iterator<Item = (NonZero<usize>, &'a FormData_pg_attribute)>

Returns an iterator over the attributes in this PgHeapTuple.

The return value is (attribute_number: NonZeroUsize, attribute_info: &pg_sys::FormData_pg_attribute).

source

pub fn get_attribute_by_index( &'a self, index: NonZero<usize> ) -> Option<&'a FormData_pg_attribute>

Get the attribute information for the specified attribute number.

Returns None if the attribute number is out of bounds.

source

pub fn get_attribute_by_name( &'a self, name: &str ) -> Option<(NonZero<usize>, &'a FormData_pg_attribute)>

Get the attribute information for the specified attribute, by name.

Returns None if the attribute name is not found.

source

pub fn get_by_name<T>( &self, attname: &str ) -> Result<Option<T>, TryFromDatumError>
where T: FromDatum + IntoDatum + 'static,

Retrieve the value of the specified attribute, by name.

Attribute names are case-insensitive.

§Errors
source

pub fn get_by_index<T>( &self, attno: NonZero<usize> ) -> Result<Option<T>, TryFromDatumError>
where T: FromDatum + IntoDatum + 'static,

Retrieve the value of the specified attribute, by index.

Attribute numbers start at 1, not 0.

§Errors

Trait Implementations§

source§

impl<'a> FromDatum for PgHeapTuple<'a, AllocatedByRust>

source§

unsafe fn from_polymorphic_datum( composite: Datum, is_null: bool, _oid: Oid ) -> Option<PgHeapTuple<'a, AllocatedByRust>>

Like from_datum for instantiating polymorphic types which require preserving the dynamic type metadata. Read more
source§

unsafe fn from_datum_in_memory_context( memory_context: PgMemoryContexts, composite: Datum, is_null: bool, _oid: Oid ) -> Option<PgHeapTuple<'a, AllocatedByRust>>

Default implementation switched to the specified memory context and then simply calls FromDatum::from_datum(...) from within that context. Read more
source§

const GET_TYPOID: bool = false

Should a type OID be fetched when calling from_datum?
source§

unsafe fn from_datum(datum: Datum, is_null: bool) -> Option<Self>
where Self: Sized,

Safety Read more
source§

unsafe fn try_from_datum( datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>
where Self: Sized + IntoDatum,

try_from_datum is a convenience wrapper around FromDatum::from_datum that returns a a Result around an Option, as a Datum can be null. It’s intended to be used in situations where the caller needs to know whether the type conversion succeeded or failed. Read more
source§

unsafe fn try_from_datum_in_memory_context( memory_context: PgMemoryContexts, datum: Datum, is_null: bool, type_oid: Oid ) -> Result<Option<Self>, TryFromDatumError>
where Self: Sized + IntoDatum,

A version of try_from_datum that switches to the given context to convert from Datum
source§

impl<'a, AllocatedBy> IntoDatum for PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: WhoAllocated,

source§

fn into_datum(self) -> Option<Datum>

source§

fn type_oid() -> Oid

source§

fn composite_type_oid(&self) -> Option<Oid>

source§

fn is_compatible_with(other: Oid) -> bool

Is a Datum of this type compatible with another Postgres type? Read more
source§

fn array_type_oid() -> Oid

source§

impl SqlTranslatable for PgHeapTuple<'static, AllocatedByPostgres>

source§

impl SqlTranslatable for PgHeapTuple<'static, AllocatedByRust>

Auto Trait Implementations§

§

impl<'a, AllocatedBy> RefUnwindSafe for PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: RefUnwindSafe,

§

impl<'a, AllocatedBy> !Send for PgHeapTuple<'a, AllocatedBy>

§

impl<'a, AllocatedBy> !Sync for PgHeapTuple<'a, AllocatedBy>

§

impl<'a, AllocatedBy> Unpin for PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: Unpin,

§

impl<'a, AllocatedBy> UnwindSafe for PgHeapTuple<'a, AllocatedBy>
where AllocatedBy: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> Conv for T

source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
source§

impl<T> FmtForward for T

source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pipe for T
where T: ?Sized,

source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
source§

impl<T> Tap for T

source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> TryConv for T

source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithTypeIds for T
where T: 'static + ?Sized,

source§

const ITEM_ID: Lazy<TypeId> = _

source§

const OPTION_ID: Lazy<Option<TypeId>> = _

source§

const VEC_ID: Lazy<Option<TypeId>> = _

source§

const VEC_OPTION_ID: Lazy<Option<TypeId>> = _

source§

const OPTION_VEC_ID: Lazy<Option<TypeId>> = _

source§

const OPTION_VEC_OPTION_ID: Lazy<Option<TypeId>> = _

source§

const ARRAY_ID: Lazy<Option<TypeId>> = _

source§

const OPTION_ARRAY_ID: Lazy<Option<TypeId>> = _

source§

const VARIADICARRAY_ID: Lazy<Option<TypeId>> = _

source§

const OPTION_VARIADICARRAY_ID: Lazy<Option<TypeId>> = _

source§

const VARLENA_ID: Lazy<Option<TypeId>> = _

source§

const OPTION_VARLENA_ID: Lazy<Option<TypeId>> = _

source§

fn register_with_refs(map: &mut HashSet<RustSqlMapping>, single_sql: String)
where Self: 'static,

source§

fn register_sized_with_refs( _map: &mut HashSet<RustSqlMapping>, _single_sql: String )
where Self: 'static,

source§

fn register_sized(_map: &mut HashSet<RustSqlMapping>, _single_sql: String)
where Self: 'static,

source§

fn register_varlena_with_refs( _map: &mut HashSet<RustSqlMapping>, _single_sql: String )
where Self: 'static,

source§

fn register_varlena(_map: &mut HashSet<RustSqlMapping>, _single_sql: String)
where Self: 'static,

source§

fn register_array_with_refs( _map: &mut HashSet<RustSqlMapping>, _single_sql: String )
where Self: 'static,

source§

fn register_array(_map: &mut HashSet<RustSqlMapping>, _single_sql: String)
where Self: 'static,

source§

fn register(set: &mut HashSet<RustSqlMapping>, single_sql: String)
where Self: 'static,