Skip to content

Instantly share code, notes, and snippets.

@kmoppel
Last active March 17, 2025 09:11
Show Gist options
  • Save kmoppel/dbc805c6a0519d4a9e0a5d9b92c03425 to your computer and use it in GitHub Desktop.
Save kmoppel/dbc805c6a0519d4a9e0a5d9b92c03425 to your computer and use it in GitHub Desktop.
Postgres extract production pg_stats to test envs
/* ON PROD DB */
CREATE TEMP TABLE exported_stats AS
select
schemaname,
tablename,
attname,
null_frac,
avg_width,
n_distinct,
most_common_freqs,
correlation,
most_common_vals::text::text[], -- assuming no super secret data (has real values)
histogram_bounds::text::text[] -- assuming no super secret data (has real values)
from pg_stats
where
not schemaname in ('pg_catalog', 'information_schema')
and tablename in ('pgbench_accounts')
--and (tablename, attname) = ('pgbench_accounts', 'abalance')
;
\copy exported_stats to '/tmp/exported_pg_stats.copy'
/* ON PLAY DB */
CREATE TABLE public.exported_stats (
schemaname name,
tablename name,
attname name,
null_frac real,
avg_width integer,
n_distinct real,
most_common_freqs real[],
correlation real,
most_common_vals text[],
histogram_bounds text[]
);
\copy exported_stats from '/tmp/exported_pg_stats.copy'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment