I am trying to get plv8 3.x working in Postgres v14. I am trying to preserve the type information when calling a plv8 function, but everything ends up being just "object". This is affecting my ability to do equality comparisons between two instances of the object.
do $$
begin
CREATE TYPE t2 AS (
arrival_date date,
departure_date date
);
CREATE OR REPLACE FUNCTION foo1(t t2)
RETURNS void
AS
$t1$
begin
raise notice 'foo1';
raise notice '%', pg_typeof(t);
raise notice '%', pg_typeof(t.arrival_date);
raise notice '%', (t.arrival_date = t.departure_date);
end
$t1$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION foo2(t t2)
RETURNS void
AS
$t2$
plv8.elog(NOTICE, 'foo2');
plv8.elog(NOTICE, typeof t);
plv8.elog(NOTICE, typeof t.arrival_date);
plv8.elog(NOTICE, t.arrival_date == t.departure_date);
$t2$ LANGUAGE plv8;
CREATE OR REPLACE FUNCTION foo3(t anyelement)
RETURNS void
AS
$t2$
plv8.elog(NOTICE, 'foo3');
plv8.elog(NOTICE, typeof t);
plv8.elog(NOTICE, typeof t.arrival_date);
plv8.elog(NOTICE, t.arrival_date == t.departure_date);
$t2$ LANGUAGE plv8;
-------------------------
raise notice 'starting';
perform foo1(row('2023-01-01'::date, '2023-01-01'::date)::t2);
perform foo2(row('2023-01-01'::date, '2023-01-01'::date)::t2);
perform foo3(row('2023-01-01'::date, '2023-01-01'::date)::t2);
raise notice 'finishing';
end
$$ LANGUAGE plpgsql;
The first, foo1, is a simple plsql function as a baseline so I can see that type is preserved under normal circumstances. The second and third are a plv8 where the IN type is strongly typed and polymorphic (respectively). Neither are preserving the types and as you can see, my equality is not working. (below is my output)
NOTICE: starting
NOTICE: foo1
NOTICE: t2
NOTICE: date
NOTICE: t
NOTICE: foo2
NOTICE: object
NOTICE: object
NOTICE: false
NOTICE: foo3
NOTICE: object
NOTICE: object
NOTICE: false
NOTICE: finishing
Is there any way to preserve the type information in a plv8 function call?
I know I could turn everything into a string and compare, but that's extra processing that I would rather not do for every field I compare.