I am pretty new to PostgreSQL and also to Node-RED.
It seems like you can create tables with dynamic names in PostgreSQL. With something like this or this:
-- gives error: syntax error at »LANGUAGE«
CREATE OR REPLACE FUNCTION table_per_meter(meter_secondary text)
RETURNS INTEGER AS
LANGUAGE plpgsql
$body$
BEGIN
RETURN QUERY EXECUTE
format('CREATE TABLE IF NOT EXISTS meter_value_%I(id SERIAL PRIMARY KEY, v NUMERIC(21, 2) NOT NULL, d TIMESTAMP WITH TIME ZONE NOT NULL);',
meter_secondary)
END;
$body$
I cannot spot the error here.
Is using RETURN QUERY EXECUTE wrong or overhead?
These examples look much more simple:
EXECUTE format('SELECT count(*) FROM %I '
'WHERE inserted_by = $1 AND inserted <= $2', tabname)
INTO c
USING checked_user, checked_date;
But my problems seems to be related to the way of function definition.
LANGUAGEis in the wrong place.END; $body$but the code's missing an obligatory one after it to terminate the statement:END; $body$;orEND $body$;. In practice, most clients handle unterminated statements fine.