With the help of Igor Romanchenko's suggestions I came up with this solution that does what I want:
CREATE OR REPLACE FUNCTION trigger_function()
RETURNS trigger AS
$BODY$
DECLARE
attribute_type_id numeric(12,0);
attribute_type_max_length numeric(9,0);
is_longer_than_max_length BOOLEAN;
BEGIN
IF (NEW.default_value IS NOT NULL) THEN
SELECT attribute_type.id, attribute_type.max_length,
length(NEW.default_value) > attribute_type.max_length
INTO attribute_type_id, attribute_type_max_length,
is_longer_than_max_length FROM attribute_group_attribute
JOIN attribute ON NEW.attribute_id = attribute.id JOIN attribute_type
ON attribute.attribute_type_id = attribute_type.id;
IF (is_longer_than_max_length) THEN
RAISE EXCEPTION 'Attribute type with id % has max length of %, while
attribute default value % has a length of %.', attribute_type_id,
attribute_type_max_length, NEW.default_value,
length(NEW.default_value) USING ERRCODE='20808';
END IF;
END IF;
RETURN NEW;
END
$BODY$
LANGUAGE 'plpgsql' SECURITY INVOKER
;
CREATE TRIGGER trigger_name BEFORE INSERT OR UPDATE ON attribute_group_attribute
FOR EACH ROW EXECUTE PROCEDURE trigger_function()
;