-- key_cut by default: sift.key_cut_decision says what CREATE INDEX decides (the same two steps over the same sample), so the option a build writes is the decision it reports: -- for leaves filled in 100% to 0% of the rows, and for a table of the shape of a large table of dated records (an entity stored in runs, a key of few values, a scattered date, a -- sparse soft-delete timestamp, an arrival timestamp) at its own size and with the row estimate of a table of 257,000,000 rows CREATE EXTENSION pg_sift; SET client_min_messages = warning; SET max_parallel_maintenance_workers = 0; SET max_parallel_workers_per_gather = 0; CREATE TABLE kz (id int PRIMARY KEY, tenant_id int, a1 int, z_all int, z_50 int, z_10 int, z_1 int, z_02 int, z_0 int, z_rand int, pad text) WITH (fillfactor = 10, autovacuum_enabled = false); SELECT setseed(0.5); INSERT INTO kz SELECT i, i % 101, i % 1000, i, CASE WHEN random() < 0.5 THEN i END, CASE WHEN random() < 0.1 THEN i END, CASE WHEN random() < 0.01 THEN i END, CASE WHEN random() < 0.002 THEN i END, NULL, (random() * 1000000)::int, repeat('x', 100) FROM generate_series(1, 60000) i; CREATE TABLE ko (id int PRIMARY KEY, tenant_id int, kind_id int, day date, gone_at timestamp, upd_at timestamp, pad text) WITH (autovacuum_enabled = false); INSERT INTO ko SELECT i, 1 + ((i - 1) / 7800 * 7919) % 38, CASE WHEN random() < 0.85 THEN 1 + ((i - 1) / 7800 * 31) % 17 ELSE 1 + (power(random(), 3) * 17)::int END, DATE '2010-01-01' + (random() * 5083)::int, CASE WHEN random() < 0.002 THEN TIMESTAMP '2021-01-01' + i * interval '1 second' END, TIMESTAMP '2020-01-01' + i * interval '8 seconds' + random() * interval '3 days', repeat('z', 600) FROM generate_series(1, 300000) i; VACUUM (FREEZE, ANALYZE) kz, ko; -- the column the decision cuts by (none), and the one the build wrote CREATE FUNCTION kagree(tbl regclass, cols text) RETURNS TABLE (decided text, built text, same boolean) LANGUAGE plpgsql AS $$ BEGIN EXECUTE format('CREATE INDEX ka_i ON %s USING sift_col (%s)', tbl, cols); RETURN QUERY SELECT d.d, b.b, d.d = b.b FROM (SELECT coalesce((SELECT leaf FROM sift.key_cut_decision('ka_i') WHERE decision = 'cut'), 'none') d) d, (SELECT coalesce((SELECT substring(o FROM 'key_cut=auto: (.*)') FROM pg_class c, unnest(c.reloptions) o WHERE c.relname = 'ka_i' AND o ~ '^key_cut=auto'), 'none') b) b; DROP INDEX ka_i; END $$; SELECT z AS leaf, k.* FROM unnest(ARRAY['z_all', 'z_50', 'z_10', 'z_1', 'z_02', 'z_0', 'z_rand']) z, LATERAL kagree('kz', 'id, tenant_id, a1, ' || z) k; SELECT 'large-table shape' AS table, k.* FROM kagree('ko', 'id, tenant_id, kind_id, day, gone_at, upd_at') k; UPDATE pg_class SET reltuples = 257000000 WHERE relname = 'ko'; SELECT 'large-table shape, 257M rows estimated' AS table, k.* FROM kagree('ko', 'id, tenant_id, kind_id, day, gone_at, upd_at') k; DROP TABLE kz, ko; DROP FUNCTION kagree(regclass, text); DROP EXTENSION pg_sift;