CREATE OR REPLACE FUNCTION auto_increment_version(id bigint)
RETURNS bigint
LANGUAGE plpgsql
AS $$
DECLARE
res BIGINT;
BEGIN
SELECT COUNT(*)
INTO res
FROM appsellschema."product-version"
WHERE template_id = id;
RETURN res+1;
END;
$$;
SELECT appsellschema.auto_increment_version(1);
It could refer to either a PL/pgSQL variable or a table column.
Find more questions by tags PostgreSQL