DROP TABLE IF EXISTS foo;

CREATE TABLE foo (
    foo_id SERIAL PRIMARY KEY,
    foo_text TEXT NOT NULL
);

CREATE OR REPLACE FUNCTION foo_insert(
    in_foo_text TEXT
)
RETURNS INTEGER
LANGUAGE plperlu
AS $$
use strict;

elog NOTICE, "in_foo_text is $_[0]";

my $sql = <<'SQL';
INSERT INTO foo (
    foo_text
)
VALUES (
    $1
)
RETURNING foo_id
SQL

elog NOTICE, $sql;

my $sth = spi_prepare($sql, 'TEXT');

my $foo_id = spi_exec_prepared(
    $sth,
    $_[0],
)->{rows}->[0]->{foo_id};

spi_freeplan($sth);

return $foo_id;

$$;

SELECT foo_insert('blargh'::TEXT);

