CREATE OR REPLACE FUNCTION fn_alter_entity(data JSONB, t TEXT )
RETURNS VOID
SECURITY INVOKER
AS $$
BEGIN
EXECUTE FORMAT(
'INSERT INTO %I SELECT * FROM jsonb_populate_record( null::%I, $1);',
t,
t
)
USING data;
END;
$$
LANGUAGE plpgsql
VOLATILE;
CREATE TABLE foo ( a int );
CREATE TABLE bar ( a int, b text );
SELECT fn_alter_entity( '{"a": 5}', 'foo' );
SELECT fn_alter_entity( '{"a": 42}', 'foo' );
SELECT fn_alter_entity( '{"a": 5, "b": "foo"}', 'bar' );
SELECT fn_alter_entity( '{"a": 42, "b": "baz"}', 'bar' );
test=# TABLE foo;
a
----+
5
42
(2 rows)
test=# TABLE bar;
a | b
----+-----
5 | foo
42 | baz
(2 rows)