Truncate text encoding safe
Use this function:

CREATE OR REPLACE FUNCTION get_prefix (
   string text,
   max_bytes bigint
) RETURNS text
   LANGUAGE sql STRICT AS
$$SELECT p
FROM (SELECT p.p, octet_length(p.p) AS len
      FROM generate_series(0, length($1)) AS len
         CROSS JOIN LATERAL substr($1, 1, len.len) AS p) AS q
WHERE len &lt;= $2
ORDER BY len DESC
LIMIT 1$$;


It works like this:

SELECT get_prefix('abc€', 4);
 get_prefix 
------------
 abc
(1 row)