Here is a function that does what pg_relation_size does, more or less, without taking any locks. Because of this, it is always fast, but it may give an incorrect result if the table is being heavily altered at the same time:
CREATE OR REPLACE FUNCTION pg_relation_size_nolock(tablename regclass) RETURNS BIGINT LANGUAGE plpgsql AS $$ DECLARE classoutput RECORD; tsid INTEGER; rid INTEGER; dbid INTEGER; filepath TEXT; filename TEXT; datadir TEXT; i INTEGER := 0; tablesize BIGINT; BEGIN -- -- Get data directory -- EXECUTE 'SHOW data_directory' INTO datadir; -- -- Get relfilenode and reltablespace -- SELECT reltablespace as tsid ,relfilenode as rid INTO classoutput FROM ...