Postgresql – how to get back the modified INOUT parameter in postgres

castfunctionspostgresqltrigger

I have two functions with a similar signature, lets say:

CREATE FUNCTION func1(INOUT new "My_Table") AS $$ ... $$ LANGUAGE plpgsql;
CREATE FUNCTION func2(INOUT new "My_Table") AS $$ ... $$ LANGUAGE plpgsql;

I call both of these functions from a BEFORE UPDATE TRIGGER where the parameter is the row being updated. I want to give the row to the function as an input but subsequently modify it inside the function (therefore the INOUT).

I call the first function as:

SELECT * FROM func1(NEW) INTO NEW;

Which works just fine.

However, I can't make it work with the second function because the type of NEW is no longer of "My_Table" but it is "record" and I am unable to cast it.

SELECT * FROM func1(NEW) INTO NEW;

or

SELECT * FROM func1(NEW::"My_Table") INTO NEW;

or

SELECT * FROM func1(CAST(NEW AS "My_Table")) INTO NEW;

gives

ERROR:  42846: cannot cast type record to "My_Table"

How can I do this please?

Best Answer

These are the two working solutions:

NEW := func1(NEW);

or first cast the record to text:

SELECT * FROM func1(NEW::text::"My_Table") INTO NEW;