-- A scan of a sift_table that reads a wide column (bytea, here) holds the distinct values of the rows of one unit in a dictionary: past the limit -- wide_dict_bytes (1 GB) it fails naming the column. sift.test_limits lowers it to 64 kB, where 3000 values of 80 bytes do not fit and 7 do. CREATE EXTENSION pg_sift; SET client_min_messages = warning; SET max_parallel_workers_per_gather = 0; SET cpu_tuple_cost = 0.5; CREATE TABLE wl (id int, g int, few bytea, many bytea) USING sift_table; INSERT INTO wl SELECT i, i % 7, decode(md5((i % 7)::text), 'hex'), decode(repeat(md5(i::text), 5), 'hex') FROM generate_series(1, 3000) i; VACUUM wl; CREATE FUNCTION wl_distinct(col text) RETURNS text LANGUAGE plpgsql AS $$ DECLARE n bigint; hint text; BEGIN EXECUTE format('SELECT sum(c) FROM (SELECT g, count(DISTINCT %I) c FROM wl GROUP BY g) s', col) INTO n; RETURN 'ok ' || n; EXCEPTION WHEN program_limit_exceeded THEN GET STACKED DIAGNOSTICS hint = PG_EXCEPTION_HINT; RETURN regexp_replace(SQLERRM, '[0-9]+ bytes', 'N bytes') || ' / ' || regexp_replace(hint, '[0-9]+', 'N'); END $$; SELECT wl_distinct('many') AS default_limit; SET sift.test_limits = 'wide_dict_bytes=65536'; SELECT wl_distinct('many') AS many_values; SELECT wl_distinct('few') AS few_values; RESET sift.test_limits; SELECT wl_distinct('many') AS reset; DROP FUNCTION wl_distinct(text); DROP TABLE wl; DROP EXTENSION pg_sift;