dropdb --if-exists stats_src
createdb stats_src

psql -X -v ON_ERROR_STOP=1 stats_src <<'SQL'
CREATE SCHEMA s1;

CREATE TABLE s1.t AS
  SELECT g AS a, g % 10 AS b
  FROM generate_series(1, 1000) AS g;

CREATE INDEX idx_expr ON s1.t ((a + 1));

ANALYZE s1.t;
SQL

pg_dump --statistics-only -Fc -n s1 -f /tmp/stats-only.dump stats_src

pg_restore --statistics-only -n s1 --table t \
  -f /tmp/table-stats-only.sql /tmp/stats-only.dump

pg_restore --statistics-only -n s1 --index idx_expr \
  -f /tmp/index-stats-only.sql /tmp/stats-only.dump

echo 'archive TOC:'
pg_restore --list /tmp/stats-only.dump | grep -E 'TABLE|INDEX|STATISTICS DATA'

echo 'pg_restore --statistics-only --table t:'
grep -E 'pg_restore_(relation|attribute)_stats' /tmp/table-stats-only.sql || true

echo 'pg_restore --statistics-only --index idx_expr:'
grep -E 'pg_restore_(relation|attribute)_stats' /tmp/index-stats-only.sql || true

