plrust icon indicating copy to clipboard operation
plrust copied to clipboard

seg faults when working with composite types

Open darrenmothersele opened this issue 8 months ago • 3 comments

Hi,

I am getting seg faults from plrust code when working with composite types. I've seen this when working locally (in Docker), using cloudnative-pg, and in RDS on AWS (so I'm pretty sure it's not an environment issue).

I have a few cases where this occurs and I have not been able to track down what is causing it. I think it might be related to PgHeapTuple<AllocatedByRust>. I have seen it when calling the functions directly, but it usually occurs when calling a plrust function from a plpgsql function. Here's a minimal example that triggers it...

DROP TYPE IF EXISTS testevent CASCADE;
create type testevent AS
(
    entity_id text
);

DROP FUNCTION IF EXISTS test_fault_inner;
CREATE OR REPLACE FUNCTION test_fault_inner(entity_id text)
    RETURNS testevent
    LANGUAGE plrust AS
$_$
    let entity_id = entity_id.unwrap();

    let mut e = PgHeapTuple::new_composite_type("testevent")?;
    Spi::connect(|mut client| {
        e.set_by_name("entity_id", entity_id)?;
        Ok(Some(e))
    })
$_$;


DROP FUNCTION IF EXISTS test_fault;
CREATE OR REPLACE FUNCTION test_fault(id text)
    RETURNS void
    LANGUAGE plpgsql AS
$_$
DECLARE
    temp testevent;
BEGIN
    temp = test_fault_inner(id);
    RAISE LOG '%', temp.entity_id;
end
$_$;

SELECT test_fault(gen_random_uuid()::text);
SELECT test_fault(gen_random_uuid()::text);

Hopefully I'm holding it wrong.

darrenmothersele avatar May 24 '24 11:05 darrenmothersele